如何在 Python 中更改光标图像 [英] How to change cursor image in Python

查看:67
本文介绍了如何在 Python 中更改光标图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的程序运行时更改我的光标图像(屏幕上的任何地方).

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

我尝试使用 win32gui 加载图像,然后使用 win32api 更改光标图像,但出现问题并且我的光标不显示我的光标图像是 map.cur

I try to load image with win32gui and then use win32api to change cursor image, but something is wrong and my cursor doesn't show up My cursor image is map.cur

import win32api
import time
import win32gui
import win32con

x = win32gui.LoadImage(0,'map.cur',win32con.IMAGE_CURSOR,0,0,win32con.LR_LOADFROMFILE)
win32api.SetCursor(x)

time.sleep(5)

推荐答案

不建议更改系统光标.出于好奇,可以用SetSystemCursor来完成,例子

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

请参阅文档OCR_NORMAL 和其他游标常量.

See documentation for OCR_NORMAL and other cursor constants.

对于python,我完全不推荐这样做,因为恢复游标很困难,因此除非用户通过系统设置更改游标,否则用户将卡在新游标上.您可以尝试保存旧游标并恢复它,但如果您的程序异常退出,此方法将失败.

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)
hcursor = 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)

  • hnew重命名为hcursor
  • 这篇关于如何在 Python 中更改光标图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆