簡體   English   中英

從彈出菜單win32 api Python中選擇項目

[英]Select item from popup menu 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作為結果)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM