Search code examples
c++winapimouse-cursor

How do I set the "Select Precision" cursor in a C++ application?


I need to somehow set the cursor to the "Select Precision" pointer (the horizontal and vertical cross-over) for a C++ application.

Does anyone know how this would be integrated using the WinApi protocol?


Solution

  • Somewhere in initialization code:

    HCURSOR precision_cursor = LoadCursor( NULL, IDC_CROSS );
    

    And window procedure:

    LRESULT CALLBACK YourWindowProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
    {
        switch ( msg )
        {
        case WM_SETCURSOR:
            // If you omit test below, you will change cursor also for scrollbars, frames, etc.
            if ( LOWORD( lparam ) == HTCLIENT )
            {
                SetCursor( precision_cursor );
                return TRUE;
            }
            break;
        }
    
        // This will also handle cursor for scrollbars and frames.
        return DefWindowProc( hwnd, msg, wparam, lparam );
    }