标签:
实现的功能包含:
1)实现一个椭圆的按钮。可居中显示一行文本。
2)鼠标离开按钮和进入按钮时,按钮边框和背景色需要发生变化。
实现步骤:
1)新增一个用户控件库。
public class UserButton:System.Windows.Forms.UserControl { }
2)定义控件属性:
private Color _borderColor = Color.Blue; /// <summary> /// 按钮边框色 /// </summary> [System.ComponentModel.Category("Appearance")] [System.ComponentModel.DefaultValue(typeof(Color),"Blue")] public Color BorderColor { get { return _borderColor; } set { _borderColor = value; } } private Color _backColor = Color.White; /// <summary> /// 按钮背景色 /// </summary> [System.ComponentModel.Category("Appearance")] [System.ComponentModel.DefaultValue(typeof(Color), "White")] public Color BackColor { get { return _backColor; } set { _backColor = value; } } private Color _hoverBorderColor = Color.Red; /// <summary> /// 鼠标悬停在按钮上方时的边框色 /// </summary> [System.ComponentModel.Category("Appearance")] [System.ComponentModel.DefaultValue(typeof(Color), "Red")] public Color HoverBorderColor { get { return _hoverBorderColor; } set { _hoverBorderColor = value; } } private Color _hoverBackColor = Color.RoyalBlue; /// <summary> /// 按钮背景色 /// </summary> [System.ComponentModel.Category("Appearance")] [System.ComponentModel.DefaultValue(typeof(Color), "RoyalBlue")] public Color HoverBackColor { get { return _hoverBackColor; } set { _hoverBackColor = value; } } private string _caption = string.Empty; /// <summary> /// 按钮文本 /// </summary> [System.ComponentModel.Category("Appearance")] [System.ComponentModel.DefaultValue(null)] public string Caption { get { return _caption; } set { _caption = value; } } /// <summary> /// 鼠标悬停标志 /// </summary> private bool bMouseHoverFlag = false;
3)添加相关事件
/// <summary> /// /// </summary> /// <param name="e"></param> protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { base.OnPaint(e); using (System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath()) { path.AddEllipse(0,0,this.ClientSize.Width-1,this.ClientSize.Height-1); if (bMouseHoverFlag) { //绘制背景 using (SolidBrush b = new SolidBrush(this.HoverBackColor)) { e.Graphics.FillPath(b, path); } //绘制边框 using (Pen p = new Pen(this.HoverBorderColor)) { e.Graphics.DrawPath(p, path); } } else { //绘制背景 using (SolidBrush b = new SolidBrush(this.BackColor)) { e.Graphics.FillPath(b, path); } //绘制边框 using (Pen p = new Pen(this.BorderColor)) { e.Graphics.DrawPath(p, path); } } if (this.Caption != string.Empty) { using (StringFormat f = new StringFormat()) { f.Alignment = System.Drawing.StringAlignment.Center; f.LineAlignment = System.Drawing.StringAlignment.Center; f.FormatFlags = System.Drawing.StringFormatFlags.NoWrap; using (SolidBrush b = new SolidBrush(this.ForeColor)) { e.Graphics.DrawString(this.Caption,this.Font,b,new RectangleF(0,0,this.ClientSize.Width,this.ClientSize.Height),f); } } } } }
4)添加效果:
private bool CheckMouseHover(int x, int y) { bool flag = false; using (System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath()) { path.AddEllipse(0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1); flag = path.IsVisible(x,y); if (flag != bMouseHoverFlag) { bMouseHoverFlag = flag; this.Invalidate(); } } return flag; } protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e) { base.OnMouseMove(e); this.CheckMouseHover(e.X,e.Y); } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); this.CheckMouseHover(-1, -1); } protected override void OnClick(EventArgs e) { Point p = System.Windows.Forms.Control.MousePosition; p = base.PointToClient(p); if (CheckMouseHover(p.X, p.Y)) { base.OnClick(e); } }
标签:
原文地址:http://www.cnblogs.com/fer-team/p/4352193.html