How to change cursor image in Python

后端 未结 1 1426
误落风尘
误落风尘 2021-01-24 00:22

I want to change my cursor image (everywhere on screen) when my program is running.

I try to load image with win32gui and then use win32api to change cursor image, but s

1条回答
  •  不思量自难忘°
    2021-01-24 00:46

    Changing the system cursor is not recommended. For the sake of curiosity, it can be done with SetSystemCursor, example

    ctypes.windll.user32.SetSystemCursor(hcursor, 32512) #OCR_NORMAL
    

    See documentation for OCR_NORMAL and other cursor constants.

    I would not recommend this at all for python, because it is difficult to restore the cursor, so the user will be stuck with a new cursor unless he changes the cursor through the system settings. You can try to save the old cursor and restore it, but this method fails if your program exits abnormally.

    hold = win32gui.LoadImage(0, 32512, win32con.IMAGE_CURSOR, 
                              0, 0, win32con.LR_SHARED )
    hsave = ctypes.windll.user32.CopyImage(hold, win32con.IMAGE_CURSOR, 
                                           0, 0, win32con.LR_COPYFROMRESOURCE)
    hnew = win32gui.LoadImage(0, 'file.cur', 
                                 win32con.IMAGE_CURSOR, 0, 0, win32con.LR_LOADFROMFILE);
    ctypes.windll.user32.SetSystemCursor(hcursor, 32512)
    time.sleep(5)
    
    #restore the old cursor
    ctypes.windll.user32.SetSystemCursor(hsave, 32512)
    

    0 讨论(0)
提交回复
热议问题