Sign in to follow this  

Changing windows cursor

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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)

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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);


Share this post


Link to post
Share on other sites

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this