Changing windows cursor

Started by
9 comments, last by Colin Jeanne 18 years, 11 months ago
I'm trying to set a custom cursor that dosen't change when it leaves my window. I can it to work within my window but whenever the cursor leaves my window it goes back to the default arrow. Any help would be great thanks.

bullseye = LoadCursor(hInstance, MAKEINTRESOURCE(CURSOR_BULLSEYE));
SetCursor(bullseye);

Advertisement
That's by design, SetCursor() is only intended to set the cursor for your application rather than the whole system.

Try using SetSystemCursor() if you want to set the cursor for the whole of Windows.

Bear in mind that you should "play nice" with other applications on the desktop - if your application doesn't have focus, you should probably restore the cursor to default in such cases.

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

thanks, that does the trick but how do I change it back to the arrow. Here's what I have but it the cursor is dosen't change back to the arrow.

arrow = LoadCursor(NULL, IDC_ARROW);
SetSystemCursor(arrow, 32512)
Thanks, that does the trick. But how do I change it back to the arrow. Here's what I have.


arrow = LoadCursor(NULL, IDC_ARROW);
SetSystemCursor(arrow, 32512);
Did you look at the documentation for SetSystemCursor. I think there's a good chance that it returns the current cursor (before changing to the new one) so you can save it in a variable and then re-set it when you're finished.
It returns a BOOl 1 is it suceeds and 0 if it fails. I have it checking for errors and the funcion returns none. Also the custom cursor remains after my app is closed and I can't get it to go back to the arrow with out restarting my computer.
Quote:
From the MSDN
The system destroys hcur by calling the DestroyCursor function. Therefore, hcur cannot be a cursor loaded using the LoadCursor function. To specify a cursor loaded from a resource, copy the cursor using the CopyCursor function, then pass the copy to SetSystemCursor.
I saw tried that but it didn't seem to make a difference. Here's my code.

arrow = LoadCursor(NULL, IDC_ARROW);HCURSOR h = CopyCursor(arrow);if(!SetSystemCursor(h, OCR_NORMAL)){        DWORD a = GetLastError();	char buffer[10];	itoa(a, buffer, 10);	MsgBox(buffer);}
I discovered the problem. You see before you change a cursor using SetSystemCusor() you first load it using LoadCursor() or LoadImage() and then store it in a safe place using CopyCursor().

What is happening is this:
You set OCR_NORMAL to be some other cursor.
Now you try to use LoadCursor(0, IDC_ARROW) to get back the original

IDC_ARROW is #defined as MAKEINTRESOURCE(OCR_NORMAL) so what you are really doing is loading the changed cursor under the impression that it is the original.

The solution:
Before changing the cursor do something like this

HCURSOR arrow = LoadCursor(0, IDC_ARROW);HCURSOR original_arrow = CopyCursor(arrow);


original_arrow will be a variable that will not change during the life of your program. To reset the cursor to the original arrow just call

SetSystemCursor(original_arrow, OCR_NORMAL);
Thanks for the help. Theres still one small problem though. I change the cursor to my custom cursor than back to an arrow and it remains the custom cursor while in my window. When the cursor is in another window it's the arrow but when it's in my window it stays the custom one and I can't get it to change back. I tried a call to SetCursor but that didn't help.

[source[	HCURSOR tarrow = LoadCursor(NULL, IDC_ARROW);	HCURSOR tbullseye = LoadCursor(hinstance, MAKEINTRESOURCE(CURSOR_BULLSEYE));	arrow = CopyCursor(tarrow);	bullseye = CopyCursor(tbullseye);	SetSystemCursor(bullseye, OCR_NORMAL);	SetSystemCursor(arrow, OCR_NORMAL);

This topic is closed to new replies.

Advertisement