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

标签 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

非常感谢任何帮助,谢谢!

最佳答案

首先,您不能假设 FindWindow 调用将获得弹出菜单窗口。如果您的代码运行得太快,则可能是窗口尚未创建。您应该在一个非无限循环中使用 Sleep。

其次,FindWindow 返回一个 HWND,而不是一个 HMENU。尝试使用 MN_GETHMENU Windows 消息(将其发送到 HWND,结果接收到 HMENU)。

关于python - 从弹出菜单中选择项目 win32 api Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21739488/

相关文章:

multithreading - 调用 Delphi DLL 的意外线程行为

javascript - 停止 jQuery "click scroll"重置

javascript - 为什么Android点击区域的半径比touchstart更宽?我怎样才能使它一致?

python selenium点击按钮

python - 我怎样才能有效地半向前/向后填充数据帧中的空白?

python - 如何将 jupyter notebook 设置为在浏览器上自动打开

python - 两个数据帧的 Pandas join.fillna 将所有值替换为,而不仅仅是 nan

python - 如何确定使用哪种合并模式(添加/平均/乘法/点/连接)?

C++ Win32,用位图显示窗口的最简单方法

c++ - 枚举注册表项 C++