How can I draw a hollow rectangle using CreatePen?

Posted by LarsTech on Stack Overflow See other posts from Stack Overflow or by LarsTech
Published on 2011-06-25T00:38:57Z Indexed on 2011/06/25 8:22 UTC
Read the original article Hit count: 187

Filed under:
|
|
|

Since using the DrawArc function in GDI+ isn't very accurate when drawing a small rounded rectangle, I am using RoundRect instead.

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

This will draw a nice rounded rectangle, but it will also fill it with a white brush, erasing what I don't want to have erased.

How can I draw this without erasing the inside of the rectangle?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about winapi