Change cursor in win32 window

Funthing gamer 20 Reputation points
2023-06-27T13:55:38.58+00:00

How do I change my cursor from arrow to hand when hovering over a button?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,654 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,762 questions
{count} votes

Accepted answer
  1. RLWA32 45,701 Reputation points
    2023-06-28T14:41:35.9866667+00:00

    For those that are interested in changing the cursor only when the mouse is hovering (i.e., not moving) over a button -

    MouseHover

    Subclass procedure to use with SetWindowSubclass function -

    LRESULT ButtonProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR uiSubclass, DWORD_PTR dwRefdata)
    {
        static BOOL bTracking = FALSE;
        static HCURSOR handCursor = LoadCursor(NULL, IDC_HAND);
    
        switch (msg)
        {
        case WM_MOUSEMOVE:
            {
                if (!bTracking)
                {
                    TRACKMOUSEEVENT tme{ sizeof tme };
                    tme.dwFlags = TME_HOVER | TME_LEAVE;
                    tme.dwHoverTime = HOVER_DEFAULT;
                    tme.hwndTrack = hwnd;
    
                    if (TrackMouseEvent(&tme))
                        bTracking = TRUE;
                }
            }
            break;
        case WM_MOUSEHOVER:
            {
                SetCursor(handCursor);
                bTracking = FALSE;
                return 0;
            }
            break;
        case WM_MOUSELEAVE:
            {
                bTracking = FALSE;
                return 0;
            }
            break;
        case WM_NCDESTROY:
            {
                RemoveWindowSubclass(hwnd, ButtonProc, uiSubclass);
            }
            break;
        }
    
        return DefSubclassProc(hwnd, msg, wParam, lParam);;
    }
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Castorix31 85,956 Reputation points
    2023-06-27T14:58:51.5733333+00:00

    Just with WM_SETCURSOR

    For example by subclassing a Button hWndButton :

    BOOL bRet = SetWindowSubclass(hWndButton, ButtonSubclassProc, 0, 0);
    

    with :

    LRESULT CALLBACK ButtonSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
    {
    	switch (uMsg)
    	{
    	case WM_SETCURSOR:
    	{
    		SetCursor(LoadCursor(NULL, IDC_HAND));
    		return FALSE;
    	}
    	}
    	return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
    
    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.