Messages not reaching WndProc
-
I am writing a class that overrides Control and utilizes the non-client area. I need to be able to have hotspots within the non-client area. However, because of what seems to me to be unusual behavior, I am not able to get it working. I override WndProc to intercept WM_NCMOUSEMOVE and WM_NCPAINT (as well as WM_NCCALCSIZE, WM_NCHITTEST, and WM_LBUTTONDOWN/UP) as follows: (Please don't flame me for posting VB.NET code. I use both interchangably as they are pretty much the same thing anyways. It just so happens that I wrote this in VB.NET. I posted here because I would guess that C# programmers know more about the Win32 API than VB.NET programmers, and my questions have nothing to do with VB.NET)
Protected Overrides Sub WndProc(ByRef msg As Message) Select Case msg.Msg Case 131 'WM\_NCCALCSIZE ... Case 132 'WM\_NCHITTEST ... Case 133 'WM\_NCPAINT Dim dc As Integer dc = GetDCEx(msg.HWnd.ToInt32(), msg.WParam.ToInt32(), 65536 + 128 + 1) Dim hdc As IntPtr = New IntPtr(dc) Dim g As Graphics = Graphics.FromHdc(hdc) OnPaintNonClient(New PaintEventArgs(g, Rectangle.Round(g.ClipBounds))) ReleaseDC(msg.HWnd.ToInt32(), hdc.ToInt32()) Case 160 'WM\_NCMOUSEMOVE Dim pt As PointL pt.x = msg.LParam.ToInt32() Mod 65536 pt.y = msg.LParam.ToInt32() / 65536 Dim ppt As IntPtr = Marshal.AllocHGlobal(8) Marshal.StructureToPtr(pt, ppt, False) ScreenToClient(msg.HWnd.ToInt32(), ppt.ToInt32()) pt = Marshal.PtrToStructure(ppt, GetType(PointL)) Dim args As MouseEventArgs = New MouseEventArgs(MouseButtons.None, 0, pt.x, pt.y, 0) OnNonClientMouseMove(args) Marshal.FreeHGlobal(ppt) Case 161 'WM\_NCLBUTTONDOWN ... Case 162 'WM\_NCLBUTTONUP ... Case Else MyBase.WndProc(msg) End Select End Sub Protected Overridable Sub OnNonClientMouseMove(ByVal args As MouseEventArgs) If ... Then 'Test hotspots ... PostMessage(Me.Handle, 133, 0, 0) 'WM\_NCPAINT End If End Sub Protected Overridable Sub OnPaintNonClient(ByVal args As PaintEventArgs) ... End Sub
-
I am writing a class that overrides Control and utilizes the non-client area. I need to be able to have hotspots within the non-client area. However, because of what seems to me to be unusual behavior, I am not able to get it working. I override WndProc to intercept WM_NCMOUSEMOVE and WM_NCPAINT (as well as WM_NCCALCSIZE, WM_NCHITTEST, and WM_LBUTTONDOWN/UP) as follows: (Please don't flame me for posting VB.NET code. I use both interchangably as they are pretty much the same thing anyways. It just so happens that I wrote this in VB.NET. I posted here because I would guess that C# programmers know more about the Win32 API than VB.NET programmers, and my questions have nothing to do with VB.NET)
Protected Overrides Sub WndProc(ByRef msg As Message) Select Case msg.Msg Case 131 'WM\_NCCALCSIZE ... Case 132 'WM\_NCHITTEST ... Case 133 'WM\_NCPAINT Dim dc As Integer dc = GetDCEx(msg.HWnd.ToInt32(), msg.WParam.ToInt32(), 65536 + 128 + 1) Dim hdc As IntPtr = New IntPtr(dc) Dim g As Graphics = Graphics.FromHdc(hdc) OnPaintNonClient(New PaintEventArgs(g, Rectangle.Round(g.ClipBounds))) ReleaseDC(msg.HWnd.ToInt32(), hdc.ToInt32()) Case 160 'WM\_NCMOUSEMOVE Dim pt As PointL pt.x = msg.LParam.ToInt32() Mod 65536 pt.y = msg.LParam.ToInt32() / 65536 Dim ppt As IntPtr = Marshal.AllocHGlobal(8) Marshal.StructureToPtr(pt, ppt, False) ScreenToClient(msg.HWnd.ToInt32(), ppt.ToInt32()) pt = Marshal.PtrToStructure(ppt, GetType(PointL)) Dim args As MouseEventArgs = New MouseEventArgs(MouseButtons.None, 0, pt.x, pt.y, 0) OnNonClientMouseMove(args) Marshal.FreeHGlobal(ppt) Case 161 'WM\_NCLBUTTONDOWN ... Case 162 'WM\_NCLBUTTONUP ... Case Else MyBase.WndProc(msg) End Select End Sub Protected Overridable Sub OnNonClientMouseMove(ByVal args As MouseEventArgs) If ... Then 'Test hotspots ... PostMessage(Me.Handle, 133, 0, 0) 'WM\_NCPAINT End If End Sub Protected Overridable Sub OnPaintNonClient(ByVal args As PaintEventArgs) ... End Sub
I tried something else out that gave me strange results as well... Instead of using
PostMessage
, I switched to usingSendMessage
. The WM_NCPAINT message is now reachingWndProc
, however, the strange thing is that the drawing that I do there does not show up. The drawing code works fine in response to WM_NCPAINT messages caused by resizes, min/maxes, etc... Any ideas?