简体   繁体   English

暂时把cursor改成Python

[英]Temporarily change cursor using Python

I am writing a script that intercepts a touchpad output and send to the windows after some processing.我正在编写一个脚本,拦截触摸板 output 并在经过一些处理后发送到 windows。 So there is no GUI involved.因此不涉及 GUI。 I want to change the cursor temporarily when certain cursor behavior occurs.当某些 cursor 行为发生时,我想临时更改 cursor。 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.我已尽我所能搜索 web,发现很少有帖子谈到使用 win32api.SetCursor() 但这根本不起作用。 Most of the posts talks about changing cursor using Tkinter or wxPython.大多数帖子都在谈论使用 Tkinter 或 wxPython 更改 cursor。 Is there any other solution to change the cursor system wide?是否有任何其他解决方案来更改 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"
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我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM