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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How do I change my cursor from arrow to hand when hovering over a button?
For those that are interested in changing the cursor only when the mouse is hovering (i.e., not moving) over a button -
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);;
}
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);
}