简体   繁体   中英

WinAPI SetSystemCursor and LoadCursorFrom - how to set default cursor?

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...?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM