3

I want hide cursor inside window client area without borders and title bar (it is simple opengl application). So, function

    ShowCursor(FALSE);

is not suitable. After some searching the winapi i find this solution:

    //when create window class for application window
    WNDCLASSEX WndClass;
    //...
    BYTE CursorMaskAND[] = { 0xFF };
    BYTE CursorMaskXOR[] = { 0x00 };
    WndClass.hCursor = CreateCursor(NULL, 0,0,1,1, CursorMaskAND, CursorMaskXOR);

Is this a good way to solve this typical task? What way is the best?

4

3 回答 3

6

MSDN says that you can set the WNDCLASSEX hCursor field to NULL, in which case you must explicitly set the cursor in your window procedure (which means handling the WM_SETCURSOR message). For example:

if (Msg == WM_SETCURSOR && LOWORD(lParam) == HTCLIENT)
{
    SetCursor(NULL);

    return TRUE;
}

// Remainder of window procedure code

Checking for HTCLIENT ensures that the cursor is only hidden in the client area, and that the window frame and caption will use the correct cursors.

于 2013-01-03T06:43:20.630 回答
0

The SetCursor() call you're using doesn't take a BOOL - it takes an HCURSOR. So you're calling SetCursor( NULL ) which means "hide that cursor". What I found in the old days on Windows is that this is video driver dependent and many drivers don't respect it. The most consistent way to handle this is to make a transparent cursor resource in your app, and return a handle to that cursor in the WM_SETCURSOR message from your main window.

于 2013-01-03T06:40:14.680 回答
0

I found that first setting hCursor to NULL:

    wc.hCursor = NULL;

and then setting the cursor to NULL:

    SetCursor(NULL);

will make it disappear.

From MSDN, I read that the application will set its own cursor by default if one is not defined in hCursor. That's what the first line of code is doing.

Then, after the application sets its own cursor, I mess with it with the second line of code. Or at least, I think that's what happens.

于 2021-03-13T06:44:40.113 回答