gpt4 book ai didi

vb.net - 圆角矩形不准确

转载 作者:行者123 更新时间:2023-12-04 06:41:09 25 4
gpt4 key购买 nike

我曾经找到的使用 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

这会产生一个圆角矩形,其中只有左上角是准确的。

必须关闭 AntiAliasing,因为它正在通过远程桌面连接,我不能依赖它是否可用。此外,我正在寻找一个清晰的圆角矩形。

enter image description here

我尝试调整其他角的大小并更改笔的对齐方式,但似乎没有任何东西可以产生简单、准确的圆角矩形。

有没有办法在旧的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

这会产生我一直在寻找的对称圆角矩形:

enter image description here

关于vb.net - 圆角矩形不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6060525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com