问题
In my media player application, I hid the cursor using SetCursor(NULL)
and to make sure that Windows does not reset the cursor state, I handled WM_SETCURSOR
in my WndProc
method.
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM.SETCURSOR:
base.WndProc(ref m);
int lowWord = (m.LParam.ToInt32() << 16) >> 16;
if (lowWord == HTCLIENT && FullScreen)
{
SetCursor(IntPtr.Zero); // hides cursor
m.Result = (IntPtr)1; // return TRUE; equivalent in C++
}
return;
}
}
However when the cursor is in the client area (aka LOWORD(lParam) == HTCLIENT
), WM_SETCURSOR
is never triggered in WndProc
. So I never actually get the WM_SETCURSOR
message when the cursor is in the client area and only get it when LOWORD(lParam) != HTCLIENT
.
However in Spy++, it clearly shows that the application received the WM_SETCURSOR
and WM_MOUSEMOVE
messages.
Where is the message getting lost/handled? What do I have to do in order to receive the WM_SETCURSOR
message in C#?
回答1:
My application has several panels covering the application. So another user pointed out to me kindly that since every control has its own WndProc
, the WM_SETCURSOR
method wasn't being passed on to the form underneath it. In order to receive those messages, I would have to override each of those panels with its own WndProc
method.
However the above code does work if there are no controls covering the form where the cursor is.
来源:https://stackoverflow.com/questions/18279938/how-to-handle-wm-setcursor-in-c-sharp