WinAPI SetSystemCursor and LoadCursorFrom - how to set default cursor?
Asked Answered
S

2

2

I set my own cursor by:

HCURSOR hCurStandard =  LoadCursorFromFile(TEXT("cursor.cur"));
SetSystemCursor(hCurStandard, 32512);
DestroyCursor(hCurStandard);

How to go back and set default cursor?

This doesnt work:

SetSystemCursor(LoadCursor(0, IDC_ARROW), 32512);

----EDIT-----

HCURSOR hcursor = LoadCursor(0, IDC_ARROW);
HCURSOR hcursor_copy = CopyCursor(hcursor);
BOOL ret = SetSystemCursor(hcursor_copy, OCR_NORMAL);
DestroyCursor(hcursor);

This works for all cursors except IDC_ARROW, what the...?

Stound answered 24/11, 2014 at 19:41 Comment(4)
Did you check the return value of SetSystemCursor? If it's 0, call GetLastError to see what the error is.Unpack
SetSytemCursor returns 1 but nothing changesStound
ok. The docs say you shouldn't use LoadCursor for calls to SetSystemCursor See here: msdn.microsoft.com/en-us/library/windows/desktop/… So the answer below is probably what you need to do.Unpack
You right, I edited my post, but still have some weird problem.Stound
H
7

The problem is that you probably use SetSystemCursor function to change the standard arrow cursor. This function actually overwrites the system cursor with HCURSOR you provided, so when you call LoadCursor with IDC_ARROW it loads your custom cursor. That explains the weird behaviour of your program. To avoid that, you should save the default system cursor before you change it.

HCURSOR def_arrow_cur = CopyCursor(LoadCursor(0, IDC_ARROW));
//now you have a copy of the original cursor
SetSystemCursor(LoadCursorFromFile("my_awesome_cursor.cur"),OCR_NORMAL);
...
SetSystemCursor(def_arrow_cur,OCR_NORMAL);//restore the good old arrow

I know it's a late answer, but I hope somebody will find this useful.

Hotchpotch answered 16/7, 2015 at 18:38 Comment(0)
U
1

According to the SetSystemCursor docs:

To specify a cursor loaded from a resource, copy the cursor using the CopyCursor function, then pass the copy to SetSystemCursor.

So doing this might fix your original problem:

HCURSOR hCurDef = CopyCursor(LoadCursor(0, IDC_ARROW));
SetSystemCursor(hCurDef, OCR_NORMAL);
DestroyCursor(hCurDef);

If that doesn't work, you can store the filename of the existing cursor, which you can obtain by reading the registry (HKEY_CURRENT_USER\Control Panel\Cursors\Arrow), or as a shortcut use GetProfileString:

TCHAR chCursorFile[MAX_PATH];
GetProfileString(TEXT("Cursors"), TEXT("Arrow"), TEXT(""), chCursorFile, MAX_PATH);

To restore the cursor, load the previous one in using LoadCursorFromFile and set it with SetSystemCursor.

Note that calling SetSystemCursor doesn't update the registry, so your custom cursor wouldn't survive a reboot.

Urbai answered 24/11, 2014 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.