我该如何在C#中修一个按钮的角?

时间:2018-06-20 07:39:06

标签: c# button

我需要在不使用Windows窗体应用程序中创建新的圆角按钮的情况下对现有按钮的圆角进行处理。我该怎么办?我只能从互联网上找到新创建的按钮代码。

1 个答案:

答案 0 :(得分:0)

要保存较旧的帖子,您可以编写此简单函数以将舍入应用于现有的Button或其他控件。.:

void makeRound(Control ctl, int radius)
{
    ctl.Paint += (s, e) =>
    {
        RectangleF Rect = new RectangleF(0, 0, ctl.Width, ctl.Height);
        GraphicsPath GraphPath = GetRoundPath(Rect, 20);

        ctl.Region = new Region(GraphPath);
        using (Pen pen = new Pen(Color.CadetBlue, 1.75f))
        {
            pen.Alignment = PenAlignment.Inset;
            e.Graphics.DrawPath(pen, GraphPath);
        }
    };
}

GraphicsPath GetRoundPath(RectangleF Rect, int radius)
{
    float r2 = radius / 2f;
    GraphicsPath GraphPath = new GraphicsPath();

    GraphPath.AddArc(Rect.X, Rect.Y, radius, radius, 180, 90);
    GraphPath.AddLine(Rect.X + r2, Rect.Y, Rect.Width - r2, Rect.Y);
    GraphPath.AddArc(Rect.X + Rect.Width - radius, Rect.Y, radius, radius, 270, 90);
    GraphPath.AddLine(Rect.Width, Rect.Y + r2, Rect.Width, Rect.Height - r2);
    GraphPath.AddArc(Rect.X + Rect.Width - radius, 
                    Rect.Y + Rect.Height - radius, radius, radius, 0, 90);
    GraphPath.AddLine(Rect.Width - r2, Rect.Height, Rect.X + r2, Rect.Height);
    GraphPath.AddArc(Rect.X, Rect.Y + Rect.Height - radius, radius, radius, 90, 90);
    GraphPath.AddLine(Rect.X, Rect.Height - r2, Rect.X, Rect.Y + r2);

    GraphPath.CloseFigure();
    return GraphPath;
}

Region不允许抗锯齿,因此在圆角​​边缘看起来有点粗糙。