C++
  Home arrow C++ arrow Page 6 - Using MFC in C++ Part 2: Menus
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Using MFC in C++ Part 2: Menus
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2001-11-29

    Table of Contents:
  • Using MFC in C++ Part 2: Menus
  • The resource file
  • Adding a resource file to our project
  • A sample menu
  • Loading the menu
  • Linking up our new menus
  • Adding the OnItem functions
  • Adding accelerator keys
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using MFC in C++ Part 2: Menus - Linking up our new menus


    (Page 6 of 9 )

    To make our menu items “clickable” and to give them a purpose, we need to add some code to our app to let the C++ compiler know that we want to execute a certain function whenever a specific menu item is clicked on.

    Change the CMainWin() class declaration in menu.h, like it’s shown below:

    class CMainWin : public CFrameWnd

    {

    public:

    CMainWin();

    afx_msg void OnItem1();

    afx_msg void OnItem2();

    afx_msg void OnItem3();

    DECLARE_MESSAGE_MAP()

    };

    class CApp : public CWinApp

    {

    public:

    BOOL InitInstance();

    };


    As you can see, we have added three new lines. They are MFC function declarations, which will be called whenever a menu item is clicked on. Now, we need to modify the menu.cpp file. Firstly, we need to include our ids.h file:

    #include "ids.h"

    Next, modify the BEGIN_MESSAGE_MAP() and END_MESSAGE_MAP() lines, to look like this:

    BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)

    ON_COMMAND(IDM_1, OnItem1)

    ON_COMMAND(IDM_2, OnItem2)

    ON_COMMAND(IDM_3, OnItem3)

    END_MESSAGE_MAP()


    We have added three ON_COMMAND() macros to the message map for our main window. They tell the C++ compiler which messages our app will respond to. The ON_COMMAND macro takes two parameters: The first is the id of the message our app will “catch” (as defined in ids.h), and the second is the name of the function to execute whenever that message is “caught” from windows).

    Therefore, whenever our app receives an IDM_1 message from windows, it will execute the CMainWin::OnItem1() function, etc.

    More C++ Articles
    More By Mitchell Harper


       · I have one small question,can u tell me how to provide this key board short...
     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT