首页 > 解决方案 > How to change multiple cursors system-wide in Windows API

问题描述

I'm trying to replace multiple system cursors using SetSystemCursor. My first call changes the cursor of OCR_NORMAL but the subsequent calls are not working.

HCURSOR hWaitCur = LoadCursor(NULL, IDC_WAIT);
HCURSOR cursorCopy = CopyCursor(hWaitCur);

SetSystemCursor(cursorCopy, OCR_NORMAL); // This works

// Not working
SetSystemCursor(cursorCopy, OCR_APPSTARTING); 
SetSystemCursor(cursorCopy, OCR_CROSS);
SetSystemCursor(cursorCopy, OCR_HAND);
SetSystemCursor(cursorCopy, OCR_HELP);
SetSystemCursor(cursorCopy, OCR_IBEAM); 
SetSystemCursor(cursorCopy, OCR_NO);
SetSystemCursor(cursorCopy, OCR_WAIT);

What would be the correct way to update multiple system cursors at once?

标签: c++winapi

解决方案


From the docs (emphasize by me):

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.

So you need to copy it before each call:

SetSystemCursor(CopyCursor(hWaitCur), OCR_NORMAL);
SetSystemCursor(CopyCursor(hWaitCur), OCR_APPSTARTING); 
SetSystemCursor(CopyCursor(hWaitCur), OCR_CROSS);
SetSystemCursor(CopyCursor(hWaitCur), OCR_HAND);
SetSystemCursor(CopyCursor(hWaitCur), OCR_HELP);
SetSystemCursor(CopyCursor(hWaitCur), OCR_IBEAM); 
SetSystemCursor(CopyCursor(hWaitCur), OCR_NO);
SetSystemCursor(CopyCursor(hWaitCur), OCR_WAIT);

推荐阅读