Monday, April 1, 2013

visual c++(vc++) ,mfc program for respond to a menu item

 /*TO RESPOND TO A MENU ITEM*/


#include<afxwin.h>
#include"resource.h"
class myframe:public CFrameWnd
{
public:
    myframe()
    {
      Create(0,"my mfc",WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));
    }
    void mymessage()
    {
        MessageBox("MESSAGE","HAI");
    }
    void mycommand()
    {
        MessageBox("COMMAND","HAI");
    }
    DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_COMMAND(100,mymessage)
ON_COMMAND(101,mycommand)
END_MESSAGE_MAP()
class myapp:public CWinApp
{
public:
     virtual BOOL InitInstance()
     {
        m_pMainWnd=new myframe();
        m_pMainWnd->ShowWindow(1);
        m_pMainWnd->UpdateWindow();
        return TRUE;
     }
};
myapp app;


Adding Menus and Menu Items to Windows Forms

A menu on a Windows Form is created with a MainMenu object, which is a collection of MenuItem objects. You can add menus to Windows Forms at design time by adding the MainMenu component and then appending menu items to it using the Menu Designer. Menus can also be added programmatically by adding one or more MainMenu objects to a Windows Form and adding MenuItem objects to the collection. The procedures in this topic show how to create a simple menu named File, either with the Menu Designer or in code.
To add a menu to a Windows Form at design time
  1. Open the form you wish to add a menu to in the Windows Forms Designer.
  2. In the Toolbox, double-click the MainMenu component. A menu is added to the form (displaying the text "Type Here"), and the MainMenu component is added to the component tray.

No comments:

Post a Comment