在 Python2.7 with 中,我可以通过 win32guiwin32
轻松获取其他窗口的句柄。,但是我不知道如何通过它的项目名称来获得它的按钮项目控制,例如单击toolStrip1中的项目按钮“加载”。toolStrip1
EnumChildWindows
我可以尝试使用win32gui.SendMessage(handle, click, "Load"......)
或类似的方式来实现这一点吗?
我可以使用 rect x,y 的 toolStrip1 via win32gui.GetWindowRect
,大致可以通过x+40,x+80相应地点击按钮,但是特别是在新版本中项目位置发生变化时,它并不是很好。
这是示例中的代码,并修改了一些其他问题:
import win32con import commctrl, ctypes from ctypes import * import sys import time
类文本框:
# 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
rx=文本框()
rx.run()