?
Solved

Executing Menu Item Programmatically

Posted on 1999-10-02
13
Medium Priority
?
362 Views
Last Modified: 2010-04-16
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?
0
Comment
Question by:sivaramy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 2093895
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.
0
 
LVL 1

Author Comment

by:sivaramy
ID: 2093978
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)
0
 
LVL 23

Expert Comment

by:chensu
ID: 2094021
>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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:jhance
ID: 2094575
>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!
0
 
LVL 10

Expert Comment

by:viktornet
ID: 2095870
hehe.... do you know where the space button of your keyboard is? you dont? that's what i thought... heh...
0
 

Expert Comment

by:TSM1999
ID: 2096977
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 :-)
0
 
LVL 1

Author Comment

by:sivaramy
ID: 2099954
Please without telling jokes can any one answer my question?
0
 
LVL 32

Expert Comment

by:jhance
ID: 2100402
Your question _WAS_ answered but you rejected it.  So now the only thing left to do is joke about it!
0
 

Expert Comment

by:TSM1999
ID: 2103474
MessageBox(NULL, "Please chose the rightmost menu and click the third item","Important message", 0);

That would do the trick :)

0
 
LVL 1

Author Comment

by:sivaramy
ID: 2106732
WONDERFUL!

Thanks a lot!

Wow!

Superb!
0
 

Accepted Solution

by:
TSM1999 earned 240 total points
ID: 2106934

Ok, let's be a little more serious. This function executes the third item of the last menu of notepad.exe (tested on NT, should work the same on 95/98) (launch NOTEPAD before :)

Note that you should use SPI++ from VC 5 (or VC 6) to get the class name. This has to be done once to put the name of the class in the FindWindow() function call.

If the caption of the main window of the app you want to invoke is always the same, you could alternatively use

 hTarget = ::FindWindow(NULL, "Some application V1.0");

In that case the class name is not necessary...

void CMITDlg::OnButton1()
{
 HWND hTarget;
 int i;
 HMENU hsm = (HMENU)-1;
 HMENU hsm2 = NULL;

 
 // "Notepad" is the class name of the application
 // to know it, just point SPY++ at the application window
 // and look for "Class Name"
 hTarget = ::FindWindow("Notepad", NULL);
 if (hTarget == NULL)
   return;

 // Get the top level menu item
 HMENU hm = ::GetMenu(hTarget);
 if (hm == NULL)
   return;
 
 // Iterate all the menus. just keep the last ones (the rightmost)
 i = 0;
 while (hsm != NULL)
   {
    hsm2 = hsm;
    hsm = ::GetSubMenu(hm, i);
    i++;
   }
 //We did not find any menu ??????
 if (hsm2 == (HMENU)-1) return;
 
 
 UINT nID;
 // Get the ID of the menuitem (it is ZERO based, the first item is 0
 // in notepad, item 2 (third item) is ABOUT
 nID = ::GetMenuItemID(hsm2, 2);
 
 // send the message to the target window to execute the menu item.
 ::SendMessage(hTarget, WM_COMMAND, nID, 0);
}

This really does answer your question...

0
 
LVL 1

Author Comment

by:sivaramy
ID: 2111398
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.
0
 
LVL 1

Author Comment

by:sivaramy
ID: 2111417
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
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question