FlatButton.cs 22.7 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Asa.Theme
{
    /// <summary>
    /// 按钮控件
    /// </summary>
    [DefaultProperty("Text")]
    [DefaultEvent("Click")]
    public partial class FlatButton : FlatBase
    {
        private bool _leftDown;
        private bool _rightDown;
        private ButtonTypes _type = ButtonTypes.Button;
        private Color _color = Common.BACK_COLOR;
        private Bitmap _img = null;
        private Size _imgSize = new Size();
        private ContentAlignment _textAlign = ContentAlignment.MiddleCenter;
        private bool _border = true;
        private bool _absolute = false;

        private Rectangle _rect;
        private RectangleF _imgRect;
        private RectangleF _textRect;
        private RectangleF _colorRect;
        private Rectangle _leftRect;
        private Rectangle _rightRect;
        private RectangleF _rightStrRect;

        private const int COLOR_W = 6;
        private const int BORDER_W = 2;


        /// <summary>
        /// 按钮控件
        /// </summary>
        public FlatButton()
        {
            InitializeComponent();
            base.Text = "";
            base.Font = new Font("宋体", 9f);
            CalcSize();
            Refresh();
        }


        #region 属性
        /// <summary>
        /// 控件上显示的图像
        /// </summary>
        [Browsable(true), Category(""), Description("控件上显示的图像"), DefaultValue(null)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Bitmap Image
        {
            set
            {
                _img = value;
                if (_img == null)
                {
                    ImageSize = new Size();
                }
                else
                {
                    if (_imgSize.Width == 0 || _imgSize.Height == 0)
                        ImageSize = _img.Size;
                    else
                    {
                        CalcSize();
                        Refresh();
                    }
                }
            }
            get
            {
                return _img;
            }
        }

        /// <summary>
        /// 显示的图像的大小
        /// </summary>
        [Browsable(true), Category(""), Description("显示的图像的大小")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Size ImageSize
        {
            set
            {
                _imgSize = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return _imgSize;
            }
        }

        /// <summary>
        /// 显示的文本
        /// </summary>
        [Browsable(true), Category(""), Description("显示的文本"), DefaultValue("")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get => base.Text;
            set
            {
                base.Text = value;
                CalcSize();
                Refresh();
            }
        }

        /// <summary>
        /// 显示的文本的字体
        /// </summary>
        [Browsable(true), Category(""), Description("显示的文本的字体")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override Font Font
        {
            set
            {
                base.Font = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return base.Font;
            }
        }

        /// <summary>
        /// 显示的文本的对齐方式
        /// </summary>
        [Browsable(true), Category(""), Description("显示的文本的对齐方式"), DefaultValue(ContentAlignment.MiddleCenter)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public ContentAlignment TextAlign
        {
            set
            {
                _textAlign = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return _textAlign;
            }
        }

        /// <summary>
        /// 文本对齐是否绝对对齐于原点
        /// </summary>
        [Browsable(true), Category(""), Description("文本对齐是否绝对对齐于原点"), DefaultValue(false)]
        public bool TextAbsolute
        {
            set
            {
                _absolute = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return _absolute;
            }
        }

        /// <summary>
        /// 状态颜色
        /// </summary>
        [Browsable(true), Category(""), Description("状态颜色")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Color StateColor
        {
            set
            {
                _color = value;
                Refresh();
            }
            get
            {
                return _color;
            }
        }

        /// <summary>
        /// 按钮的类型
        /// </summary>
        [Browsable(true), Category(""), Description("按钮的类型"), DefaultValue(ButtonTypes.Button)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public ButtonTypes ButtonType
        {
            set
            {
                _type = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return _type;
            }
        }

        /// <summary>
        /// 控件是否显示边框
        /// </summary>
        [Browsable(true), Category(""), Description("控件是否显示边框"), DefaultValue(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool Border
        {
            set
            {
                _border = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return _border;
            }
        }

        /// <summary>
        /// 单击时触发
        /// </summary>
        [Browsable(true), Category(""), Description("单击时触发")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public new event EventHandler Click;

        /// <summary>
        /// 单击右侧按钮时触发
        /// </summary>
        [Browsable(true), Category(""), Description("单击右侧按钮时触发")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public event EventHandler ClickRight;
        

        #endregion


        /// <summary>
        /// 计算大小
        /// </summary>
        protected override void CalcSize()
        {
            SizeF sf;
            int offsetL = 0, offsetR = 0;
            Graphics g = CreateGraphics();
            switch (ButtonType)
            {
                case ButtonTypes.Button:
                    break;
                case ButtonTypes.LeftState:
                    offsetL = COLOR_W;
                    _colorRect = new RectangleF(BORDER_W, BORDER_W, COLOR_W, Height - BORDER_W - BORDER_W);
                    break;
                case ButtonTypes.RightState:
                    offsetR = COLOR_W;
                    _colorRect = new RectangleF(Width - BORDER_W - COLOR_W, BORDER_W, COLOR_W, Height - BORDER_W - BORDER_W);
                    break;
                case ButtonTypes.ButtonTwo:
                    offsetR = Common.RBUTTON_W;
                    _rightRect = new Rectangle(Width - Common.RBUTTON_W - BORDER_W / 2, BORDER_W / 2, Common.RBUTTON_W, Height - BORDER_W);
                    sf = g.MeasureString("...", Common.FONT_SONG_9);
                    _rightStrRect = new RectangleF(_rightRect.X + (_rightRect.Width - sf.Width) / 2f, _rightRect.Y + (_rightRect.Height - sf.Height) / 2f - 2, sf.Width, sf.Height);
                    break;
                case ButtonTypes.StateButtonTwo:
                    offsetL = COLOR_W;
                    _colorRect = new RectangleF(BORDER_W, BORDER_W, COLOR_W, Height - BORDER_W - BORDER_W);
                    offsetR = Common.RBUTTON_W;
                    _rightRect = new Rectangle(Width - Common.RBUTTON_W - BORDER_W / 2, BORDER_W / 2, Common.RBUTTON_W, Height - BORDER_W);
                    sf = g.MeasureString("...", Common.FONT_SONG_9);
                    _rightStrRect = new RectangleF(_rightRect.X + (_rightRect.Width - sf.Width) / 2f, _rightRect.Y + (_rightRect.Height - sf.Height) / 2f - 2, sf.Width, sf.Height);
                    break;
            }

            sf = g.MeasureString(Text, Font);
            _leftRect = new Rectangle(BORDER_W / 2, BORDER_W / 2, Width - BORDER_W - offsetR, Height - BORDER_W);
            if(_border)
                _rect = new Rectangle(BORDER_W / 2, BORDER_W / 2, Width - BORDER_W, Height - BORDER_W);
            else
                _rect = new Rectangle(0, 0, Width, Height);

            if (_absolute)
            {
                offsetL = 0;
                offsetR = 0;
            }

            int space = 6;
            float imgX = 0, imgY = 0, textY = 0;
            if (_img == null)
            {
                switch (TextAlign)
                {
                    case ContentAlignment.TopLeft:
                        imgX = offsetL + BORDER_W;
                        textY = BORDER_W;
                        break;
                    case ContentAlignment.TopCenter:
                        imgX = offsetL + (Width - offsetL - offsetR - sf.Width) / 2f;
                        textY = BORDER_W;
                        break;
                    case ContentAlignment.TopRight:
                        imgX = Width - BORDER_W - sf.Width - offsetR;
                        textY = BORDER_W;
                        break;
                    case ContentAlignment.MiddleLeft:
                        imgX = offsetL + BORDER_W;
                        textY = (Height - sf.Height) / 2f;
                        break;
                    case ContentAlignment.MiddleCenter:
                        imgX = offsetL + (Width - offsetL - offsetR - sf.Width) / 2f;
                        textY = (Height - sf.Height) / 2f;
                        break;
                    case ContentAlignment.MiddleRight:
                        imgX = Width - BORDER_W - sf.Width - offsetR;
                        textY = (Height - sf.Height) / 2f;
                        break;
                    case ContentAlignment.BottomLeft:
                        imgX = offsetL + BORDER_W;
                        textY = Height - BORDER_W - sf.Height;
                        break;
                    case ContentAlignment.BottomCenter:
                        imgX = offsetL + (Width - offsetL - offsetR - sf.Width) / 2f;
                        textY = Height - BORDER_W - sf.Height;
                        break;
                    case ContentAlignment.BottomRight:
                        imgX = Width - BORDER_W - sf.Width - offsetR;
                        textY = Height - BORDER_W - sf.Height;
                        break;
                }
                _imgRect = new RectangleF();
                _textRect = new RectangleF(imgX, textY, sf.Width, sf.Height);
            }
            else
            {
                switch (TextAlign)
                {
                    case ContentAlignment.TopLeft:
                        imgX = BORDER_W + offsetL;
                        if (_imgSize.Height > sf.Height)
                        {
                            textY = BORDER_W + (_imgSize.Height - sf.Height) / 2f;
                            imgY = BORDER_W;
                        }
                        else
                        {
                            imgY = BORDER_W + (sf.Height - _imgSize.Height) / 2f;
                            textY = BORDER_W;
                        }
                        break;
                    case ContentAlignment.TopCenter:
                        imgX = offsetL + (Width - offsetL - offsetR - space - _imgSize.Width - sf.Width) / 2f;
                        if (_imgSize.Height > sf.Height)
                        {
                            textY = BORDER_W + (_imgSize.Height - sf.Height) / 2f;
                            imgY = BORDER_W;
                        }
                        else
                        {
                            imgY = BORDER_W + (sf.Height - _imgSize.Height) / 2f;
                            textY = BORDER_W;
                        }
                        break;
                    case ContentAlignment.TopRight:
                        imgX = Width - BORDER_W - _imgSize.Width - sf.Width - offsetR - space;
                        if (_imgSize.Height > sf.Height)
                        {
                            textY = BORDER_W + (_imgSize.Height - sf.Height) / 2f;
                            imgY = BORDER_W;
                        }
                        else
                        {
                            imgY = BORDER_W + (sf.Height - _imgSize.Height) / 2f;
                            textY = BORDER_W;
                        }
                        break;
                    case ContentAlignment.MiddleLeft:
                        imgX = BORDER_W + offsetL;
                        imgY = (Height - _imgSize.Height) / 2f;
                        textY = (Height - sf.Height) / 2f;
                        break;
                    case ContentAlignment.MiddleCenter:
                        imgX = offsetL + (Width - offsetL - offsetR - space - _imgSize.Width - sf.Width) / 2f;
                        imgY = (Height - _imgSize.Height) / 2f;
                        textY = (Height - sf.Height) / 2f;
                        break;
                    case ContentAlignment.MiddleRight:
                        imgX = Width - BORDER_W - _imgSize.Width - sf.Width - offsetR - space;
                        imgY = (Height - _imgSize.Height) / 2f;
                        textY = (Height - sf.Height) / 2f;
                        break;
                    case ContentAlignment.BottomLeft:
                        imgX = BORDER_W + offsetL;
                        if (_imgSize.Height > sf.Height)
                        {
                            imgY = Height - BORDER_W - _imgSize.Height;
                            textY = Height - BORDER_W - (imgY + _imgSize.Height) + (_imgSize.Height - sf.Height) / 2f;
                        }
                        else
                        {
                            imgY = Height - BORDER_W - sf.Height + (sf.Height - _imgSize.Height) / 2f;
                            textY = Height - BORDER_W - sf.Height;
                        }
                        break;
                    case ContentAlignment.BottomCenter:
                        imgX = offsetL + (Width - offsetL - offsetR - space - _imgSize.Width - sf.Width) / 2f;
                        if (_imgSize.Height > sf.Height)
                        {
                            imgY = Height - BORDER_W - _imgSize.Height;
                            textY = Height - BORDER_W - (imgY + _imgSize.Height) + (_imgSize.Height - sf.Height) / 2f;
                        }
                        else
                        {
                            imgY = Height - BORDER_W - sf.Height + (sf.Height - _imgSize.Height) / 2f;
                            textY = Height - BORDER_W - sf.Height;
                        }
                        break;
                    case ContentAlignment.BottomRight:
                        imgX = Width - BORDER_W - _imgSize.Width - sf.Width - offsetR - space;
                        if (_imgSize.Height > sf.Height)
                        {
                            imgY = Height - BORDER_W - _imgSize.Height;
                            textY = Height - BORDER_W - (imgY + _imgSize.Height) + (_imgSize.Height - sf.Height) / 2f;
                        }
                        else
                        {
                            imgY = Height - BORDER_W - sf.Height + (sf.Height - _imgSize.Height) / 2f;
                            textY = Height - BORDER_W - sf.Height;
                        }
                        break;
                }
                _imgRect = new RectangleF(imgX, imgY, _imgSize.Width, _imgSize.Height);
                _textRect = new RectangleF(imgX + space + _imgSize.Width, textY, sf.Width, sf.Height);
            }
        }

        private void FlatButton_Paint(object sender, PaintEventArgs e)
        {
            //e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            //e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            //e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

            switch (ButtonType)
            {
                case ButtonTypes.Button:
                    if (_leftDown)
                    {
                        e.Graphics.FillRectangle(Common.BLUE_BRUSH, _rect);
                        e.Graphics.DrawRectangle(Common.BLUE_PEN, _rect);
                    }
                    else if (Inside)
                    {
                        e.Graphics.FillRectangle(Common.OVER_BRUSH, _rect);
                        if (_border) e.Graphics.DrawRectangle(Common.BLUE_PEN, _rect);
                    }
                    else
                    {
                        e.Graphics.Clear(Common.BACK_COLOR);
                        if (_border) e.Graphics.DrawRectangle(Common.BORDER_PEN, _rect);
                    }
                    break;
                case ButtonTypes.LeftState:
                case ButtonTypes.RightState:
                    if (_leftDown)
                    {
                        e.Graphics.FillRectangle(Common.BLUE_BRUSH, _rect);
                        e.Graphics.DrawRectangle(Common.BLUE_PEN, _rect);
                    }
                    else if (Inside)
                    {
                        e.Graphics.FillRectangle(Common.OVER_BRUSH, _rect);
                        if (_border) e.Graphics.DrawRectangle(Common.BLUE_PEN, _rect);
                    }
                    else
                    {
                        e.Graphics.Clear(Common.BACK_COLOR);
                        if (_border) e.Graphics.DrawRectangle(Common.BORDER_PEN, _rect);
                    }
                    e.Graphics.FillRectangle(new SolidBrush(StateColor), _colorRect);
                    break;
                case ButtonTypes.ButtonTwo:
                    if (_leftDown)
                    {
                        e.Graphics.FillRectangle(Common.BLUE_BRUSH, _leftRect);
                        e.Graphics.DrawRectangle(Common.BLUE_PEN, _leftRect);
                        e.Graphics.FillRectangle(Common.OVER_BRUSH, _rightRect);
                        e.Graphics.DrawRectangle(Common.BLUE_PEN, _rightRect);
                    }
                    else if (_rightDown)
                    {
                        e.Graphics.FillRectangle(Common.BLUE_BRUSH, _rightRect);
                        e.Graphics.DrawRectangle(Common.BLUE_PEN, _rightRect);
                        e.Graphics.FillRectangle(Common.OVER_BRUSH, _leftRect);
                        e.Graphics.DrawRectangle(Common.BLUE_PEN, _leftRect);
                    }
                    else if (Inside)
                    {
                        e.Graphics.FillRectangle(Common.OVER_BRUSH, _rect);
                        if (_border) e.Graphics.DrawRectangle(Common.BLUE_PEN, _leftRect);
                        if (_border) e.Graphics.DrawRectangle(Common.BLUE_PEN, _rightRect);
                    }
                    else
                    {
                        e.Graphics.Clear(Common.BACK_COLOR);
                        if (_border) e.Graphics.DrawRectangle(Common.BORDER_PEN, _rect);
                    }
                    e.Graphics.DrawString("...", Common.FONT_SONG_9, Common.FORE_BRUSH, _rightStrRect);
                    break;
                case ButtonTypes.StateButtonTwo:
                    if (_leftDown)
                    {
                        e.Graphics.FillRectangle(Common.BLUE_BRUSH, _leftRect);
                        e.Graphics.DrawRectangle(Common.BLUE_PEN, _leftRect);
                        e.Graphics.FillRectangle(Common.OVER_BRUSH, _rightRect);
                        e.Graphics.DrawRectangle(Common.BLUE_PEN, _rightRect);
                    }
                    else if (_rightDown)
                    {
                        e.Graphics.FillRectangle(Common.BLUE_BRUSH, _rightRect);
                        e.Graphics.DrawRectangle(Common.BLUE_PEN, _rightRect);
                        e.Graphics.FillRectangle(Common.OVER_BRUSH, _leftRect);
                        e.Graphics.DrawRectangle(Common.BLUE_PEN, _leftRect);
                    }
                    else if (Inside)
                    {
                        e.Graphics.FillRectangle(Common.OVER_BRUSH, _rect);
                        if (_border) e.Graphics.DrawRectangle(Common.BLUE_PEN, _leftRect);
                        if (_border) e.Graphics.DrawRectangle(Common.BLUE_PEN, _rightRect);
                    }
                    else
                    {
                        e.Graphics.Clear(Common.BACK_COLOR);
                        if (_border)
                            e.Graphics.DrawRectangle(Common.BORDER_PEN, _rect);
                    }
                    e.Graphics.FillRectangle(new SolidBrush(StateColor), _colorRect);
                    e.Graphics.DrawString("...", Common.FONT_SONG_9, Common.FORE_BRUSH, _rightStrRect);
                    break;
            }

            if (_img != null) e.Graphics.DrawImage(_img, _imgRect, new RectangleF(0, 0, _img.Width, _img.Height), GraphicsUnit.Pixel);
            e.Graphics.DrawString(Text, Font, Common.FORE_BRUSH, _textRect);




        }

        private void FlatButton_MouseDown(object sender, MouseEventArgs e)
        {
            bool bln = _leftRect.Contains(e.Location);
            if (bln)
            {
                _leftDown = true;
                _rightDown = false;
                Refresh();
                return;
            }

            bln = _rightRect.Contains(e.Location);
            if (bln)
            {
                _leftDown = false;
                _rightDown = true;
                Refresh();
            }
        }

        private void FlatButton_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftDown)
            {
                Click?.Invoke(this, new EventArgs());
            }
            else if (_rightDown)
            {
                ClickRight?.Invoke(this, new EventArgs());
            }

            _leftDown = false;
            _rightDown = false;
            Refresh();
        }

    }
}