从弹出菜单win32 api Python中选择项目

我在 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作为结果).

相关文章

文章浏览阅读2.2k次,点赞6次,收藏20次。在我们平时办公工作...
文章浏览阅读1k次。解决 Windows make command not found 和...
文章浏览阅读3.2k次,点赞2次,收藏6次。2、鼠标依次点击“计...
文章浏览阅读1.3w次。蓝光版属于高清版的一种。BD英文全名是...
文章浏览阅读974次,点赞7次,收藏8次。提供了更强大的功能,...
文章浏览阅读1.4w次,点赞5次,收藏22次。如果使用iterator的...