Link to home
Start Free TrialLog in
Avatar of sivaramy
sivaramy

asked on

Executing Menu Item Programmatically

I want to execute a menu item in a application programmatically from a C++ program. I don't know thw exact position of that menu. But I know it is the Rightmost menu topics first item. How can I execute it?
Avatar of chensu
chensu
Flag of Canada image

1. Most menu items have a shortcut key. You can set focus to the window and use keybd_event to simulate that key press.

or

2. Use Spy++ to find out the menu ID (WM_COMMAND). Then you can send the WM_COMMAND message to execute it.

or

3. I am not whether GetMenu works if the window belongs to other processes. If it does, use GetMenu, GetSubMenu and GetMenuItemID to get the menu ID and send a WM_COMMAND message.
Avatar of sivaramy
sivaramy

ASKER

I want to execute rightmost menu directly.

I don't want to take risk in giving keyboard events.

How can I ask others(Customers) to run Spy++?

Please explain (3)
>How can I ask others(Customers) to run Spy++?

I meant you run Spy++ to find out the menu IDs in cases that they are fixed.

>Please explain (3)

HMENU hm = GetMenu(hWnd);
HMENU hsm = GetSubMenu(hm, nPos1?);
UINT nID = GetMenuItemID(hsm, nPos2?);

Try it.
>I don't want to take risk in giving keyboard events.

Yes!!  I heard about a guy last year who was using a program that gave keyboard events.  Something went wrong and the whole system blew up in his face.  Not a pretty sight!
hehe.... do you know where the space button of your keyboard is? you dont? that's what i thought... heh...
I thought buttons were for mice and keys for keyboards. Of course cats have seven lifes, but we all know that's another story. What ? Off topic ?

Anyway, I agree with Chensu. and GetMenu() will work from another process (I think)

TSM :-)
Please without telling jokes can any one answer my question?
Your question _WAS_ answered but you rejected it.  So now the only thing left to do is joke about it!
MessageBox(NULL, "Please chose the rightmost menu and click the third item","Important message", 0);

That would do the trick :)

WONDERFUL!

Thanks a lot!

Wow!

Superb!
ASKER CERTIFIED SOLUTION
Avatar of TSM1999
TSM1999

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks TSM1999. I will test it now. To find the window handle I have a piece of code given by Chensu in another EE Question. I will post it also later.Thanks again for your effort.
Wow! It is working!! Thanks a lot. Here is the code to find a application window handle of we know a part of that title. Given By Chensu

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);

::EnumWindows(EnumWindowsProc, 0);

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    int nLen = ::GetWindowTextLength(hwnd);
    if (nLen > 0)
    {
        LPTSTR lpszBuf = new TCHAR[nLen + 1];
        if (::GetWindowText(hwnd, lpszBuf, nLen + 1) > 0)
        {
            // parse lpszBuf, you might want to use the _tcsstr function
            if (found)
                return FALSE;    // stop
        }
        delete []lpszBuf;
    }

    return TRUE;    // continue
}