WinAPI SetSystemCursor and LoadCursorFrom - how to set default cursor?

痴心易碎 提交于 2019-12-11 16:50:32

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/27112845/winapi-setsystemcursor-and-loadcursorfrom-how-to-set-default-cursor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!