python winapi_从弹出菜单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作为结果).

标签:python,click,winapi,popupmenu,notification-area

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值