I would like to attach to a separate application (Microsoft Excel, for example), and detect when a certain menu item is clicked (or a ribbon command in new versions, whatever).
I thought I could do it using the RegisterWindowMessage in user32.dll, but I have no clue which messages to intercept. Ideally, I would like to generalize this and detect something like:
"menu item XXX was clicked in the app YYY"
I found this CodeProject article which shows how to register hooks for events like creation of controls, application start/stop etc., but I couldn’t find an example of how to get button clicks or menu clicks.
Is this even possible? Am I on the right track, or I need to take a different approach?
Alright, so I couldn’t resist the challenge here 🙂 Wrote a little program that does what you want.
How it works :
SetWindowsHookExlets you place a global mouse hook. Now, you get theX,Yand then use WindowFromPoint to get thehWndof your target window. From here you can do anything you like, in my case, I sent aWM_GETTEXTto get its title.This is what the program looks like implemented. Once you click
Begin, it looks globally for Right Click events, and adds them to the listbox. Note : It NEEDS to be a window forms app, the hook will not work with a console app.Usage (Just create a default WinForms project and change it to this):
Implementation (It’s a bit of code 😉 )