赞
踩
Winform控件美化有两种方式:一是继承自现有控件,使用现有控件的属性、方法开发美化。另一种是继承Control类,完全自定义控件开发。
这篇文章以按键美化为例讲继承现有控件开发。
按键有三种鼠标状态:空状态,鼠标放上及鼠标按下。定义枚举类型
/// <summary>
/// 鼠标状态
/// </summary>
public enum MouseActionType
{
None,
Hover,
Click
}
对应鼠标的三种状态,定义三种颜色与状态对应
新建ButtonEx类,继承自Button类,引入System.Windows.Forms命令空间
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApp1 { public class ButtonEx : Button { private MouseActionType mouseAction; private string _title = "title"; /// <summary> /// /// </summary> [Description("获取或设置标题")] public string Title { get { return _title; } set { _title = value; } } private Color _baseColor = Color.DodgerBlue; private Color _clickColor = Color.FromArgb(255, 0, 0); [Description("鼠标按下颜色")] [Category("外观")] public Color ClickColor { get { return _clickColor; } set { _clickColor = value; } } private Color _enterColor = Color.Red; [Description("鼠标进入色")] [Category("外观")] public Color EnterColor { get { return _enterColor; } set { _enterColor = value; } } /// <summary> /// 构造函数 /// </summary> public ButtonEx () { mouseAction = MouseActionType.None; this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.Opaque, false); this.Width = 20; this.Height = 20; } protected override void OnPaint(PaintEventArgs pevent) { //base.OnPaint(pevent); Graphics g = pevent.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; g.CompositingQuality = CompositingQuality.HighQuality; Color clr = this.BackColor; switch (mouseAction) { case MouseActionType.None: using (SolidBrush sb = new SolidBrush(BackColor)) { g.FillRectangle(sb, this.ClientRectangle); } break; case MouseActionType.Hover: using (SolidBrush sb = new SolidBrush(_enterColor)) { g.FillRectangle(sb, this.ClientRectangle); } break; case MouseActionType.Click: using (SolidBrush sb = new SolidBrush(_clickColor)) { g.FillRectangle(sb, this.ClientRectangle); } break; default: break; } using (Pen pen = new Pen(Color.Red, 2f)) { g.DrawLine(pen, new Point(4, 4), new Point(Width - 4, Height - 4)); g.DrawLine(pen, new Point(Width - 4, 4), new Point(4, Height - 4)); } } // 以下重写鼠标事件 protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { this.mouseAction = MouseActionType.Click; this.Invalidate(); } base.OnMouseDown(e); } protected override void OnMouseUp(MouseEventArgs e) { this.mouseAction = MouseActionType.Hover; this.Invalidate(); base.OnMouseUp(e); } protected override void OnMouseHover(EventArgs e) { this.mouseAction = MouseActionType.Hover; this.Invalidate(); base.OnMouseHover(e); } protected override void OnMouseEnter(EventArgs e) { this.mouseAction = MouseActionType.Hover; this.Invalidate(); base.OnMouseEnter(e); } protected override void OnMouseLeave(EventArgs e) { this.mouseAction = MouseActionType.None; this.Invalidate(); base.OnMouseLeave(e); } } } /// <summary> /// 鼠标状态 /// </summary> public enum MouseActionType { None, Hover, Click }
使用按键前先生成解决方案,编译通过后工具栏里会出现相应按键
属性窗体对应颜色
F5启动程序
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。