由于在绘制小圆角矩形时在GDI +中使用DrawArc函数不是很准确,所以我使用的是RoundRect。
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim hDC As IntPtr = e.Graphics.GetHdc
Dim rc As New Rectangle(10, 10, 64, 24)
Dim hPen As IntPtr = Win32.CreatePen(Win32.PenStyle.PS_SOLID, 0, _
ColorTranslator.ToWin32(Color.Green))
Dim hOldPen As IntPtr = Win32.SelectObject(hDC, hPen)
Call Win32.RoundRect(hDC, rc.Left, rc.Top, rc.Right, rc.Bottom, 10, 10)
Win32.SelectObject(hDC, hOldPen)
Win32.DeleteObject(hPen)
e.Graphics.ReleaseHdc(hDC)
MyBase.OnPaint(e)
End Sub
这将绘制一个漂亮的圆角矩形,但它也会用白色画笔填充它,删除我不想删除的内容。
如何在不删除矩形内部的情况下绘制它?
答案 0 :(得分:6)
您只需在绘制矩形之前选择一个库存,空心画笔。使用HOLLOW_BRUSH调用GetStockObject,然后以与选择笔相同的方式将其选择到设备上下文中。
答案 1 :(得分:1)
我使用这样的方法。对我来说很好。
private static GraphicsPath CreateRoundRectranglePath(Rectangle rect, Size rounding)
{
var path = new GraphicsPath();
var l = rect.Left;
var t = rect.Top;
var w = rect.Width;
var h = rect.Height;
var rx = rounding.Width;
var dx = rounding.Width << 1;
var ry = rounding.Height;
var dy = rounding.Height << 1;
path.AddArc(l, t, dx, dy, 180, 90); // topleft
path.AddLine(l + rx, t, l + w - rx, t); // top
path.AddArc(l + w - dx, t, dx, dy, 270, 90); // topright
path.AddLine(l + w, t + ry, l + w, t + h - ry); // right
path.AddArc(l + w - dx, t + h - dy, dx, dy, 0, 90); // bottomright
path.AddLine(l + w - rx, t + h, l + rx, t + h); // bottom
path.AddArc(l, t + h - dy, dx, dy, 90, 90); // bottomleft
path.AddLine(l, t + h - ry, l, t + ry); // left
path.CloseFigure();
return path;
}