c# - 向 Windows 窗体添加圆形按钮

标签 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();
    }
}

最佳答案

您需要填充在 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/

相关文章:

c# - 让 ActiveX 控件在没有窗体的情况下工作?

Xamarin 触发器按钮未在 FreshMvvm/SwipeView 上下文中触发

c# - 如何在 Aero/.NET 4 中获取 block 样式进度条

c# - Azure ML Web 服务超时

c# - 是否有必要在控制处理之前明确清除数据绑定(bind)?

c# - 为什么 Bound Textbox 不修改其关联属性,而 Text 值是以编程方式设置的?

c# - 如何从标签中删除数据

c# - 在 CSS 中查找和删除注释的正则表达式

forms - Laravel 表单 : Input Button Not Showing HTML Icon

Javascript Button - onClick 之后不会调用方法