How To Insert An Executeable In An MFC Application Menu Item

Greetings,

I want to insert an executable in my application as a submenu item from the main menu bar.  What code is necessary to do this?

This is what I know so far:

In the 'Docked Window' under the 'Rescource' tab and the subfolder marked 'Menu' are two IDR objects.  One is the default IDR_MAINFRAME, the other is the IDR_PROJECTNAME.

If the project IDR is selected, the current available menu items are displayed in the workspace.  From here on, I need help.  What steps make it possible to launch an exe from the menu item I select?

I'm guessing that I should select the menu item in which the exe should appear as a submenu item.  Then, from that dropdown list, select the blank box listed as the last submenu item, choose properties, select an 'ID:' from the dropdown box, type in a caption in the Caption Box.  Even if I'm correct in these steps, I'm sure some code needs to be inserted somewhere.

This is where I need help, can you help me with these steps?  

Thanks!




 

John500Asked:
Who is Participating?
 
lif081199Connect With a Mentor Commented:
Well, once you have created your new menu item with the resource editor :

- Create in your frame a member function to handle the menu message from this item.

// in the .H of your frame
afx_msg void OnMyMenuSelected();

// in the .CPP

void CMyFrame::OnMyMenuSelected()
{
   // Launch your executable
   ShellExecute(... )
}

You have now to assign your function to the menu item message. If this item is IDM_EXECUTE_THIS (its ID), add in the message map on your frame :

BEGIN_MESSAGE_MAP(CMyFrame, CFrameWnd)
    ...
    ON_COMMAND(IDM_EXECUTE_THIS ,OnMyMenuSelected)
    ...
END_MESSAGE_MAP()

And that's all !
Read the help about ShellExecute to know how to launch the executable.

Good luck,
Lionel.




0
 
lif081199Commented:
For example about ShellExecute, here is how to launch the windows calc.exe :

HWND ParentWindow = NULL;
char *FileName = "c:\\windows\\calc.exe";
ShellExecute(ParentWindow,"open",FileName,NULL,NULL,SW_SHOWNORMAL);

Bye,
Lionel.
0
 
John500Author Commented:
Lionel,

Thanks for the response.  I have 'zilch' MFC experience.  This task will be my first introduction to Windows/MFC programming.

Up until now, I've had two courses in data structures and one operating systems course which got my feet wet to programming, but that's it.

Some of the terms you mentioned left me in the dust.

Starting from:

"Create in your frame a member function to handle the menu message from this item."

Can you define 'frame' or how I access it?  Does this have anything to do with the ClassWizard, or can I access the 'frame' using the ClassWizard?  I can see from within the ClassWizard that a block of empty space/code has already been allotted for this menu item and it looks like this:

void CProjectHelpMenu::OnProjecthelp()
{
      // Call Help file
      ShellExecute("Projecthelp.hlp", SW_SHOW);

}

I think the above code satisfies the portion you define as:

// in the .CPP"

But I still have questions.  Should the full file path of 'Projecthelp.hlp' be displayed like "C:\Help\ProjectHelp.hlp", SW_SHOW)

How do I access the .h file you mentioned to perform the following:

// in the .H of your frame
afx_msg void OnMyMenuSelected();

After I knock that out, I'll just need help on assigning my function to the menu item message.

Thanks!


0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
lif081199Commented:
In a MFC non Dialog-box based application, the main window is called a Frame window. In a MDI application, a child window is called a Child Frame.

The class of a frame is CFrameWnd, which herits from the classical CWnd.

The wizard create a CMainFrame class for you, which herits from a CFrameWnd.

You can of course use the wizard to add this function.

BUT It seems that the wizard has already add the function to your project. So, you just have to add the ShellExecute function, as you mentionned it.


0
 
lif081199Commented:
For the ShellExecute function, if the "slave" application is with you own app (in the same dir, for example), you don't need to give the full path. Only "slave.exe" is needed.

0
 
lif081199Commented:
To access to the .H file, just double-clic on the name of the CMainFrame class, in the Project window of Visual Studio (the tree on the left of your screen)

This will open the .H.
0
 
lif081199Commented:
Just ask for more details. You can also send me your code (only .CPP and .H files !!!!) here : lfumery@nordnet.fr
in order I see exactly your situation.

Good luck !
Lionel.
0
 
John500Author Commented:
Thanks, that did the trick
0
All Courses

From novice to tech pro — start learning today.