[RESOLVED] Graphics wont stick
I want to put an outline around each control on a panel, as a test case I used some checkboxes. It draws the outlines fine, but if I mouseover the checkboxes, they dont repaint. I cant see anything wrong with this (or I might have code blindness). Can anyone see what's wrong?
VB Code:
Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Integer
Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Integer) As Integer
Dim MyPanel As New Panel
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d As IntPtr = New IntPtr(GetWindowDC(MyPanel.Handle.ToInt32))
Dim g As Graphics = Graphics.FromHdc(d)
PaintCon(MyPanel, g)
g.Dispose()
End Sub
Private Sub PaintCon(ByVal Con As Control, ByVal g As Graphics)
For Each ctl As Control In Con.Controls
If ctl.Controls.Count > 0 Then
PaintCon(ctl, g)
Else
If ctl.GetType.ToString.ToLower = "system.windows.forms.checkbox" Then
Dim ConRect As New Rectangle(MyPanel.PointToClient(ctl.Parent.PointToScreen(ctl.Location)), ctl.Size)
g.DrawRectangle(Pens.Black, ConRect)
RemoveHandler Con.Paint, AddressOf ConPaint
AddHandler Con.Paint, AddressOf ConPaint
End If
End If
Next
End Sub
Private Sub ConPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
Dim con As Control = DirectCast(sender, Control)
Dim ConRect As New Rectangle(MyPanel.PointToClient(con.Parent.PointToScreen(con.Location)), con.Size)
e.Graphics.DrawRectangle(Pens.Black, ConRect)
End Sub