C# 圆形按钮

C# 圆形按钮,c#,winforms,C#,Winforms,如何将按钮做成圆形而不是传统的矩形 我正在使用winforms(2.0)我需要基于默认的Windows窗体按钮实现您自己的按钮类。 类似于或。您可以使用google查找更多示例。Code project有许多关于这类事情的文章,特别是这篇文章可能会引起兴趣,因为它显示您必须执行不同类型的圆形按钮。首先创建一个类。给它命名:“圆形按钮”。 然后直接编写代码,如下所示: using System; using System.Collections.Generic; using System.Draw

如何将按钮做成圆形而不是传统的矩形


我正在使用winforms(2.0)

我需要基于默认的Windows窗体按钮实现您自己的按钮类。
类似于或。您可以使用google查找更多示例。

Code project有许多关于这类事情的文章,特别是这篇文章可能会引起兴趣,因为它显示您必须执行不同类型的圆形按钮。

首先创建一个类。给它命名:“圆形按钮”。 然后直接编写代码,如下所示:

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);
        }
    }

}
然后,构建应用程序并关闭它

现在转到工具箱,您将看到一个名为RoundButton的控件

然后将其拖放到Windows窗体上并进行测试。

这就是您想要的

GraphicsPath p = new GraphicsPath();
p.AddEllipse(1, 1, button1.Width - 4, button1.Height - 4);
button1.Region = new Region(p);
public class RoundButton : Control
{
    private readonly Label lbl;
    public RoundButton() : base()
    {
        lbl = new Label
        {
            Text = Text,
            ForeColor = ForeColor,
            BackColor = BackColor,
            Font = Font
        };
        CenterInParent();
    }
    private void CenterInParent()
    {
        lbl.Left = (Width - lbl.Width) / 2;
        lbl.Top = (Height - lbl.Height) / 2;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        GraphicsPath grPath = new GraphicsPath();
        grPath.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
        Region = new Region(grPath);
        base.OnPaint(e);
    }
    protected override void OnMove(EventArgs e)
    {
        CenterInParent();
        base.OnMove(e);
    }
    protected override void OnTextChanged(EventArgs e)
    {
        lbl.Text = Text;
        base.OnTextChanged(e);
    }
    protected override void OnForeColorChanged(EventArgs e)
    {
        lbl.ForeColor = ForeColor;
        base.OnForeColorChanged(e);
    }
    protected override void OnBackColorChanged(EventArgs e)
    {
        lbl.BackColor = BackColor;
        base.OnBackColorChanged(e);
    }
    protected override void OnFontChanged(EventArgs e)
    {
        lbl.Font = Font;
        base.OnFontChanged(e);
    }
}
优点:

  • 没有削减

  • 圆的

缺点:

  • 圆形按钮的设计器加载程序错误

你说的是ASP.NET还是WinForms。如果是后者,我不知道。如果是前者,这是一个CSS问题,谷歌“CSS圆角”我使用了这段代码,但我的按钮在左侧和底部都有切口,有人能帮忙吗?@AyushBhargava:原因是设置区域属性只需要普通按钮(包括其不同颜色的边界)切断给定路径之外的所有东西,留下一些旧边界的残余。您必须自己重新绘制形状的边界(即,除了设置区域外,还必须绘制一个比区域小一点的空心椭圆)。请参阅:一个简单的解决方案是在FlatAppearance属性下将RoundButton的FlatStyle属性设置为Flat,将BorderSize设置为0。那就没有切口了我想这是最好的解决办法。究竟为什么这个解决方案没有多少投票权?
public class OptionsMenu : Button
{
    public OptionsMenu(int NoOfOptions, Point Location, ControlCollection controls,
                       Size ButtonSize, int DistanceBetweenOptions)
    {
        Button[] buttons = new Button[NoOfOptions];

        for (int i = 0; i < NoOfOptions; i++)
        {
            buttons[i] = new Button()
            {
                Size = ButtonSize,
            };

            GraphicsPath p = new GraphicsPath();
            p.AddEllipse(1, 1, buttons[i].Width - 4, buttons[i].Height - 4);
            buttons[i].Region = new Region(p);
            buttons[i].Location = new Point(Location.X, Location.Y + DistanceBetweenOptions * i);

            controls.Add(buttons[i]);
        }
    }
}
OptionsMenu menu = new OptionsMenu(4, new Point(50, 50), Controls, new Size(20, 20), 40);

Controls.AddRange(new Control[] 
{
    menu
});