我在 Windows 窗体中创建了一个圆形按钮。按钮没问题。唯一的问题是我希望它的颜色与背景不同,所以我将 BackColor 设置为 GoldenRod。然而,它只是在圆形按钮周围创建了一个“goldenRod”立方体......我如何制作它以便只有按钮是彩色的?
public MainForm(){
InitializeComponent();
myButtonObject start = new myButtonObject();
EventHandler myHandler = new EventHandler(start_Click);
start.Click += myHandler;
start.Location = new System.Drawing.Point(5, 5);
start.Size = new System.Drawing.Size(101, 101);
start.BackColor=System.Drawing.Color.Goldenrod;
this.Controls.Add(start);
`}
void start_Click(Object sender, System.EventArgs e)
{
MessageBox.Show("Start");
}
public class myButtonObject : UserControl
{
// Draw the new button.
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
Pen myPen = new Pen(Color.Black);
// Draw the button in the form of a circle
graphics.DrawEllipse(myPen, 0, 0, 100, 100);
myPen.Dispose();
}
}
最佳答案
您需要填充在 OnPaint()
方法期间绘制的椭圆
。
graphics.FillEllipse(Brushes.Goldenrod, new Rectangle(0,0,100,100));
graphics.DrawEllipse(myPen, 0, 0, 100, 100);
然后确保从 MainForm()
构造函数中删除 start.BackColor
属性。
关于c# - 向 Windows 窗体添加圆形按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23256076/