使用Python临时更改光标

发布于 2024-12-12 13:57:49 字数 181 浏览 0 评论 0原文

我正在编写一个脚本,该脚本拦截触摸板输出并在经过一些处理后发送到窗口。所以不涉及 GUI。我想在发生某些光标行为时临时更改光标。我已尽我所能在网上搜索,发现很少有帖子讨论使用 win32api.SetCursor() 但这根本不起作用。大多数帖子讨论使用 Tkinter 或 wxPython 更改光标。还有其他解决方案可以改变系统范围内的光标吗?

I am writing a script that intercepts a touchpad output and send to the windows after some processing. So there is no GUI involved. I want to change the cursor temporarily when certain cursor behavior occurs. I have searched the web to the best of my abilities and found very few posts that talked about using win32api.SetCursor() but this does not work at all. Most of the posts talks about changing cursor using Tkinter or wxPython. Is there any other solution to change the cursor system wide?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼疼热情 2024-12-19 13:57:49

使用光标下方的代码会在系统范围内更改,但我必须恢复到退出程序下方的箭头光标。如果还有其他更好的方法,我将不胜感激您的回复。

from ctypes import *
import win32con

SetSystemCursor = windll.user32.SetSystemCursor #reference to function
SetSystemCursor.restype = c_int #return
SetSystemCursor.argtype = [c_int, c_int] #arguments

LoadCursorFromFile = windll.user32.LoadCursorFromFileA #reference to function
LoadCursorFromFile.restype = c_int #return
LoadCursorFromFile.argtype = c_char_p #arguments

CursorPath = "../cursor/MyCross.cur"

NewCursor = LoadCursorFromFile(CursorPath)

if NewCursor is None:
    print "Error loading the cursor"
elif SetSystemCursor(NewCursor, win32con.IDC_ARROW) == 0:
    print "Error in setting the cursor"

Using the code below the cursor is changed system-wide though I have to restored to the arrow cursor below quitting the program. If there are other better ways I would appreciate your response.

from ctypes import *
import win32con

SetSystemCursor = windll.user32.SetSystemCursor #reference to function
SetSystemCursor.restype = c_int #return
SetSystemCursor.argtype = [c_int, c_int] #arguments

LoadCursorFromFile = windll.user32.LoadCursorFromFileA #reference to function
LoadCursorFromFile.restype = c_int #return
LoadCursorFromFile.argtype = c_char_p #arguments

CursorPath = "../cursor/MyCross.cur"

NewCursor = LoadCursorFromFile(CursorPath)

if NewCursor is None:
    print "Error loading the cursor"
elif SetSystemCursor(NewCursor, win32con.IDC_ARROW) == 0:
    print "Error in setting the cursor"
吃颗糖壮壮胆 2024-12-19 13:57:49
import win32con
import win32api
import win32gui
import ctypes
import time
import atexit


cursor = win32gui.LoadImage(0, 32512, win32con.IMAGE_CURSOR, 
                            0, 0, win32con.LR_SHARED)
save_system_cursor = ctypes.windll.user32.CopyImage(cursor, win32con.IMAGE_CURSOR, 
                            0, 0, win32con.LR_COPYFROMRESOURCE)



cursor = win32gui.LoadImage(0, "file.cur", win32con.IMAGE_CURSOR, 
                            0, 0, win32con.LR_LOADFROMFILE);
ctypes.windll.user32.SetSystemCursor(cursor, 32512)
ctypes.windll.user32.DestroyCursor(cursor);

我希望这有帮助

import win32con
import win32api
import win32gui
import ctypes
import time
import atexit


cursor = win32gui.LoadImage(0, 32512, win32con.IMAGE_CURSOR, 
                            0, 0, win32con.LR_SHARED)
save_system_cursor = ctypes.windll.user32.CopyImage(cursor, win32con.IMAGE_CURSOR, 
                            0, 0, win32con.LR_COPYFROMRESOURCE)



cursor = win32gui.LoadImage(0, "file.cur", win32con.IMAGE_CURSOR, 
                            0, 0, win32con.LR_LOADFROMFILE);
ctypes.windll.user32.SetSystemCursor(cursor, 32512)
ctypes.windll.user32.DestroyCursor(cursor);

I hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文