Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SetCursor reverts after a mouse move

I am using SetCursor to set the system cursor to my own image. The code looks something like this:

// member on some class
HCURSOR _cursor;

// at init time
_cursor = LoadCursorFromFile("somefilename.cur");

// in some function
SetCursor(_cursor);

When I do this the cursor does change, but on the first mouse move message it changes back to the default system arrow cursor. This is the only code in the project that is setting the cursor. What do I need to do to make the cursor stay the way I set it?

like image 356
Joe Ludwig Avatar asked Oct 03 '08 22:10

Joe Ludwig


4 Answers

It seems that I have two options. The first is the one that Mark Ransom suggested here, which is to respond to the windows WM_SETCURSOR message and call SetCursor at that time based on where the mouse is. Normally windows will only send you WM_SETCURSOR when the cursor is over your window, so you would only set the cursor in your window.

The other option is to set the default cursor for the window handle at the same time as I call SetCursor. This changes the cursor set by the default handler to WM_SETCURSOR. That code would look something like this:

// defined somewhere
HWND windowHandle;
HCURSOR cursor;

SetCursor(cursor);
SetClassLong(windowHandle, GCL_HCURSOR, (DWORD)cursor);

If you use the second method you have to call both SetCursor and SetClassLong or your cursor will not update until the next mouse move.

like image 189
Joe Ludwig Avatar answered Nov 12 '22 05:11

Joe Ludwig


You need to make your HCURSOR handle not go out of scope. When the mouse moves, windows messages start flying all over the place, and it will wipe out your handle (in the example above).

Make an HCURSOR a private member of the class, and use that handle when you call LoadCursor...() and SetCursor(). When you are done, do not forget to free it, and clean it up, or you will end up with a resource leak.

like image 27
LarryF Avatar answered Nov 12 '22 05:11

LarryF


You need to respond to the Windows message WM_SETCURSOR.

like image 31
Mark Ransom Avatar answered Nov 12 '22 07:11

Mark Ransom


This behavior is intended to be this way. I think the most simple solution is: When creating your window class (RegisterClass || RegisterClassEx), set the WNDCLASS.hCursor || WNDCLASSEX.hCursor member to NULL.

like image 1
Heinz Traub Avatar answered Nov 12 '22 05:11

Heinz Traub