Lar*_*ech 8 vb.net rounded-corners winforms
我发现使用GDI +绘制圆角矩形的每个示例代码都是这样的(从BobPowell.net中解除并略微修改):
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Panel1.Paint
e.Graphics.Clear(SystemColors.Window)
e.Graphics.SmoothingMode = SmoothingMode.None
Call DrawRoundRect(e.Graphics, Pens.Red, 10, 10, 48, 24, 6)
End Sub
Public Sub DrawRoundRect(ByVal g As Graphics, ByVal p As Pen, ByVal x As Single, ByVal y As Single, ByVal width As Single, ByVal height As Single, ByVal radius As Single)
Using gp As New GraphicsPath()
gp.StartFigure()
gp.AddArc(x + width - radius, y, radius * 2, radius * 2, 270, 90)
gp.AddArc(x + width - radius, y + height - radius, radius * 2, radius * 2, 0, 90)
gp.AddArc(x, y + height - radius, radius * 2, radius * 2, 90, 90)
gp.AddArc(x, y, radius * 2, radius * 2, 180, 90)
gp.CloseFigure()
g.DrawPath(p, gp)
End Using
End Sub
Run Code Online (Sandbox Code Playgroud)
这会产生一个圆角矩形,只有左上角是准确的.
AntiAliasing必须关闭,因为它通过远程桌面连接,我不能依赖它可用.此外,我正在寻找一个清晰的圆角矩形.

我已经尝试调整其他角落的大小并更改笔对齐,但似乎没有任何东西可以产生简单,准确的圆角矩形.
有没有办法在旧的winforms中绘制比这更好的圆角矩形?
我发现最好的解决方案就是老式的 Windows API:
Private Sub DrawRoundRect(ByVal g As Graphics, ByVal r As Rectangle)
Dim hDC As IntPtr = g.GetHdc
Dim hPen As IntPtr = CreatePen(PS_SOLID, 0, ColorTranslator.ToWin32(Color.Red))
Dim hOldPen As IntPtr = SelectObject(hDC, hPen)
SelectObject(hDC, GetStockObject(NULL_BRUSH))
RoundRect(hDC, r.Left, r.Top, r.Right - 1, r.Bottom - 1, 12, 12)
SelectObject(hDC, hOldPen)
DeleteObject(hPen)
g.ReleaseHdc(hDC)
End Sub
Run Code Online (Sandbox Code Playgroud)
这会产生我一直在寻找的对称圆角矩形:

| 归档时间: |
|
| 查看次数: |
3990 次 |
| 最近记录: |