Search code examples
c++windowscursordirectxhide

hide mouse cursor over specific client area in windows


I have directx embedded into a child window of my application and would like to hide the windows cursor only when it's over that client area. I know how to hide the cursor in general and did manage to find a make-shift example if only showing the cursor while it's not over any client areas, but it wasn't helpful for this. How can I hide the cursor only while it's over a specific client area (/child window)?

edit: this is as close as I've gotten but the cursor flickers unpredictably (as the mouse moves) while over the dx area

case WM_SETCURSOR:
{
    static bool bCursorVisible = TRUE;

    if( hWnd!=hwD3DArea && !bCursorVisible )
    {
        ShowCursor((bCursorVisible=TRUE));
    }
    else if( hWnd==hwD3DArea && bCursorVisible )
    {
        ShowCursor((bCursorVisible=FALSE));
        return TRUE;
    }
}
break;

edit2: AHAH! you have to use wParam instead of hWnd in this message Here's the working code:

case WM_SETCURSOR:
{
    static bool bCursorVisible = TRUE;

    if( ((HWND)wParam)!=hwD3DArea && !bCursorVisible )
    {
        ShowCursor((bCursorVisible=TRUE));
    }
    else if( ((HWND)wParam)==hwD3DArea && bCursorVisible )
    {
        ShowCursor((bCursorVisible=FALSE));
        return TRUE;
    }
}
break;

Solution

  • the fix:

    case WM_SETCURSOR:
            {
                static bool bCursorVisible = TRUE;
                if( ((HWND)wParam)!=hwD3DArea && !bCursorVisible )
                {
                    ShowCursor((bCursorVisible=TRUE));
                }
                else if( ((HWND)wParam)==hwD3DArea && bCursorVisible )
                {
                    ShowCursor((bCursorVisible=FALSE));
                    return TRUE;
                }
            }
            break;
    

    I was on the right track but was using hWnd when I should have been using wParam (which holds the REAL handle of the window the cursor is in)