win32gui.FindWindow未返回正确的值

2024-04-16 09:43:46 发布

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

所以我总是用这段代码来查找窗口的位置/大小,今天我试图找到Memu(android仿真器)窗口

此代码:

def print_memu_position():
    hwnd = win32gui.FindWindow(None, "MEmu")
    left, top, right, bottom = win32gui.GetWindowRect(hwnd)
    print "topleft_xy = {},{}".format(left, top)
    print "bottomright_xy = {},{}".format(right, bottom)
    print "width = {}".format(right-left)
    print "height = {}".format(bottom-top)

印刷品:

topleft_xy = 3635,881
bottomright_xy = 3714,925
width = 79
height = 44

这是不正确的Memu窗口是574x982

我使用了3个屏幕,但这应该不是一个问题,因为当我使用此代码查找其他内容时,例如Vysor或您计算机上的手机屏幕,我会得到正确的结果

以下是在视觉上发现的内容:

enter image description here

关于如何解决这个问题有什么想法吗? 谢谢

编辑:

此代码确实可以找到MEmu hwnd

def printMemuHwnd():

    def winEnumHandler(hwnd, ctx):
        if win32gui.IsWindowVisible(hwnd):
            if win32gui.GetWindowText(hwnd) == "MEmu":
                print (hex(hwnd), win32gui.GetWindowText(hwnd))
                left, top, right, bottom = win32gui.GetWindowRect(hwnd)
                print "topleft_xy = {},{}".format(left, top)
                print "bottomright_xy = {},{}".format(right, bottom)
                print "width = {}".format(right - left)
                print "height = {}".format(bottom - top)

    win32gui.EnumWindows(winEnumHandler, None)

通过GetForegroundWindow()我得到了正确的维数

那么为什么hwnd = win32gui.FindWindow(None, "MEmu")不起作用呢


Tags: 代码rightnoneformattopdefleftprint