Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cursor style doesn't stay updated

I have a normal Windows GUI application (made using the API, not MFC) and as I move my mouse on and off the application and the mouse changes styles (like when you move it over the border, it changes to a resize arrow, etc.) but sometimes it "sticks" in that style, so that I can move the mouse around and it will stay in a resize arrow or whatever, even after it's off the window border. It fixes itself if I move it over another control.

It's just an inconvenience, but it looks unprofessional and I would like to fix it. How can I make it where it stays up to date all the time?

like image 742
John Zane Avatar asked Dec 21 '10 20:12

John Zane


2 Answers

Set a valid cursor handle when you register your window class. See WNDCLASSEX::hCursor. Use LoadCursor to load a valid cursor. Like,

WNDCLASSEX wc = {0};
...
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
...
RegisterClassEx(&wc);
like image 140
tenfour Avatar answered Oct 10 '22 03:10

tenfour


tenfour's answer is correct. Here's a little more background.

When the mouse moves within a window, and it's not captured, the window will get a WM_SETCURSOR message. The message name can be a little confusing. It's basically the window's opportunity to set the cursor, not an instruction to set the cursor.

A window can handle this message by calling SetCursor and returning.

A window can also punt by passing the message to DefWindowProc to get the default behavior. The default behavior is to look at the hCursor field in the WNDCLASS for the window. This is why tenfour's answer works.

(It's actually a bit more complicated than that, since the DefWindowProc first gives the parent window a chance to intervene.)

If you want to do something dynamic, like choose a cursor depending on some state variable, then you should have to handle the WM_SETCURSOR so that it calls SetCursor with whatever cursor is appropriate and then returns TRUE.

See SetCursor for details.

like image 25
Adrian McCarthy Avatar answered Oct 10 '22 05:10

Adrian McCarthy