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);
}
}
优点:
- 没有削减
- 圆的
- 圆形按钮的设计器加载程序错误
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
});