将圆形按钮添加到Windows窗体

时间:2014-04-23 22:03:40

标签: c# winforms button colors

我在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();
    }
}

1 个答案:

答案 0 :(得分:2)

您需要在Ellipse方法中填写您正在绘制的OnPaint()

graphics.FillEllipse(Brushes.Goldenrod, new Rectangle(0,0,100,100));
graphics.DrawEllipse(myPen, 0, 0, 100, 100);

然后确保从start.BackColor构造函数中删除MainForm()属性。