123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- using System.Drawing;
- using System.Drawing.Text;
- using System.Windows.Forms;
- using Microsoft.Drawing;
- namespace Microsoft.Windows.Forms
- {
- /// <summary>
- /// 虚拟按钮
- /// </summary>
- public class UIButton : UIControl
- {
- private Color m_HoveredBackColor = DefaultTheme.DarkDarkTransparent;
- /// <summary>
- /// 鼠标悬停背景色
- /// </summary>
- public virtual Color HoveredBackColor
- {
- get
- {
- return this.m_HoveredBackColor;
- }
- set
- {
- if (value != this.m_HoveredBackColor)
- {
- this.m_HoveredBackColor = value;
- this.Invalidate();
- }
- }
- }
- private Color m_PressedBackColor = DefaultTheme.DarkTransparent;
- /// <summary>
- /// 鼠标按下背景色
- /// </summary>
- public virtual Color PressedBackColor
- {
- get
- {
- return this.m_PressedBackColor;
- }
- set
- {
- if (value != this.m_PressedBackColor)
- {
- this.m_PressedBackColor = value;
- this.Invalidate();
- }
- }
- }
- private Image m_Image;
- /// <summary>
- /// 图片
- /// </summary>
- public virtual Image Image
- {
- get
- {
- return this.m_Image;
- }
- set
- {
- if (value != this.m_Image)
- {
- this.m_Image = value;
- this.Invalidate();
- }
- }
- }
- private Image m_ImageHovered;
- /// <summary>
- /// 图片
- /// </summary>
- public virtual Image ImageHovered
- {
- get
- {
- return this.m_ImageHovered;
- }
- set
- {
- if (value != this.m_ImageHovered)
- {
- this.m_ImageHovered = value;
- this.Invalidate();
- }
- }
- }
- private Image m_ImagePressed;
- /// <summary>
- /// 图片
- /// </summary>
- public virtual Image ImagePressed
- {
- get
- {
- return this.m_ImagePressed;
- }
- set
- {
- if (value != this.m_ImagePressed)
- {
- this.m_ImagePressed = value;
- this.Invalidate();
- }
- }
- }
- private Image m_ImageDisabled;
- /// <summary>
- /// 图片
- /// </summary>
- public virtual Image ImageDisabled
- {
- get
- {
- return this.m_ImageDisabled;
- }
- set
- {
- if (value != this.m_ImageDisabled)
- {
- this.m_ImageDisabled = value;
- this.Invalidate();
- }
- }
- }
- private Size m_ImageSize;
- /// <summary>
- /// 图片大小
- /// </summary>
- public virtual Size ImageSize
- {
- get
- {
- return this.m_ImageSize;
- }
- set
- {
- if (value != this.m_ImageSize)
- {
- this.m_ImageSize = value;
- this.Invalidate();
- }
- }
- }
- private ContentAlignment m_ImageAlign = ContentAlignment.MiddleCenter;
- /// <summary>
- /// 图片对齐方式
- /// </summary>
- public virtual ContentAlignment ImageAlign
- {
- get
- {
- return this.m_ImageAlign;
- }
- set
- {
- if (value != this.m_ImageAlign)
- {
- this.m_ImageAlign = value;
- this.Invalidate();
- }
- }
- }
- private TextRenderingHint m_TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
- /// <summary>
- /// 文本呈现质量
- /// </summary>
- public virtual TextRenderingHint TextRenderingHint
- {
- get
- {
- return this.m_TextRenderingHint;
- }
- set
- {
- if (value != this.m_TextRenderingHint)
- {
- this.m_TextRenderingHint = value;
- this.Invalidate();
- }
- }
- }
- private ContentAlignment m_TextAlign = ContentAlignment.MiddleCenter;
- /// <summary>
- /// 文本对齐方式
- /// </summary>
- public virtual ContentAlignment TextAlign
- {
- get
- {
- return this.m_TextAlign;
- }
- set
- {
- if (value != this.m_TextAlign)
- {
- this.m_TextAlign = value;
- this.Invalidate();
- }
- }
- }
- private TextImageRelation m_TextImageRelation = TextImageRelation.ImageBeforeText;
- /// <summary>
- /// 文本图片关系
- /// </summary>
- public virtual TextImageRelation TextImageRelation
- {
- get
- {
- return this.m_TextImageRelation;
- }
- set
- {
- if (value != this.m_TextImageRelation)
- {
- this.m_TextImageRelation = value;
- this.Invalidate();
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- protected override State GetState()
- {
- if (this.Enabled)
- {
- if (this.Capture)
- {
- if ((Control.MouseButtons & MouseButtons.Left) != 0)//左键按下
- return State.Pressed;
- else
- return State.Hovered;
- }
- else
- {
- return State.Normal;
- }
- }
- else
- {
- return State.Disabled;
- }
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- public UIButton()
- {
- this.BackColor = DefaultTheme.Transparent;
- }
- /// <summary>
- /// 渲染控件
- /// </summary>
- /// <param name="e">数据</param>
- protected override void RenderSelf(PaintEventArgs e)
- {
- //准备
- Graphics g = e.Graphics;
- Rectangle rect = RectangleEx.Subtract(this.ClientRectangle, this.Padding);
- //渲染
- this.Sprite.BorderVisibleStyle = BorderVisibleStyle.None;
- this.Sprite.BackColor = this.BackColor;
- this.Sprite.BackColorHovered = this.HoveredBackColor;
- this.Sprite.BackColorPressed = this.PressedBackColor;
- this.Sprite.Image = this.Image;
- this.Sprite.ImageHovered = this.ImageHovered;
- this.Sprite.ImagePressed = this.ImagePressed;
- this.Sprite.ImageDisabled = this.ImageDisabled;
- this.Sprite.ImageSize = this.ImageSize;
- this.Sprite.ImageAlign = this.ImageAlign;
- this.Sprite.Font = this.Font;
- this.Sprite.ForeColor = this.ForeColor;
- this.Sprite.ForeColorHovered = this.ForeColor;
- this.Sprite.ForeColorPressed = this.ForeColor;
- this.Sprite.ForeColorDisabled = this.ForeColor;
- this.Sprite.Text = this.Text;
- this.Sprite.TextRenderingHint = this.TextRenderingHint;
- this.Sprite.TextAlign = this.TextAlign;
- this.Sprite.TextImageRelation = this.TextImageRelation;
- this.Sprite.State = this.State;
- this.Sprite.BeginRender(g);
- this.Sprite.RenderBackColor(rect);
- this.Sprite.RenderTextAndImage(rect);
- this.Sprite.EndRender();
- }
- }
- }
|