按钮C#(WinForms)中的圆角边

这是一个圆形边缘按钮

你好,通过这里和其他网站的一些研究,我做了一个圆角边缘按钮。

protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Rectangle Rect = new Rectangle(0, 0, this.Width, this.Height); GraphicsPath GraphPath = new GraphicsPath(); GraphPath.AddArc(Rect.X, Rect.Y, 50, 50, 180, 90); GraphPath.AddArc(Rect.X + Rect.Width - 50, Rect.Y, 50, 50, 270, 90); GraphPath.AddArc(Rect.X + Rect.Width - 50, Rect.Y + Rect.Height - 50, 50, 50, 0, 90); GraphPath.AddArc(Rect.X, Rect.Y + Rect.Height - 50, 50, 50, 90, 90); this.Region = new Region(GraphPath); } 

我面临的问题是按钮的“蓝色突出显示”:它显示在大部分按钮上,但它没有显示在圆形边缘上,所以我的按钮部分突出显示,部分不突出显示(在边缘上)。 我该怎么做才能解决这个问题? 谢谢。

PS:我不能使用WPF。 该应用程序适用于非常旧的计算机; 所以,请不要建议。 此外,客户没有钱购买新电脑。

这是一个快速的,你可能想要微调和优化一些细节..

 class RoundedButton : Button { 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; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); RectangleF Rect = new RectangleF(0, 0, this.Width, this.Height); GraphicsPath GraphPath = GetRoundPath(Rect, 50); this.Region = new Region(GraphPath); using (Pen pen = new Pen(Color.CadetBlue, 1.75f)) { pen.Alignment = PenAlignment.Inset; e.Graphics.DrawPath(pen, GraphPath); } } } 

显然,由于我们有一个类,我们可以将GraphicsPath缓存在类变量中。 当然你选择颜色..

在此处输入图像描述

您可以使用WebBrowser,使用HTML和CSS创建一个按钮,然后使用webbrowser.DocumentText = "your html";

如果没有自己画画,我认为你无能为力。 基本按钮绘制逻辑不会写为“在窗口区域的任何部分周围显示蓝色突出显示”。 相反,它是用它所期望的区域类型编写的 – 一个矩形区域。 因此,基础涂料总是将矩形图像绘制成裁剪形状。 在WPF中,您可以更轻松地完成此类操作。