加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 05:24:36 所属栏目:Windows 来源:网络整理
导读:我在 Windows 7 64位机器上使用python 2.7(32位).我正在使用win32 Api自动执行一些Windows任务,我也是python和win32 api的新手.我看到了类似的问题,但没有在python中,我无法理解代码,很遗憾我是新来的,所以我不能评论和提问,因为我的代表不到50,所以我不得不
我在 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作为结果).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读