winforms 向Windows窗体添加圆形按钮

a64a0gku  于 2023-10-23  发布在  Windows
关注(0)|答案(2)|浏览(194)

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

11dmarpk1#

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

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

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

jgwigjjp

jgwigjjp2#

BackColor将根据提供的SizeLocation更改用户控件矩形的颜色。正如Evan所说,你需要使用FillEllipse来创建一个填充的圆-DrawEllipse只绘制圆的轮廓。
这里是一个完整的解决方案,以“圆形按钮”的问题,结合OP的形式与埃文的答案和悬停事件的处理。

public class MainForm
{
    public MainForm()
    {
        InitializeComponent();
        ClickableCircle circle = new ClickableCircle(center: new Point(150, 150), radius: 50);
        circle.Click += circle_Click;
        Controls.Add(circle);
    }

    private void circle_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Clicked the circle!");
    }
}

class ClickableCircle : UserControl
{
    public readonly Point Center;
    public readonly int Radius;

    public ShapeColoring DefaultColoring { get; set; } = new ShapeColoring()
    {
        Outline = Color.Black,
        Fill = Brushes.Black,
    };

    public ShapeColoring HoverColoring { get; set; } = new ShapeColoring()
    {
        Outline = Color.DarkGray,
        Fill = Brushes.DarkGray,
    };

    private ShapeColoring _coloring = new ShapeColoring();

    public ClickableCircle(Point center, int radius)
    {
        Radius = radius;
        Center = center;
        Location = new Point(x: center.X - radius, y: center.Y - radius);
        Size = new Size(radius * 2, radius * 2);
        _coloring = DefaultColoring;
        MouseEnter += ClickableCircle_MouseEnter;
        MouseLeave += ClickableCircle_MouseLeave;
    }

    private void ClickableCircle_MouseEnter(object sender, EventArgs e)
    {
        Cursor = Cursors.Hand;
        ChangeColoring(HoverColoring);
    }

    private void ClickableCircle_MouseLeave(object sender, EventArgs e)
    {
        Cursor = Cursors.Default;
        ChangeColoring(DefaultColoring);
    }

    private void ChangeColoring(ShapeColoring newColoring)
    {
        _coloring = newColoring;
        Refresh();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics graphics = e.Graphics;
        Pen pen = new Pen(_coloring.Outline);
        try
        {
            Rectangle ellipse = new Rectangle(
                x: 0, // relative to the top-left of this user control, not the whole form
                y: 0,
                // adjust width and height so the outline doesn't get cut off
                width: Size.Width - 1,
                height: Size.Height - 1
            );
            graphics.FillEllipse(_coloring.Fill, ellipse);
            graphics.DrawEllipse(pen, ellipse);
        }
        finally { pen.Dispose(); }
    }
}

class ShapeColoring
{
    public Color Outline { get; set; } = Color.White;
    public Brush Fill { get; set; } = Brushes.White;
}

将鼠标悬停在圆上后,光标变为手形,圆的颜色也随之改变:

备注

有很多细微的差别,我不得不实验发现:

  • 位置和大小可以在circle类构造函数中设置。
  • OnPaint方法中椭圆的x/y位置是相对于圆的,而不是相对于窗口的。
  • 如果你想改变颜色,你需要调用Refresh()来立即重绘-参见this SO answer
  • 椭圆的widthheight需要比完整尺寸小1个像素,以防止轮廓被切断。

相关问题