簡體   English   中英

我該如何在C#中修一個按鈕的角?

[英]How can I round the corners of a button in c#?

我需要繞過現有按鈕的角,而不必在Windows窗體應用程序中創建新的角按鈕。 我該怎么辦?我只能從互聯網上找到新創建的按鈕代碼。

挽救較舊的帖子,您可以編寫此簡單功能,以將舍入應用於現有的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不允許抗鋸齒,因此在圓角​​邊緣看起來有些粗糙。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM