Trap WM_NCHITTEST message but keep checking for MouseEnter/-Leave

403 views Asked by At

I've got a form where I've removed the title bar but kept the border (see this answer).

In the above answer it's stated that it's required to have the FormBorderStyle set to Sizable or SizableToolWindow, and in order to stop the form from being sizable you'd trap the WM_NCHITTEST event. The only problem being that doing so will make it not raise the normal Form_MouseEnter or Form_MouseLeave events.

Is there any workaround to this?

My code:

Protected Overrides Sub WndProc(ByRef message As Message)
    If message.Msg = &H84 Then 'WM_NCHITTEST
        Me.Focus() 'Focus the form when it receives a click.
        Return
    End If
    MyBase.WndProc(message)
End Sub

Private Sub PanelForm_MouseLeave(sender As Object, e As System.EventArgs) Handles PanelForm.MouseLeave
    PlaceOnScreen(False) 'Placed a breakpoint here, it won't execute.
End Sub
1

There are 1 answers

1
Hans Passant On BEST ANSWER

It is just a bug, the return value of WM_NCHITTEST is right now 0. Which means "the mouse is nowhere". So lots of stuff stops working, like activating and focusing the window and the MouseEnter event. You must return 1 (aka HTCLIENT) instead, means "it is on the client area":

Protected Overrides Sub WndProc(ByRef message As Message)
    If message.Msg = &H84 Then 'WM_NCHITTEST
        message.Result = CType(1, IntPtr)
        Return
    End If
    MyBase.WndProc(message)
End Sub