Common.cs 16.6 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
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace Asa.Theme
{
    /// <summary>
    /// 公共的参数
    /// </summary>
    public static 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 FORE_COLOR = Color.FromArgb(240, 240, 240);
        /// <summary>
        /// 前景色画笔
        /// </summary>
        public static readonly SolidBrush FORE_BRUSH = new SolidBrush(FORE_COLOR);
        /// <summary>
        /// 前景色线条,width=2
        /// </summary>
        public static readonly Pen FORE_PEN = new Pen(FORE_COLOR, BORDER_W);

        /// <summary>
        /// 鼠标经过
        /// </summary>
        public static readonly Color OVER_COLOR = Color.FromArgb(40, 40, 40);
        /// <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>
        /// 蓝色线条,width=2
        /// </summary>
        public static readonly Pen BLUE_PEN = new Pen(BLUE_COLOR, BORDER_W);

        /// <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>
        /// 边框线条,width=2
        /// </summary>
        public static readonly Pen BORDER_PEN = new Pen(BORDER_COLOR, BORDER_W);

        /// <summary>
        /// 右侧按钮的宽度
        /// </summary>
        public static int RBUTTON_W = 30;
        /// <summary>
        /// 边框的宽度
        /// </summary>
        private const int BORDER_W = 2;

        /// <summary>
        /// 错误颜色
        /// </summary>
        public static readonly Color ERROR_COLOR = Color.FromArgb(243, 139, 118);
        /// <summary>
        /// 运行颜色
        /// </summary>
        public static readonly Color RUN_COLOR = Color.FromArgb(141, 210, 138);
        /// <summary>
        /// 选择颜色
        /// </summary>
        public static readonly Color PICK_COLOR = Color.FromArgb(122, 193, 255);



        /// <summary>
        /// 字体,宋体,11号
        /// </summary>
        public static readonly Font FONT_SONG_11 = new Font("宋体", 11f);
        /// <summary>
        /// 字体,宋体,9号
        /// </summary>
        public static readonly Font FONT_SONG_9 = new Font("宋体", 9f);
        /// <summary>
        /// 字体,宋体,7号
        /// </summary>
        public static readonly Font FONT_SONG_7 = new Font("宋体", 7f);


    }

    /// <summary>
    /// 形状,符号
    /// </summary>
    public static class Shape
    {
        /// <summary>
        /// 乘号,叉叉
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="space"></param>
        /// <returns></returns>
        public static Point[] Multiply(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[] Underline(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[] 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 Calc
    {
        /// <summary>
        /// 字符串大小
        /// </summary>
        /// <param name="s"></param>
        /// <param name="font"></param>
        /// <param name="strSize"></param>
        /// <param name="charRect"></param>
        public static void StringSize(string s, Font font, out Size strSize, out Rectangle[] charRect)
        {
            strSize = new Size();
            charRect = null;

            //转成图片
            Size size = System.Windows.Forms.TextRenderer.MeasureText(s, font);
            Bitmap bmp = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            g.DrawString(s, font, Brushes.Black, 0, 0);
            g.Save();

            //扫描行宽度
            int stride = bmp.Width * 3;
            int m = stride % 4;
            if (m != 0) m = 4 - m;
            stride += m;

            //图片转数据流
            byte[] rtn = new byte[stride * bmp.Height];
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
            Marshal.Copy(bmpData.Scan0, rtn, 0, bmpData.Stride * bmp.Height);
            bmp.UnlockBits(bmpData);

            //去除补全字节
            int idx1 = 0, idx2 = 0;
            int len = bmp.Width * 3;
            byte[] bytSrc = new byte[bmp.Width * bmp.Height * 3];
            for (int i = 0; i < bmp.Height; i++)
            {
                Array.Copy(rtn, idx1, bytSrc, idx2, len);
                idx1 += len + m;
                idx2 += len;
            }

            //转灰度阈值化
            int n = 0;
            byte[] bytGray = new byte[bytSrc.Length / 3];
            for (int i = 0; i < bytGray.Length; i++)
            {
                byte bb = Convert.ToByte((bytSrc[n] + bytSrc[n + 1] + bytSrc[n + 2]) / 3);
                bytGray[i] = Convert.ToByte(bb < 128 ? 0 : 255);
                n += 3;
            }

            //获取大小
            int idx = 0;
            int white = 0;
            int left = bmp.Width;
            int top = bmp.Height;
            int right = 0;
            int bottom = 0;
            for (int w = 0; w < bmp.Width; w++)
            {
                for (int h = 0; h < bmp.Height; h++)
                {
                    idx = h * bmp.Width + w;
                    if (bytGray[idx] == 0)
                    {
                        if (w < left) left = w;
                        if (w > right) right = w;
                        if (h < top) top = h;
                        if (h > bottom) bottom = h;
                    }
                    else
                    {
                        white++;
                    }
                }
            }

            strSize = new Size(right - left + 1, bottom - top + 1);





        }

    }

    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();

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

    /// <summary>
    /// 控件操作类
    /// </summary>
    public static class Control
    {
        /// <summary>
        /// 移动控件
        /// </summary>
        /// <param name="handle">句柄</param>
        public static void Move(IntPtr handle)
        {
            API.ReleaseCapture();
            int rt = API.SendMessage(handle, API.WM_SYSCOMMAND, API.SC_MOVE + API.HTCAPTION, 0);
        }

        /// <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;
        }
    }

    /// <summary>
    /// 消息对话框
    /// </summary>
    public static class MessageBox
    {
        /// <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)
        {
            FlatMessage msg = new FlatMessage(title, content, buttons);
            msg.TopMost = true;
            System.Windows.Forms.DialogResult dr = msg.ShowDialog();
            return dr;
        }
    }

    /// <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)
        {
            FlatInput ff = new FlatInput(msg, hint, title);
            System.Windows.Forms.DialogResult dr = ff.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.Cancel)
                return "";
            else
                return ff.MsgStr;
        }
    }

    public static class Event
    {
        public delegate void StringEvent(object sender, string s);
        public delegate void ValueChanged(object sender);
        public delegate void SelectedIndexChanged(object sender);
        public delegate void ButtonClick(object sender, bool leftRight);
    }





    /// <summary>
    /// 文本与开关的相对位置
    /// </summary>
    public enum TextSwitchRelation
    {
        /// <summary>
        /// 开关在文本上面
        /// </summary>
        SwitchAboveText,
        /// <summary>
        /// 文本在开关上面
        /// </summary>
        TextAboveSwitch,
        /// <summary>
        /// 开关在文本前面
        /// </summary>
        SwitchBeforeText,
        /// <summary>
        /// 文本在开关前面
        /// </summary>
        TextBeforeSwitch
    }

    /// <summary>
    /// 控件方向
    /// </summary>
    public enum Direction
    {
        /// <summary>
        /// 水平
        /// </summary>
        Horizontal,
        /// <summary>
        /// 垂直
        /// </summary>
        Vertical
    }

    /// <summary>
    /// 按钮类型
    /// </summary>
    public enum ButtonTypes
    {
        /// <summary>
        /// 按钮
        /// </summary>
        Button,
        /// <summary>
        /// 左侧带状态颜色
        /// </summary>
        LeftState,
        /// <summary>
        /// 右侧带状态颜色
        /// </summary>
        RightState,
        /// <summary>
        /// 按钮,右侧带第二个按钮
        /// </summary>
        ButtonTwo,
        /// <summary>
        /// 左侧状态,中间按钮,右侧按钮
        /// </summary>
        StateButtonTwo
    }

}