Common.cs 21.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Asa.Face
{
    public class Common
    {
        /// <summary>
        /// 背景色
        /// </summary>
        public static readonly Color BACK_COLOR = Color.FromArgb(30, 30, 30);
        /// <summary>
        /// 背景色画笔
        /// </summary>
        public static readonly SolidBrush BACK_BRUSH = new SolidBrush(BACK_COLOR);

        /// <summary>
        /// 标题颜色
        /// </summary>
        public static readonly Color TITLE_COLOR = Color.FromArgb(40, 40, 40);
        /// <summary>
        /// 标题画笔
        /// </summary>
        public static readonly SolidBrush TITLE_BRUSH = new SolidBrush(TITLE_COLOR);

        /// <summary>
        /// 前景色
        /// </summary>
        public static readonly Color FORE_COLOR = Color.FromArgb(240, 240, 240);
        /// <summary>
        /// 前景色
        /// </summary>
        public static readonly Color FORE_COLOR_LOST = Color.FromArgb(70, 70, 70);
        /// <summary>
        /// 前景色画笔
        /// </summary>
        public static readonly SolidBrush FORE_BRUSH = new SolidBrush(FORE_COLOR);
        /// <summary>
        /// 前景色画笔
        /// </summary>
        public static readonly SolidBrush FORE_BRUSH_LOST = new SolidBrush(FORE_COLOR_LOST);
        /// <summary>
        /// 前景色
        /// </summary>
        public static readonly Pen FORE_PEN = new Pen(FORE_COLOR, 1);

        /// <summary>
        /// 鼠标经过
        /// </summary>
        public static readonly Color OVER_COLOR = Color.FromArgb(50, 50, 50);
        /// <summary>
        /// 鼠标经过画笔
        /// </summary>
        public static readonly SolidBrush OVER_BRUSH = new SolidBrush(OVER_COLOR);

        /// <summary>
        /// 鼠标按下
        /// </summary>
        public static readonly Color DOWN_COLOR = Color.FromArgb(60, 60, 60);
        /// <summary>
        /// 鼠标按下画笔
        /// </summary>
        public static readonly SolidBrush DOWN_BRUSH = new SolidBrush(DOWN_COLOR);

        /// <summary>
        /// 蓝色
        /// </summary>
        public static readonly Color BLUE_COLOR = Color.FromArgb(0, 122, 204);
        /// <summary>
        /// 蓝色画笔
        /// </summary>
        public static readonly SolidBrush BLUE_BRUSH = new SolidBrush(BLUE_COLOR);
        /// <summary>
        /// 蓝色
        /// </summary>
        public static readonly Pen BLUE_PEN = new Pen(BLUE_COLOR, 1);

        /// <summary>
        /// 边框颜色
        /// </summary>
        public static readonly Color BORDER_COLOR = Color.FromArgb(80, 80, 80);
        /// <summary>
        /// 边框画笔
        /// </summary>
        public static readonly SolidBrush BORDER_BRUSH = new SolidBrush(BORDER_COLOR);
        /// <summary>
        /// 边框色
        /// </summary>
        public static readonly Pen BORDER_PEN = new Pen(BORDER_COLOR, 1);

        /// <summary>
        /// 列表项行1
        /// </summary>
        public static readonly Color ITEM_ROW1 = Color.FromArgb(10, 10, 10);
        /// <summary>
        /// 列表项行2
        /// </summary>
        public static readonly Color ITEM_ROW2 = Color.FromArgb(30, 30, 30);
        /// <summary>
        /// 列表项行1
        /// </summary>
        public static readonly SolidBrush ITEM_ROW1_BRUSH = new SolidBrush(ITEM_ROW1);
        /// <summary>
        /// 列表项行2
        /// </summary>
        public static readonly SolidBrush ITEM_ROW2_BRUSH = new SolidBrush(ITEM_ROW2);
        /// <summary>
        /// 宋体,9号
        /// </summary>
        public static readonly Font FONT_SONG9 = new Font("宋体", 9f);

        /// <summary>
        /// 光标闪烁速度
        /// </summary>
        public static readonly int CURSOR_SPEED = API.GetCaretBlinkTime();

    }

    /// <summary>
    /// 形状,符号
    /// </summary>
    public static class Shape
    {
        /// <summary>
        /// 关闭按钮
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="space"></param>
        /// <returns></returns>
        public static Point[] FormClose(Rectangle rect, int space)
        {
            int w = rect.Width - space - space;
            int h = rect.Height - space - space;
            int l, t;
            if (w > h)
            {
                l = (w - h) / 2 + space;
                t = space;
                w = h;
            }
            else
            {
                l = space;
                t = (h - w) / 2 + space;
                h = w;
            }

            Point[] pt = new Point[4];
            pt[0] = new Point(rect.X + l, rect.Y + t);
            pt[1] = new Point(rect.X + l + w, rect.Y + t + h);
            pt[2] = new Point(rect.X + l, rect.Y + t + h);
            pt[3] = new Point(rect.X + l + w, rect.Y + t);
            return pt;
        }

        /// <summary>
        /// 最大化按钮
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="space"></param>
        /// <returns></returns>
        public static Point[] FormMax(Rectangle rect, int space)
        {
            int w = rect.Width - space - space;
            int h = rect.Height - space - space;
            int l, t;
            if (w > h)
            {
                l = (w - h) / 2 + space;
                t = space;
                w = h;
            }
            else
            {
                l = space;
                t = (h - w) / 2 + space;
                h = w;
            }

            Point[] pt = new Point[8];
            pt[0] = new Point(rect.X + l, rect.Y + t);
            pt[1] = new Point(rect.X + l + w, rect.Y + t);
            pt[2] = new Point(rect.X + l + w, rect.Y + t);
            pt[3] = new Point(rect.X + l + w, rect.Y + t + h);
            pt[4] = new Point(rect.X + l + w, rect.Y + t + h);
            pt[5] = new Point(rect.X + l, rect.Y + t + h);
            pt[6] = new Point(rect.X + l, rect.Y + t + h);
            pt[7] = new Point(rect.X + l, rect.Y + t);
            return pt;
        }

        /// <summary>
        /// 最大化还原按钮
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="space"></param>
        /// <returns></returns>
        public static Point[] FormRestore(Rectangle rect, int space)
        {
            int w = rect.Width - space - space;
            int h = rect.Height - space - space;
            int l, t;
            if (w > h)
            {
                l = (w - h) / 2 + space;
                t = space;
                w = h;
            }
            else
            {
                l = space;
                t = (h - w) / 2 + space;
                h = w;
            }

            int n = 3;
            Point[] pt = new Point[16];
            pt[0] = new Point(rect.X + l, rect.Y + t + n);
            pt[1] = new Point(rect.X + l + w - n, rect.Y + t + n);
            pt[2] = new Point(rect.X + l + w - n, rect.Y + t + n);
            pt[3] = new Point(rect.X + l + w - n, rect.Y + t + h);
            pt[4] = new Point(rect.X + l + w - n, rect.Y + t + h);
            pt[5] = new Point(rect.X + l, rect.Y + t + h);
            pt[6] = new Point(rect.X + l, rect.Y + t + h);
            pt[7] = new Point(rect.X + l, rect.Y + t + n);
            pt[8] = new Point(rect.X + l + n, rect.Y + t + n);
            pt[9] = new Point(rect.X + l + n, rect.Y + t);
            pt[10] = new Point(rect.X + l + n, rect.Y + t);
            pt[11] = new Point(rect.X + l + w, rect.Y + t);
            pt[12] = new Point(rect.X + l + w, rect.Y + t);
            pt[13] = new Point(rect.X + l + w, rect.Y + t + h - n);
            pt[14] = new Point(rect.X + l + w, rect.Y + t + h - n);
            pt[15] = new Point(rect.X + l + w - n, rect.Y + t + h - n);
            return pt;
        }

        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="space"></param>
        /// <returns></returns>
        public static Point[] FormMin(Rectangle rect, int space)
        {
            int w = rect.Width - space - space;
            int h = rect.Height - space - space;
            int l, t;
            if (w > h)
            {
                l = (w - h) / 2 + space;
                t = space;
                w = h;
            }
            else
            {
                l = space;
                t = (h - w) / 2 + space;
                h = w;
            }

            Point[] pt = new Point[2];
            pt[0] = new Point(rect.X + l, rect.Y + t + h);
            pt[1] = new Point(rect.X + l + w, rect.Y + t + h);
            return pt;
        }

        /// <summary>
        /// 右下角三角形
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="space"></param>
        /// <returns></returns>
        public static Point[] FormSizeTriangle(Rectangle rect, int space)
        {
            Point[] pt = new Point[8];
            pt[0] = new Point(rect.Right - space - 7, rect.Bottom - space - 1);
            pt[1] = new Point(rect.Right - space, rect.Bottom - space - 1);
            pt[2] = new Point(rect.Right - space - 5, rect.Bottom - space - 3);
            pt[3] = new Point(rect.Right - space, rect.Bottom - space - 3);
            pt[4] = new Point(rect.Right - space - 3, rect.Bottom - space - 5);
            pt[5] = new Point(rect.Right - space, rect.Bottom - space - 5);
            pt[6] = new Point(rect.Right - space - 1, rect.Bottom - space - 7);
            pt[7] = new Point(rect.Right - space, rect.Bottom - space - 7);
            return pt;
        }

        /// <summary>
        /// 三角形
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="space"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        public static GraphicsPath ControlTriangle(Rectangle rect, int space, Direction direction)
        {
            int w = rect.Width - space - space;
            int h = rect.Height - space - space;
            int l, t;
            if (w > h)
            {
                l = (w - h) / 2 + space;
                t = space;
                w = h;
            }
            else
            {
                l = space;
                t = (h - w) / 2 + space;
                h = w;
            }

            GraphicsPath path = new GraphicsPath();
            switch (direction)
            {
                case Direction.Left:
                    path.AddLine(rect.X + l, rect.Y + t + h / 2, rect.X + l + w, rect.Y + t);
                    path.AddLine(rect.X + l + w, rect.Y + t, rect.X + l + w, rect.Y + t + h);
                    path.AddLine(rect.X + l + w, rect.Y + t + h, rect.X + l, rect.Y + t + h / 2);
                    break;
                case Direction.Right:
                    path.AddLine(rect.X + l, rect.Y + t, rect.X + l + w, rect.Y + t + h / 2);
                    path.AddLine(rect.X + l + w, rect.Y + t + h / 2, rect.X + l, rect.Y + t + h);
                    path.AddLine(rect.X + l, rect.Y + t + h, rect.X + l, rect.Y + t);
                    break;
                case Direction.Top:
                    path.AddLine(rect.X + l + w / 2, rect.Y + t, rect.X + l, rect.Y + t + h);
                    path.AddLine(rect.X + l, rect.Y + t + h, rect.X + l + w, rect.Y + t + h);
                    path.AddLine(rect.X + l + w, rect.Y + t + h, rect.X + l + w / 2, rect.Y + t);
                    break;
                case Direction.Bottom:
                    path.AddLine(rect.X + l, rect.Y + t, rect.X + l + w, rect.Y + t);
                    path.AddLine(rect.X + l + w, rect.Y + t, rect.X + l + w / 2, rect.Y + t + h);
                    path.AddLine(rect.X + l + w / 2, rect.Y + t + h, rect.X + l, rect.Y + t);
                    break;
            }

            return path;
        }



        /// <summary>
        /// 最适合的大小
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="space"></param>
        /// <returns></returns>
        public static Point[] AutoFit(Rectangle rect, int space)
        {
            int w = (rect.Width - space - space) / 3;
            int h = (rect.Height - space - space) / 3;
            Point[] pt = new Point[16];

            pt[0] = new Point(space, space + h);
            pt[1] = new Point(space, space);
            pt[2] = new Point(space, space);
            pt[3] = new Point(space + w, space);

            pt[4] = new Point(rect.Width - space - w, space);
            pt[5] = new Point(rect.Width - space, space);
            pt[6] = new Point(rect.Width - space, space);
            pt[7] = new Point(rect.Width - space, space + h);

            pt[8] = new Point(rect.Width - space, rect.Height - space - h);
            pt[9] = new Point(rect.Width - space, rect.Height - space);
            pt[10] = new Point(rect.Width - space, rect.Height - space);
            pt[11] = new Point(rect.Width - space - w, rect.Height - space);

            pt[12] = new Point(space + w, rect.Height - space);
            pt[13] = new Point(space, rect.Height - space);
            pt[14] = new Point(space, rect.Height - space);
            pt[15] = new Point(space, rect.Height - space - h);

            for (int i = 0; i < pt.Length; i++)
            {
                pt[i].X += rect.X;
                pt[i].Y += rect.Y;
            }

            return pt;
        }

        /// <summary>
        /// 加号,加大
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="space"></param>
        /// <returns></returns>
        public static Point[] Plus(Rectangle rect, int space)
        {
            int t, l;
            int w = rect.Width - space - space;
            int h = rect.Height - space - space;
            if (w < h)
            {
                h = w;
                l = rect.X + space;
                t = rect.Y + (rect.Height - h) / 2;
            }
            else
            {
                w = h;
                t = rect.Y + space;
                l = rect.X + (rect.Width - w) / 2;
            }


            Point[] pt = new Point[4];
            pt[0] = new Point(l, t + h / 2);
            pt[1] = new Point(l + w, t + h / 2);
            pt[2] = new Point(l + w / 2, t);
            pt[3] = new Point(l + w / 2, t + h);

            return pt;

        }

        /// <summary>
        /// 减号,缩小
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="space"></param>
        /// <returns></returns>
        public static Point[] Minus(Rectangle rect, int space)
        {
            int w = rect.Width / 2;
            int h = rect.Height / 2;
            Point[] pt = new Point[2];

            pt[0] = new Point(rect.X + space, rect.Y + h);
            pt[1] = new Point(rect.Right - space, rect.Y + h);

            return pt;

        }

        ///// <summary>
        ///// 三角形
        ///// </summary>
        ///// <param name="rect"></param>
        ///// <param name="space"></param>
        ///// <param name="UpDown"></param>
        ///// <returns></returns>
        //public static Point[] Triangle(Rectangle rect, int space, bool UpDown)
        //{
        //    int wh = rect.Height - space - space;
        //    int l = rect.X + (rect.Width - wh) / 2;

        //    Point[] pt = new Point[3];
        //    if (UpDown)
        //    {
        //        pt[0] = new Point(l + wh / 2, rect.Y + space);
        //        pt[1] = new Point(l, rect.Y + space + wh);
        //        pt[2] = new Point(l + wh, rect.Y + space + wh);
        //    }
        //    else
        //    {
        //        pt[0] = new Point(l, rect.Y + space);
        //        pt[1] = new Point(l + wh, rect.Y + space);
        //        pt[2] = new Point(l + wh / 2, rect.Y + space + wh);
        //    }
        //    return pt;
        //}
    }

    /// <summary>
    /// 控件操作类
    /// </summary>
    public static class MyControl
    {
        /// <summary>
        /// 设置控件的背景色前景色
        /// </summary>
        /// <param name="ctl"></param>
        public static void SetColor(System.Windows.Forms.Control ctl)
        {
            ctl.BackColor = Common.BACK_COLOR;
            ctl.ForeColor = Common.FORE_COLOR;
        }
    }

    public static class MyEvent
    {
        public delegate void Changed(object sender);
        public delegate void Click(object sender);
    }

    /// <summary>
    /// 输入框
    /// </summary>
    public static class InputBox
    {
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="msg">输入信息</param>
        /// <param name="hint">提示</param>
        /// <param name="title">标题</param>
        /// <returns></returns>
        public static string Show(string msg, string hint, string title)
        {
            SurInput ff = new SurInput(msg, hint, title);
            System.Windows.Forms.DialogResult dr = ff.ShowDialog();
            ff.Dispose();
            if (dr == System.Windows.Forms.DialogResult.OK)
                return ff.Message;
            else
                return null;
        }
    }

    /// <summary>
    /// 消息对话框
    /// </summary>
    public static class MessageBox
    {
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="content">显示文本</param>
        /// <returns></returns>
        public static System.Windows.Forms.DialogResult Show(string content)
        {
            SurMessage msg = new SurMessage("", content, System.Windows.Forms.MessageBoxButtons.OK);
            System.Windows.Forms.DialogResult dr = msg.ShowDialog();
            msg.Dispose();
            return dr;
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">显示文本</param>
        /// <param name="buttons">按钮</param>
        /// <returns></returns>
        public static System.Windows.Forms.DialogResult Show(string title, string content, System.Windows.Forms.MessageBoxButtons buttons)
        {
            SurMessage msg = new SurMessage(title, content, buttons);
            System.Windows.Forms.DialogResult dr = msg.ShowDialog();
            msg.Dispose();
            return dr;
        }

        /// <summary>
        /// 显示错误对话框
        /// </summary>
        /// <param name="content">显示内容</param>
        /// <returns></returns>
        public static System.Windows.Forms.DialogResult ShowError(string content)
        {
            SurMessage msg = new SurMessage("ERROR", content, System.Windows.Forms.MessageBoxButtons.OK);
            System.Windows.Forms.DialogResult dr = msg.ShowDialog();
            msg.Dispose();
            return dr;
        }



    }














    public static class API
    {
        [DllImport("user32")]
        public static extern int ReleaseCapture();
        [DllImport("user32")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        [DllImport("user32")]
        public extern static int GetCaretBlinkTime();
        [DllImport("user32.dll")]
        public extern static bool GetCursorPos(out Point pt);
        [DllImport("gdi32.dll")]
        public static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
        [DllImport("gdi32.dll")]
        public static extern IntPtr DeleteDC(IntPtr hDc);
        [DllImport("gdi32.dll")]
        public static extern IntPtr DeleteObject(IntPtr hDc);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr ptr);
        [DllImport("user32.dll")]
        public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);
        [DllImport("user32.dll")]
        public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);

        public const int WM_SYSCOMMAND = 0x112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x2;
    }

    /// <summary>
    /// 控件控制按钮
    /// </summary>
    public class ControlButton
    {
        /// <summary>
        /// 按钮
        /// </summary>
        public Rectangle Rect;
        /// <summary>
        /// 图标
        /// </summary>
        public Point[] IconLine;
        /// <summary>
        /// 图标
        /// </summary>
        public GraphicsPath IconShape;
        /// <summary>
        /// 按下
        /// </summary>
        public bool Down;
        /// <summary>
        /// 经过
        /// </summary>
        public bool Over;
    }

    public enum Direction
    {
        Left, Top, Right, Bottom
    }

    /// <summary>
    /// 外观
    /// </summary>
    public enum Appearance
    {
        /// <summary>
        /// 左右
        /// </summary>
        LeftRight,
        /// <summary>
        /// 上下
        /// </summary>
        UpDown
    }

}