【问题标题】:Select item from popup menu win32 api Python从弹出菜单中选择项目 win32 api Python
【发布时间】:2014-02-12 20:59:51
【问题描述】:

我在 Windows 7 64 位机器上使用 python 2.7(32 位)。我正在使用 win32 Api 来自动化一些 Windows 任务,我也是 python 和 win32 api 的新手。我看到了类似的问题,但在 python 中没有,而且我无法理解代码,遗憾的是我是新来的,所以我无法评论和提问,因为我的代表不到 50 岁,所以我不得不自己提出问题。

最近我一直在使用系统托盘(通知区域)。我已经按名称单击(向左或向右)托盘中的任何图标。

现在我需要帮助的是在右键单击后访问上下文菜单项

所以当我执行右键单击时,会出现一个弹出菜单。我尝试获取它的句柄,以便我可以单击它的项目或内容,但我收到一个错误,说它是无效的菜单句柄。如果我尝试 win32gui.GetSubMenu 失败,win32gui.GetMenu 失败,就像 win32gui.GetMenuItemCount 返回 -1 一样简单,我需要有关如何访问此类菜单、导航并单击项目的帮助。

我一直在尝试的代码的 sn-p:

# retrieves a handle to the notification area toolbar
tb = getNotificationAreaToolbar()

# clicks on an icon in the system tray say I'm right clicking the sound icon 
#(in my case AMD HDMI Output)
clickSystemTrayIcon('right', 'AMD HDMI Output', tb)

#now the context popup menu comes up.
# According to MSDN the class name for such menu is #32768
hPopupmenu = win32gui.FindWindow("#32768", "")

# An example of a try to access the menu items
# Getting the count: this is returning -1 saying the handle is not a menu handle
count = win32gui.GetMenuItemCount(hPopupMenu)

#send a command, doesn't do anything
win32gui.PostMessage(tb, win32con.WM_COMMAND, win32gui.GetMenuItemId(hPopupmenu,1) , 0)

# the thing that makes me sure that I'm getting the right window of the popup is 
# win32gui.GetWindowRect(hPopmenu) it's returning the right position of the menu

非常感谢您的帮助!

【问题讨论】:

    标签: python winapi click popupmenu notification-area


    【解决方案1】:

    首先,您不能假设FindWindow 调用会得到弹出菜单窗口。如果您的代码运行速度过快,则可能是该窗口尚未创建。您应该在非无限循环中玩 Sleep。

    其次,FindWindow 返回 HWND,而不是 HMENU。尝试使用MN_GETHMENU Windows 消息(将其发送到 HWND,收到 HMENU 作为结果)。

    【讨论】:

    • 谢谢 manuell 我很感激你总是在这里帮助我。如果我使用 FindWindow 然后调用 MN_GETHMENU,则您的解决方案有效,我得到了我想要的弹出菜单的句柄。至于工作正常的窗口矩形,在我的实际代码中,我确实在两者之间调用了睡眠。同样,我对这些东西还是新手,有时我找不到足够的文档,但我肯定会学到很多东西!也很高兴我的代表上升了,所以我可以在这里评价答案并做更多的事情,再次感谢! :)
    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多