Simulating right click on system tray icon and clicking on context menu in C++

1.3k views Asked by At

I am a Python developer with a little knowledge of C++.

With that said, I would like to understand how I can right click on a system tray icon, and click on one of the options on the context menu.

I have looked around the internet and was unable to find something that can get me the location of the system tray icons relative to the 'Notification Area'. Also, I can get the Button text of the tray icon.

I get the handle of ToolbarWindow32 using FindWindowEx.

I have tried to send WM_RBUTTONDOWN and WM_RBUTTONUP to the handle of ToolbarWindow32 with the X and Y coordinates, using SendMessage and nothing happens.

I am completely oblivious as to how I can right click the icon, and get the context menu information, and using that, click on one of the options.

1

There are 1 answers

0
TheCodingGent On

After my research there's no way to send a click message to a system tray icon, at least not through any API that I tried. The best way to do it and this is the way I'm following is the following:

  1. You Send the message TB_GETBUTTON to the toolbar.

  2. This will retrieve you an "idCommand" for the button you retrieve so you can use a loop to get all the "idCommand", which is found in the TBBUTTON structure.

  3. With the idComman you can send a message to the toolbar button with the toolbar handle to get the dimensions of the icon with the TB_RECT message.

  4. Once you know the dimensions of the button you just need to get the dimensions of the toolbar which is simple because it's just a window you make a cal to GetWindowRect

  5. Last step is now you want to send the click you make a call to win32api.mouse_event with x being: the left bound of the toolbar + half the width of the icon and y being: the top bound of the toolbar + half the height of the icon. (so you're sending the click to the center).

    That's it hope it helps! I've asked a similar question and answered it here.