123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- 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 TestProject.Properties;
- using System.Drawing.Text;
- using System.Drawing.Drawing2D;
- namespace TestProject
- {
- public partial class QQButton : Button
- {
- private EMouseEnum _mouseState = EMouseEnum.Normal;
- private bool _isShowBorder = true;
- private TextFormatFlags _textAlign = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
- public QQButton()
- {
- InitializeComponent();
- }
- internal EMouseEnum MouseState
- {
- get { return this._mouseState; }
- set
- {
- this._mouseState = value;
- base.Invalidate();
- }
- }
- /// <summary>
- /// 整个按钮的区域
- /// </summary>
- internal Rectangle AllRect
- {
- get { return new Rectangle(0, 0, this.Width, this.Height); }
- }
- /// <summary>
- /// 是否显示发光边框
- /// </summary>
- [Description("是否显示发光边框")]
- public virtual bool IsShowBorder
- {
- get { return this._isShowBorder; }
- set { this._isShowBorder = value; }
- }
- /// <summary>
- /// 文字区域
- /// </summary>
- internal Rectangle TextRect
- {
- get { return new Rectangle(2, 2, this.AllRect.Width - 4, this.AllRect.Height - 4); }
- }
- /// <summary>
- /// 按钮上文字的对齐方式
- /// </summary>
- [Description("按钮上文字的对齐方式")]
- new public ContentAlignment TextAlign
- {
- get { return base.TextAlign; }
- set
- {
- base.TextAlign = value;
- switch (base.TextAlign)
- {
- case ContentAlignment.BottomCenter:
- this._textAlign = TextFormatFlags.Bottom |
- TextFormatFlags.HorizontalCenter |
- TextFormatFlags.SingleLine;
- break;
- case ContentAlignment.BottomLeft:
- this._textAlign = TextFormatFlags.Bottom |
- TextFormatFlags.Left |
- TextFormatFlags.SingleLine;
- break;
- case ContentAlignment.BottomRight:
- this._textAlign = TextFormatFlags.Bottom |
- TextFormatFlags.Right |
- TextFormatFlags.SingleLine;
- break;
- case ContentAlignment.MiddleCenter:
- this._textAlign = TextFormatFlags.SingleLine |
- TextFormatFlags.HorizontalCenter |
- TextFormatFlags.VerticalCenter;
- break;
- case ContentAlignment.MiddleLeft:
- this._textAlign = TextFormatFlags.Left |
- TextFormatFlags.VerticalCenter |
- TextFormatFlags.SingleLine;
- break;
- case ContentAlignment.MiddleRight:
- this._textAlign = TextFormatFlags.Right |
- TextFormatFlags.VerticalCenter |
- TextFormatFlags.SingleLine;
- break;
- case ContentAlignment.TopCenter:
- this._textAlign = TextFormatFlags.Top |
- TextFormatFlags.HorizontalCenter |
- TextFormatFlags.SingleLine;
- break;
- case ContentAlignment.TopLeft:
- this._textAlign = TextFormatFlags.Top |
- TextFormatFlags.Left |
- TextFormatFlags.SingleLine;
- break;
- case ContentAlignment.TopRight:
- this._textAlign = TextFormatFlags.Top |
- TextFormatFlags.Right |
- TextFormatFlags.SingleLine;
- break;
- }
- base.Invalidate(this.TextRect);
- }
- }
- protected override void OnPaint(PaintEventArgs pevent)
- {
- Graphics g = pevent.Graphics;
- g.SmoothingMode = SmoothingMode.AntiAlias;
- g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
- switch (MouseState)
- {
- case EMouseEnum.Leave:
- case EMouseEnum.Normal:
- if (base.Focused)
- {
- if (this.IsShowBorder)
- {
- using (Image focus = Resources.focus)
- {
- DrawHelper.RendererBackground(g, this.TextRect, focus, true);
- }
- }
- else
- {
- DrawHelper.RendererBackground(g, this.TextRect, Resources.normal, true);
- }
- }
- else
- {
- DrawHelper.RendererBackground(g, this.TextRect, Resources.normal, true);
- }
- break;
- case EMouseEnum.Up:
- case EMouseEnum.Move:
- DrawHelper.RendererBackground(g, this.TextRect, Resources.highlight, true);
- break;
- case EMouseEnum.Down:
- DrawHelper.RendererBackground(g, this.TextRect, Resources.Light, true);
- break;
- }
- TextRenderer.DrawText(g, this.Text, this.Font, this.TextRect, this.ForeColor, this._textAlign);
- }
- /// <summary>
- /// 引发 System.Windows.Forms.Form.MouseEnter 事件。
- /// </summary>
- /// <param name="e">包含事件数据的 System.EventArgs。</param>
- protected override void OnMouseEnter(EventArgs e)
- {
- base.OnMouseEnter(e);
- this.MouseState = EMouseEnum.Move;
- }
- /// <summary>
- /// 引发 System.Windows.Forms.Form.MouseLeave 事件。
- /// </summary>
- /// <param name="e">包含事件数据的 System.EventArgs。</param>
- protected override void OnMouseLeave(EventArgs e)
- {
- base.OnMouseLeave(e);
- this.MouseState = EMouseEnum.Leave;
- }
- /// <summary>
- /// 引发 System.Windows.Forms.Form.MouseDown 事件。
- /// </summary>
- /// <param name="mevent">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
- protected override void OnMouseDown(MouseEventArgs mevent)
- {
- base.OnMouseDown(mevent);
- this.MouseState = EMouseEnum.Down;
- }
- /// <summary>
- /// 引发 System.Windows.Forms.Form.MouseUp 事件。
- /// </summary>
- /// <param name="mevent">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
- protected override void OnMouseUp(MouseEventArgs mevent)
- {
- base.OnMouseUp(mevent);
- this.MouseState = EMouseEnum.Up;
- }
- }
- }
|