Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to add Menu programmatically. and want to add some menu item to it and append a handler to the menuitems.

Can you help me how it can be done??
Posted

1 solution

POINT point;
CMenu menuHelp;

GetCursorPos(&point);
menuHelp.LoadMenu(IDR_MENU_SYSTRAY);
CMenu*pSubMenu = menuHelp.GetSubMenu(0);
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);


As you can see this piece of code creates the menu at the cursor position, you may have to change that, but the principle is the same.

Also you should know when you create the resource for the menu, that this code shows the first submenu not the primary menu itself!
For example if you pass the ID of the main program menu IDR_MAINFRAME the code above will show the content of the File menu not the main menu ("File | Edit | View ...")

appending handlers to the menuitems of a menu created that way is no different than usual. you go to the menu resource, right click on an item and add event handler that's it.

Appending menu items:
You can see how it's done in a newly created dialog based application's OnInitDialog function. see how they append the "About..." item to the system menu
 
Share this answer
 
Comments
Bhavin Jagad 2-Aug-10 7:49am    
thank you.
I tried following but it doesn't work

CMainFrame::CMainFrame()
{
Create(NULL, "Menus Fundamentals");

VERIFY(this->m_wndMainMenu.CreateMenu());
VERIFY(this->m_wndFileMenu.CreateMenu());

this->m_wndMainMenu.AppendMenu(MF_STRING, NULL, "File");
this->SetMenu(&m_wndMainMenu);
}

where m_wndMainMenu and m_wndFileMenu are object of type CMenu.
anything wrong with this?
Ivan Ivanov 83 2-Aug-10 8:27am    
Well yes there are few things: :)
- First. This code isn't supposed to be in the constructor of CMainFrame, it's for OnCreate, you should put it there. At the point the constructor of the main frame is called the main frame is NOT a window yet!

- Second. if you want a menu in a MFC application you need to create a menu resource! simply calling CreateMenu won't do the job at all. ;)

- Third. You need to specify where do you want the menu to appear, in terms of coordinates. ;)

- Fourth. You need to specify the window which the menu belongs to, the menu has to have a parent window! This is very important. ;)
The code should look like this

m_wndMainMenu.LoadMenu(IDR_MAINMENU);
m_wndFileMenu.LoadMenu(IDR_FILEMENU);
....
Now as I see your code sample... I think you want to create THE main menu and THE file menu, why?!!!
They are created automatically with the solution itself if you've created MFC SDI project.
You are not supposed to create them yourself!

What version of visual studio do you use?
Bhavin Jagad 2-Aug-10 9:48am    
thanks you,

i finally get my code and it works..

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{ /* ...........here auto generated code...........
.........................*/
// Following code add Menu dynamically
CMenu mainMenu;//this object hold main menu
mainMenu.CreateMenu();
CMenu PopUpMenu;//this will hold sub menu
PopUpMenu.CreatePopupMenu();
PopUpMenu.AppendMenu(MF_STRING|MF_ENABLED,ID_RECT,"Rectangle");//MF_STRING
PopUpMenu.AppendMenu(MF_STRING|MF_ENABLED,ID_CIRCUL,"Circle");
PopUpMenu.AppendMenu(MF_STRING|MF_ENABLED,ID_ERASE,"Erase");
mainMenu.AppendMenu (MF_POPUP, (UINT)PopUpMenu.Detach (), "Draw");
SetMenu(&mainMenu);
mainMenu.Detach();
return 0;
}
Bhavin Jagad 2-Aug-10 9:58am    
actually i am trying to make application as follow

Create an SDI application that incorporates the following object
hierarchy.
(CVeiw|(Class derived from CWnd))
• The CWnd class should response to all size notifications and resize
accordingly. It should maximize itself to utilise the entire client
area.
• The class should be able to support the following interfaces.
• ShowRectangle() - A rectangle should be drawn anywhere within
the client area.
• ShowCircle() - The first parameter being a structure that
defines the dimension and colour of the circle.
• Erase All – Clears the canvas.
• The above methods within the class should be invoked when the user
chooses Draw -> Circle, Draw -> Rectangle, Draw -> Erase from the
menu.

Now my next thing to learn is How I can draw in the client area of the child window(Created by Class derived from CWnd) when i click on the Menu item???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900