Reputation: 20464
PROBLEM
I'm trying to draw a border on a sub-classed ListBox's surface, I get pretty results:
But if I scroll the control, this is what happens:
QUESTION
In C# or else VB.Net, how I could properly draw a border over the control edges like picture one?
(to clarify: not a border on each item)
CODE
This is what I was doing:
public class mylistbox : inherits listbox
Public Sub New()
MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
End Sub
Private Sub DrawBorder(ByVal g As Graphics)
ControlPaint.DrawBorder(g, Me.ClientRectangle,
BorderColor, ButtonBorderStyle)
End Sub
Private Sub MyBase_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) _
Handles MyBase.DrawItem
Me.ColorizeItems(e)
End Sub
Private Sub ColorizeItems(ByVal e As DrawItemEventArgs)
' non important code here...
Me.DrawBorder(Graphics.FromHwnd(Me.Handle))
End Sub
end class
Upvotes: 0
Views: 1646
Reputation: 7204
Draw on WM_NCPAINT (133):
//
<DllImport("User32.dll")>_
Public Shared Function GetWindowDC(ByVal hWnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Dim HDC As IntPtr
If m.Msg = 133 Then
HDC = GetWindowDC(m.HWnd)
If HDC <> IntPtr.Zero Then
MyBase.WndProc(m) //call it to draw what it needs
Using g As Graphics = Graphics.FromHdc(HDC)
g.DrawRectangle(Pens.Blue, 0, 0, Me.Width - 1, Me.Height - 1)
End Using
ReleaseDC(m.HWnd, HDC)
Return
End If
End If
MyBase.WndProc(m)
End Sub
EDIT
Digging even more in WM_NCPAINT, you can do it like this also:
//
<DllImport("User32.dll")> _
Private Shared Function GetWindowDC(ByVal hWnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<StructLayout(LayoutKind.Sequential)> _
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
<DllImport("gdi32.dll")> _
Private Shared Function CreateRectRgnIndirect(ByRef lpRect As RECT) As IntPtr
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Dim HDC As IntPtr
Dim rgn As IntPtr
Dim rt As RECT
Dim pnt As Point
If m.Msg = 133 Then
HDC = GetWindowDC(m.HWnd)
pnt = Me.PointToScreen(New Point(0, 0))
rt.Left = pnt.X
rt.Top = pnt.Y
rt.Right = rt.Left + Me.Width - 4
rt.Bottom = rt.Top + Me.Height - 4
rgn = CreateRectRgnIndirect(rt)
If HDC <> IntPtr.Zero Then
Using g As Graphics = Graphics.FromHdc(HDC)
g.DrawRectangle(Pens.Red, 0, 0, Me.Width - 1, Me.Height - 1)
End Using
m.WParam = rgn
ReleaseDC(m.HWnd, HDC)
End If
End If
MyBase.WndProc(m)
End Sub
We are trying to create a rect that doesn't include the border but includes the vertical scrollbar. From this rect create a region and pass it to wParam.
Upvotes: 1