Question

I'm writing an application in which I need to click a menu item from another application's tray icon's menu. Here's a screenshot for a better understanding, because I'm bad at explaining stuff. Screenshot example
So I need to click one of those menu items, for example "Quit".

I know that I should use WinApi in order to do that. However, as far as I know, the message WM_NOTIFY is responsible for notifying the top window about the menu item click. And as the MSDN site says, For Windows 2000 and later systems, the WM_NOTIFY message cannot be sent between processes. The PC I want to run my application is running Windows XP, so I can not send this message to the window. Is there any other way to click the menu item?

Thanks in advance.

Was it helpful?

Solution

Your solution would most likely be application specific. What an application with tray icon menu is typically doing is registering an icon and providing its own window handle HWND for notifications, where Windows shell would forward mouse events. Once the icon is clicked, the window receives a forwarded even and uses TrackPopupMenu API to display the menu, and - with certain luck - requesting that selected menu item identifier is posted back to some window, perhaps the same window, as WM_COMMAND message/notification.

So, what you basically need is to spy over your application of interest what messages are generated within its process or thread when you click the menu. Spy++ is a good tool to do it. Once you see if there is a specific message (command) posted to some window as a result of tray icon context menu item selection, all you need in your controlling application is to locate this window, that is find its HWND by name, process name whatsoever, and then post a WM_COMMAND message to it.

This is not guaranteed to work because we are making certain assumptions as for how the menu is implemented on the application side, however it makes sense to research along the mentioned strategy.

UPD. My Asus EEE PC netbook came with Asus utility to switch monitor resolution through system tray icon and menu. And default resolution was not good for me. This small app posted a command to this utility to automate resolution change.

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