圆形按钮

kar*_*hik 5 c# winforms

如何制作圆形按钮而不是传统的矩形按钮.

我正在使用winforms(2.0)

小智 27

先上课.给它命名圆形按钮.然后直接编写代码:

using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    public class RoundButton : Button
    {
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            GraphicsPath grPath = new GraphicsPath();
            grPath.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
            this.Region = new System.Drawing.Region(grPath);
            base.OnPaint(e);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

然后构建您的应用程序并关闭它.现在转到工具箱,您将看到一个名为RoundButton的控件.然后药物并将其放在您的窗体上并测试它......享受!

  • 一个简单的解决方案是将 RoundButton 的 FlatStyle 属性设置为 Flat,并将 FlatAppearance 属性下的 BorderSize 设置为 0。然后就没有更多的削减了 (3认同)
  • 我使用了这段代码,但是我的按钮在左侧和底部有切口,有人可以帮忙吗? (2认同)

Han*_*son 5

代码项目中有许多有关这类事情的文章,特别是“ RoundButton Windows Control-Every Decreasing Circles”一文可能引起人们的兴趣,因为它表明您必须执行不同种类的圆形按钮。


Jig*_*g12 5

GraphicsPath p = new GraphicsPath();
p.AddEllipse(1, 1, button1.Width - 4, button1.Height - 4);
button1.Region = new Region(p);
Run Code Online (Sandbox Code Playgroud)