Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the cursor on a Button?

static HWND btn;
HCURSOR cursor = LoadCursor(0, IDC_CROSS);

case WM_CREATE:
    btn = CreateWindow(TEXT("BUTTON"), TEXT("Press Me"),
                       WS_CHILD|WS_VISIBLE, 50, 50, 80, 30,
                       hwnd, (HMENU) 111, NULL, NULL);

Now, in WM_COMMAND, I try to use:

SendMessage(btn, WM_SETCURSOR, 0, (LPARAM) cursor);

Which isn't working.

So how do I change the cursor of the mouse after it clicks the button? Also, how do I change the cursor of the mouse as it is hovering over the button (like it changes to a hand when hovering over links in web-browsers)?

like image 478
ApprenticeHacker Avatar asked Dec 27 '22 02:12

ApprenticeHacker


1 Answers

Use this code to change the cursor of a single control:

SetClassLong(btn, GCL_HCURSOR, (LONG)cursor);

Preferred method, for 64 bit compatibility, is:

SetClassLongPtr(btn, GCL_HCURSOR, (LONG_PTR)cursor);

Note that this won't change the icon only for the specified btn window but for all windows with the same class, you have to first register a custom class name with RegisterClass() and then use it in the WNDCLASS.lpszClassName structure when creating btn.

Again this will apply to all the Windows with that (custom) class. To change the cursor of a single specific window you need to subclass it, manage the WM_SETCURSOR message and if (for example) lParam is HTCLIENT (pointer entered the window client area) then call SetCursor() to set the cursor you want (don't forget to return TRUE in this case). Of course this might be temporary (calling SetWindowLongPtr() with GWLP_WNDPROC).

like image 190
Adriano Repetti Avatar answered Dec 31 '22 13:12

Adriano Repetti