The*_*ent 5 python winapi click popupmenu notification-area
我在 Windows 7 64 位机器上使用python 2.7(32 位)。我正在使用win32 Api来自动执行一些 Windows 任务,而且我对 python 和 win32 api 很陌生。我看到了类似的问题,但不是在 python 中,我很难理解代码,遗憾的是我是新来的,所以我无法评论和提问,因为我的代表低于 50,所以我不得不提出自己的问题。
最近我一直在使用系统托盘(通知区域)。我已经按名称单击(向左或向右)托盘中的任何图标。
现在我需要帮助的是右键单击后访问上下文菜单项。
因此,当我右键单击时,会出现一个弹出菜单。我尝试获取它的句柄,以便可以单击其项目或内容,但收到一条错误消息,指出它是无效的菜单句柄。如果我尝试 win32gui.GetSubMenu 它失败,win32gui.GetMenu 失败,像 win32gui.GetMenuItemCount 这样简单的东西返回 -1,我需要有关如何访问此类菜单的帮助,导航抛出它并单击一个项目。
我一直在尝试的代码片段:
# 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
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助,谢谢!
首先,你不能假设调用FindWindow会得到弹出菜单窗口。如果您的代码运行得太快,则可能是窗口尚未创建。您应该以非无限循环的方式使用 Sleep。
其次,FindWindow返回 HWND,而不是 HMENU。尝试使用MN_GETHMENU Windows 消息(将其发送到 HWND,接收 HMENU 作为结果)。
| 归档时间: |
|
| 查看次数: |
3346 次 |
| 最近记录: |