我在 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
非常感谢任何帮助,谢谢!
最佳答案
首先,您不能假设 FindWindow
调用将获得弹出菜单窗口。如果您的代码运行得太快,则可能是窗口尚未创建。您应该在一个非无限循环中使用 Sleep。
其次,FindWindow
返回一个 HWND,而不是一个 HMENU。尝试使用 MN_GETHMENU Windows 消息(将其发送到 HWND,结果接收到 HMENU)。
关于python - 从弹出菜单中选择项目 win32 api Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21739488/