C#圆形Button——RoundButton
2016-07-20 11:36
441 查看
C#中自定义控件,圆形按钮。编译生成后会在工具箱生成自定义的控件,通过其半径属性设置大小,通过ImageNormal等属性设置图片。
【欢迎上码】
【微信公众号搜索 h2o2s2】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace YcPackageTool.Widgets { class RoundButton : Button { private int radius;//半径 //圆形按钮的半径属性 [CategoryAttribute("布局"), BrowsableAttribute(true), ReadOnlyAttribute(false)] public int Radius { set { radius = value; this.Height = this.Width = Radius; } get { return radius; } } private Image imageEnter; [CategoryAttribute("外观"), BrowsableAttribute(true), ReadOnlyAttribute(false)] public Image ImageEnter { set { imageEnter = value; } get { return imageEnter; } } private Image imageNormal; [CategoryAttribute("外观"), BrowsableAttribute(true), ReadOnlyAttribute(false)] public Image ImageNormal { set { imageNormal = value; BackgroundImage = imageNormal; } get { return imageNormal; } } //以下代码用于在VS中隐藏BackgroundImage属性,使得只能通过Diameter设置Height和Width [BrowsableAttribute(false)] public new Image BackgroundImage { get { return base.BackgroundImage; } set { base.BackgroundImage = value; } } //以下代码用于在VS中隐藏Size属性,使得只能通过Diameter设置Height和Width [BrowsableAttribute(false)] public new Size Size { get { return base.Size; } set { base.Size = value; } } public RoundButton() { Radius = 64; this.Height = this.Width = Radius; this.FlatStyle = FlatStyle.Flat; this.FlatAppearance.BorderSize = 0; this.BackgroundImage = imageEnter; this.BackgroundImageLayout = ImageLayout.Stretch; } //重写OnPaint protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { base.OnPaint(e); System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath(); path.AddEllipse(0, 0, Radius, Radius); this.Region = new Region(path); } //重写OnMouseEnter //protected override void OnMouseEnter(EventArgs e) //{ // Graphics g = this.CreateGraphics(); // g.DrawEllipse(new Pen(Color.Blue), 0, 0, this.Width, this.Height); // g.Dispose(); //} //重写OnSizeChanged protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); if (Height != Radius) { Radius = Width = Height; } else if (Width != Radius) { Radius = Height = Width; } } //重写OnMouseEnter protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); BackgroundImage = ImageEnter; } //重写OnMouseLeave protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); BackgroundImage = ImageNormal; } } }
【欢迎上码】
【微信公众号搜索 h2o2s2】
相关文章推荐
- ComboBox自定义下拉控件
- C#——读写ini文件
- C# 使用 CancellationTokenSource 终止线程
- C# ListView用法详解
- c#——Winform PropertyGrid使用(二)
- Xamarin学习:点按按钮累加次数并显示
- C# 根据身份证号码获取简易信息
- C#基础-3:委托(Delegate)
- dev gridview显示行号
- [c#]获取exchange中的图片
- C#中压缩/解压缩7-zip文件的方法
- C# 发送邮件 附件名称为空
- 参数数组和普通数组
- C#写的邮箱发送程序源代码
- c# - 反序列化报错:Input string was not in a correct format
- C# 枚举的使用
- C# 正则表达式的使用
- C#生成二维码名片
- C#生成二维码名片
- C#的 第6节课