您的位置:首页 > 其它

圆形、椭圆形 按钮控件的制作

2009-06-11 00:56 459 查看
这个 也是朋友经常问我的,我也经常遇见的。就是 一个 非方形的按钮,即圆形、椭圆形按钮控件

首先它的制作过程是:新建一个 windows 窗体控件库,然后将 UserControl1.cs这个文件删除。

在 项目 添加新项 选择组件库 并命名为 EllipseButton.cs 。在EllipseButton.Designer.cs里添加2个事件

分别为 该控件的长度和文本改变 事件。 并添加头文件

using System.Windows.Forms;
using System.Drawing.Drawing2D;

Code
////吧Component修改为继承System.Windows.Forms.Button
public partial class EllipseButton : System.Windows.Forms.Button
{
private Color startColor=Color.Blue;
// private Color costartColor=Color.White;
private Color endColor = Color.GreenYellow;
[Description("设定渐变的起始色"),Category("Appearance")]//注视
public Color StartColor
{
get
{ return startColor;
}
set
{
startColor=value;
RePaint();
}
}
[Description("设定渐变的终止色"), Category("Appearance")]
public Color EndColor
{
get
{
return endColor;
}
set
{
endColor = value;
RePaint();
}

}
public EllipseButton()
{
InitializeComponent();
this.Width = 100;
this.Height = 100;
}
//绘制圆形区域
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);

Graphics g = pe.Graphics;
g.Clear(this.BackColor);
Rectangle rect = new Rectangle(0,0,this.Width,this.Height);
LinearGradientBrush myBrush = new LinearGradientBrush(rect,startColor,endColor,LinearGradientMode.ForwardDiagonal);
g.FillEllipse(myBrush, rect);
myBrush.Dispose();
StringFormat format = new StringFormat();
format.LineAlignment = StringAlignment.Center;
format.Alignment = StringAlignment.Center;
g.DrawString(this.Text, Font, new SolidBrush(this.ForeColor), rect, format);

}
//重新绘制圆形区域
private void RePaint()
{
Rectangle rect=new Rectangle(0,0,this.Width,this.Height);
OnPaint(new PaintEventArgs(this.CreateGraphics(), rect));
}
private void EllipseButton_Resize(object sender, System.EventArgs e)
{
RePaint();
}
private void EllipseButton_TextChanged(object sender, System.EventArgs e)
{
RePaint();
}

}
作者:FreeComputer
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: