永发信息网

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

答案:3  悬赏:70  手机版
解决时间 2021-05-04 17:12
  • 提问者网友:溺爱和你
  • 2021-05-03 17:10
高分请高手,
最佳答案
  • 五星知识达人网友:过活
  • 2021-05-03 17:56
贴一个图片
全部回答
  • 1楼网友:傲气稳了全场
  • 2021-05-03 18:42

一般这种情况,都是用图片代替按钮的~~

都有onclick事件啊~~

  • 2楼网友:第四晚心情
  • 2021-05-03 18:30

给你找了一个。

/// <summary> /// 绘制圆形按钮 /// </summary> 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; }

/// <summary> /// 绘制按下的按钮 /// </summary> 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; }

/// <summary> /// 获取绘制按钮区域 /// </summary> 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); }

/// <summary> /// 绘制不同形状的按钮 /// </summary> 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; }

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯