Question

I'm using python 2.7 (32-bit) on a windows 7 64-bit machine. I'm using the win32 Api to automate some windows tasks, also I'm new to python and win32 api. I saw similar questions but not in python and I had trouble understanding the code and sadly I'm new on here so I can't comment and ask questions because my rep is under 50, so I had to make my own question.

Lately I've been working with the system tray (notification area). I've gotten as far as clicking (left or right) on any icon in the tray by name.

Now the thing I need help with is accessing the context menu items after a right click.

So when I perform a right click there's a popup menu that comes up. I try to get a handle to it so I can click on its items or content and I get an error saying it's an invalid menu handle. If I try win32gui.GetSubMenu it fails, win32gui.GetMenu fails, something as simple as win32gui.GetMenuItemCount is returning -1, I need help on how to access such menu, navigate threw it and click an item.

A snippet of the code I've been trying:

# 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

Any help is greatly appreciated thank you!

Was it helpful?

Solution

First, you can't assume that the FindWindow call will get the pop-up menu window. If your code runs too quickly, it may be that the window is not yet created. You should play with Sleep in a not infinite loop.

Second, FindWindow returns a HWND, not a HMENU. Try to use the MN_GETHMENU Windows message (Sending it to the HWND, receiving a HMENU as result).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top