Asked By Robert
06-Jan-10 12:27 AM

I am trying to draw my own border on a usercontrol by subclassing the
WM_NCPAINT message. Everything works fine for a while but eventually
the window stops drawing properly (goes black / bits drawn in wrong
place) which I assume is due to a resource leak.
I think I have narrowed it down to the CreateRectRgn and CombineRgn
calls but I cannot work out why it is wrong. I assumed DefWindowProc
would clean up any regions it found in wParam.
Any suggestions would be greatly appreciated.
Regards
Robert
--- UserControl Code ----
Friend Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case uMsg
Case WM_NCPAINT
If (m_bThemeActive) Then
wParam = ThemeBorder(wParam)
End If
WndProc = DefWindowProc(hWnd, WM_NCPAINT, wParam, 0)
Case Else
WndProc = CallWindowProc(m_SubClass.wndprocNext, _
hWnd, uMsg, wParam, ByVal lParam)
End Select
End Function
Private Function ThemeBorder(ByVal hrgnUpdate As Long) As Long
Dim hdc As Long
Dim rc As RECT
Dim rcWindow As RECT
Dim hrgnClip As Long
hdc = GetWindowDC(hWnd)
GetWindowRect hWnd, rcWindow
GetClientRect hWnd, rc
ClientToScreenAny hWnd, rc.Left
ClientToScreenAny hWnd, rc.Right
rc.Right = rcWindow.Right - (rc.Left - rcWindow.Left)
rc.bottom = rcWindow.bottom - (rc.Top - rcWindow.Top)
hrgnClip = CreateRectRgn(rc.Left, rc.Top, rc.Right, rc.bottom)
If (hrgnUpdate <> 1) Then
CombineRgn hrgnClip, hrgnClip, hrgnUpdate, RGN_AND
End If
OffsetRect rc, -rcWindow.Left, -rcWindow.Top
ExcludeClipRect hdc, rc.Left, rc.Top, rc.Right, rc.bottom
OffsetRect rcWindow, -rcWindow.Left, -rcWindow.Top
DrawBorder hdc, rcWindow
ReleaseDC hWnd, hdc
ThemeBorder = hrgnClip
End Function