C#中怎么重写button让button显示为圆形

高分请高手,
2024-11-19 10:45:48
推荐回答(3个)
回答1:

给你找了一个。///


/// 绘制圆形按钮
///

public void DrawRoundButton(Graphics g)
{
GraphicsPath path = new GraphicsPath();
path = path=DrawButtonFade(this.buttonPath , g ,this.backColor, this.frontColor);
Region region = new Region(path);
this.Bounds = rectangle;
this.Region = region;

}///
/// 绘制按下的按钮
///

public void DrawPushedButton(Graphics g)
{
GraphicsPath path = new GraphicsPath();
path=DrawButtonFade(this.buttonPath , g ,this.frontColor , this.backColor);
Region region = new Region(path);
this.Bounds = rectangle;
this.Region = region;

}///
/// 获取绘制按钮区域
///

public void GetButtonRegion()
{
roundRect = new Rectangle(0,0,this.Width,this.Height);//设置画椭圆区域
rectangle = new Rectangle(this.Left,this.Top,this.Width,this.Height);//设定按钮显示位置和最大响应范围,只能是矩形
buttonCenter = new Point ((int)this.Width/2 , (int)this.Height/2); //绘制立体效果中心

}protected override void OnPaint(PaintEventArgs pevent)
{
// TODO: 添加 RoundButton.OnPaint 实现
base.OnPaint (pevent);
Graphics g = pevent.Graphics ;
GetButtonRegion();
if(this.isPushed != false)
this.DrawPushedButton(g);
else
this.DrawRoundButton(g);
}///
/// 绘制不同形状的按钮
///

public GraphicsPath DrawButtonFade(int fade, Graphics g, Color center, Color edge)
{
GraphicsPath path = new GraphicsPath();
switch(fade)
{
case 0:
path.AddEllipse(roundRect);//在路径中画椭圆
PathGradientBrush pthGrBrush0 = new PathGradientBrush(path);// 选取画刷
pthGrBrush0.CenterColor = center;
pthGrBrush0.CenterPoint = this.buttonCenter;
Color[] colors0 = {edge};
pthGrBrush0.SurroundColors = colors0;
g.FillEllipse(pthGrBrush0,roundRect);
break;
case 1:
path.AddRectangle(roundRect);//在路径中画矩形
PathGradientBrush pthGrBrush1 = new PathGradientBrush(path);// 选取画刷
pthGrBrush1.CenterColor = center;
pthGrBrush1.CenterPoint = this.buttonCenter;
Color[] colors1 = {edge};
pthGrBrush1.SurroundColors = colors1;
g.FillRectangle(pthGrBrush1,roundRect);
break;
case 2:
Point[] TriagleArray =
{
new Point(this.Width/2 , 0),
new Point(0,this.Height),
new Point(this.Width ,this.Height),
new Point(this.Width/2 , 0)
};
path.AddLines(TriagleArray);//在路径中画三角形
PathGradientBrush pthGrBrush2 = new PathGradientBrush(path);// 选取画刷
pthGrBrush2.CenterColor = center;
pthGrBrush2.CenterPoint = this.buttonCenter;
Color[] colors2 = {edge};
pthGrBrush2.SurroundColors = colors2;
g.FillRectangle(pthGrBrush2,roundRect);
break; default:
path.AddEllipse(roundRect);//默认:在路径中画椭圆
PathGradientBrush pthGrBrushDe = new PathGradientBrush(path);// 选取画刷
pthGrBrushDe.CenterColor = center;
pthGrBrushDe.CenterPoint = this.buttonCenter;
Color[] colorsDe = {edge};
pthGrBrushDe.SurroundColors = colorsDe;
g.FillEllipse(pthGrBrushDe,roundRect);
break; }
return path;

}

回答2:

一般这种情况,都是用图片代替按钮的~~都有onclick事件啊~~

回答3:

贴一个图片