Question

In my 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.

However in the msdn documentation for C++, in order to handle WM_SETCURSOR I have to return TRUE. However in C#'s WndProc, it is a void method so I cannot return any value.

So how would I accomplish that return statement in C#?

C++ Variant:

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
{        
    case WM_SETCURSOR:
        if (LOWORD(lParam) == HTCLIENT)
        {
            SetCursor(hCursor);
            return TRUE;
        }
        break;
}
Was it helpful?

Solution

You can return without calling base.WndProc:

protected override void WndProc(ref Message m){
    if(m.Msg == WM_SETCURSOR) {
        int lowWord = (m.LParam.ToInt32() << 16) >> 16;
        if(lowWord == HTCLIENT){
          SetCursor(hCursor);
          return;
        }
    }
    base.WndProc(ref m);
}

I guess this also works (I've experienced it with some messages but not sure with WM_SETCURSOR):

protected override void WndProc(ref Message m){     
    base.WndProc(ref m);
    if(m.Msg == WM_SETCURSOR) {
        int lowWord = (m.LParam.ToInt32() << 16) >> 16;
        if(lowWord == HTCLIENT){
          SetCursor(hCursor);
          m.Result = new IntPtr(1);
        }           
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top