WinApi, hide cursor inside window client area

2019-08-15 02:52发布

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?

标签: winapi
2条回答
【Aperson】
2楼-- · 2019-08-15 03:34

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.

查看更多
等我变得足够好
3楼-- · 2019-08-15 03:49

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.

查看更多
登录 后发表回答