Python 如何使用Win32 GUI或类似工具,通过按钮名称单击其他窗口toolStrip1项目按钮?
在Python2.7中,使用Python 如何使用Win32 GUI或类似工具,通过按钮名称单击其他窗口toolStrip1项目按钮?,python,python-2.7,win32gui,toolstripmenu,Python,Python 2.7,Win32gui,Toolstripmenu,在Python2.7中,使用win32,我可以通过win32 GUI轻松获得其他窗口的toolStrip1句柄。EnumChildWindows,但是我不知道如何通过它的项名称获得它的按钮项控件,例如单击toolStrip1中的项按钮“加载” 我是否可以尝试使用win32gui.SendMessage(句柄,单击“加载”…)或类似的方法来实现此目的 我可以通过win32gui.GetWindowRect使用toolStrip1的rect x,y,并大致可以通过x+40,x+80相应地单击按钮,但
win32
,我可以通过win32 GUI轻松获得其他窗口的toolStrip1
句柄。EnumChildWindows
,但是我不知道如何通过它的项名称获得它的按钮项控件,例如单击toolStrip1中的项按钮“加载”
我是否可以尝试使用win32gui.SendMessage(句柄,单击“加载”…)
或类似的方法来实现此目的
我可以通过win32gui.GetWindowRect
使用toolStrip1的rect x,y,并大致可以通过x+40,x+80相应地单击按钮,但在新版本中,当项目位置发生变化时,效果并不好
下面是一个示例中的代码,并修改了一些其他问题:
导入win32con
导入commctrl、CTYPE
从ctypes导入*
导入系统
导入时间
类文本框:
# represent the TBBUTTON structure
# note this is 32 bit, 64 bit padds 4 more reserved bytes
class TBBUTTON(Structure):
_pack_ = 1
_fields_ = [
('iBitmap', c_int),
('idCommand', c_int),
('fsState', c_ubyte),
('fsStyle', c_ubyte),
('bReserved', c_ubyte * 2),
('dwData', c_ulong),
('iString', c_int),
]
class RECT(Structure):
_pack_ = 1
_fields_ = [
('left',c_ulong),
('top',c_ulong),
('right',c_ulong),
('bottom',c_ulong),
]
def run(self):
#init vars
self.count=0
#get the TestApp window
lhWnd = win32gui.FindWindow(None,'TestApp')
print "TestApp handle=",lhWnd
#get the TestApp child window
win32gui.EnumChildWindows(lhWnd, self.EnumChildWindows, 0)
print "toolStrip1 handle=",self.toolbar_hwnd
#focus TestApp window
ctypes.windll.user32.SetFocus(lhWnd)
win32gui.SetForegroundWindow(lhWnd)
win32gui.ShowWindow(lhWnd, win32con.SW_SHOWNORMAL)
#############################################donot work part####################################################
#to get how many item buttons in toolStrip1, it return 0 -- WHY? <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
cnt = windll.user32.SendMessageA(self.toolbar_hwnd, commctrl.TB_BUTTONCOUNT, 0, 0)
print "Why button cnt=0?? cnt=",cnt
#and seems this is no affection as well -- Why? <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
index=1 #assume we got Load button index from it's name (do not know how?, Load Button index=1 based on 0
win32api.PostMessage(self.toolbar_hwnd,commctrl.TCM_SETCURFOCUS,index,0) #TCM_SETCURSEL
print "Why?? cannot focus on [Load] Button"
time.sleep(3)
#try to get [Load] button's rect, but not work <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pid = c_ulong();
windll.user32.GetWindowThreadProcessId(self.toolbar_hwnd, byref(pid))
hProcess = windll.kernel32.OpenProcess(win32con.PROCESS_ALL_ACCESS, 0, pid)
lpPointer = windll.kernel32.VirtualAllocEx(hProcess, 0, sizeof(TBBUTTON), win32con.MEM_COMMIT, win32con.PAGE_READWRITE)
rlpPointer = windll.kernel32.VirtualAllocEx(hProcess, 0, sizeof(RECT), win32con.MEM_COMMIT, win32con.PAGE_READWRITE)
# init our tool bar button and a handle to it
tbButton = TBBUTTON()
butHandle = c_int()
idx_rect = RECT()
# query the button into the memory we allocated
windll.user32.SendMessageA(self.toolbar_hwnd, commctrl.TB_GETBUTTON, index, lpPointer)
# read the memory into our button struct
windll.kernel32.ReadProcessMemory(hProcess, lpPointer, addressof(tbButton), 20, None)
# read the 1st 4 bytes from the dwData into the butHandle var
# these first 4 bytes contain the handle to the button
windll.kernel32.ReadProcessMemory(hProcess, tbButton.dwData, addressof(butHandle), 4, None)
# get the pid that created the button
butPid = c_ulong()
windll.user32.GetWindowThreadProcessId(butHandle, byref(butPid))
wszBuff = create_unicode_buffer(win32con.MAX_PATH)
windll.kernel32.ReadProcessMemory(hProcess, tbButton.iString, wszBuff, win32con.MAX_PATH, None)
win32api.SendMessage(self.toolbar_hwnd,commctrl.TB_GETRECT,tbButton.idCommand,rlpPointer)
windll.kernel32.ReadProcessMemory(hProcess, rlpPointer, addressof(idx_rect), sizeof(idx_rect), None)
xpos = int((idx_rect.right-idx_rect.left)/2)+idx_rect.left
ypos = int((idx_rect.bottom-idx_rect.top)/2)+idx_rect.top
lParam = ypos<<16 | xpos
print "Why x,y=0?? [Load] button X,Y=",xpos,ypos
###############################################################################################################
#but assume we got button [Load] Rect[10,80] based on toolStrip1, and click it WORKS!
lParam = 10<<16 | 80
win32api.PostMessage(self.toolbar_hwnd,win32con.WM_LBUTTONDOWN,win32con.MK_LBUTTON,lParam)
win32api.PostMessage(self.toolbar_hwnd,win32con.WM_LBUTTONUP,win32con.MK_LBUTTON,lParam)
def EnumChildWindows(self,lhWnd,lParam):
text = win32gui.GetWindowText(lhWnd)
#rect1 = win32gui.GetWindowRect(lhWnd)
if text=="toolStrip1":
self.toolbar_hwnd=lhWnd
#表示TBBUTTON结构
#注意这是32位,64位填充4个保留字节
类别TBBUTTON(结构):
_包装=1
_字段=[
('iBitmap',c_int),
('idCommand',c_int),
('fsState',c_ubyte),
('fsStyle',c_ubyte),
('bresered',c_ubyte*2),
(“dwData”,c_ulong),
(“iString”,c_int),
]
类RECT(结构):
_包装=1
_字段\=[
(‘左’,c_ulong),
(“顶部”,c_ulong),
(“对”,c_ulong),
(“底部”,c_ulong),
]
def运行(自):
#初始化变量
self.count=0
#获取TestApp窗口
lhWnd=win32gui.FindWindow(无,'TestApp')
打印“TestApp handle=”,lhWnd
#获取TestApp子窗口
win32gui.EnumChildWindows(lhWnd,self.EnumChildWindows,0)
打印“toolStrip1 handle=”,self.toolbar\u hwnd
#焦点测试窗口
ctypes.windell.user32.SetFocus(lhWnd)
win32gui.SetForegroundWindow(lhWnd)
win32gui.ShowWindow(lhWnd、win32con.SW\u SHOWNORMAL)
#############################################在现场工作工作现场,现场工作工作现场现场,现场现场工作工作现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场工作工作工作工作现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场##############
#要获取toolStrip1中有多少项按钮,它返回0——为什么?