In Python2.7 with win32
, I can easily get other window's toolStrip1
handle through win32gui.EnumChildWindows
, however I do not know how to get it's button item control through it's item name, e.g click item button "Load" in toolStrip1.
Can I try to use win32gui.SendMessage(handle, click, "Load"......)
or a similar way to achieve this?
I can use rect x,y of toolStrip1 via win32gui.GetWindowRect
, and roughly can do click button via x+40, x+80 accordingly, but it is not really good specially when the item position changes in the new version.
Here is the code from a sample and modified with a few other issues:
import win32con import commctrl, ctypes from ctypes import * import sys import time
class TextBox:
# 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=TextBox()
rx.run()