如何使用win32gui或类似工具,通过按钮名单击另一个windowtoolstrip1项按钮?

2024-10-03 02:48:01 发布

您现在位置:Python中文网/ 问答频道 /正文

在带有win32的Python2.7中,我可以很容易地通过win32gui获得其他窗口的toolStrip1句柄。EnumChildWindows,但是我不知道如何通过它的项名称来获得它的按钮项控件,例如在toolStrip1中单击项目按钮“加载”。在

我可以尝试使用win32gui.SendMessage(handle, click, "Load"......)或类似的方法来实现这一点吗?在

我可以通过win32gui.GetWindowRect使用toolStrip1的rect x,y,大致可以通过x+40x+80来做相应的点击按钮,但在新版本中,当项目位置发生变化时,这并不是很好。在

下面是一个示例中的代码,经过一些其他问题的修改:

导入win32con 导入commctrl,ctypes 从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

rx=文本框()

在rx.运行()


Tags: therectselfloadbuttontoolbaridxwin32gui