C++
  Home arrow C++ arrow Page 3 - Using MFC in C++ Part 1: A Basic Applicati...
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 
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 1: A Basic Application Skeleton
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 55
    2001-11-26

    Table of Contents:
  • Using MFC in C++ Part 1: A Basic Application Skeleton
  • Building the framework
  • Creating the framework classes
  • Adding the code to MyClass.cpp
  • Making sure our application runs
  • Responding to windows messages
  • 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 1: A Basic Application Skeleton - Creating the framework classes


    (Page 3 of 7 )

    You will notice that you still have an empty workspace and that your project contains no classes. To remedy this, right click on the “MFCSample Classes” none under the ClassView tab in the Workspace window and click on the New Class… option.

    This will show the “New Class” dialog. In the Name field, type in MyClass and click on the OK button. This will add a new class to your application, which will contain an implementation file (MyClass.cpp) and a header file (MyClass.h). Click on the FileView tab of the workspace dialog and expand all nodes.

    The Workspace Dialog Containing Our “MyClass” Clas

    When you add a new class to any application in Visual C++, it likes to “junk up” each implementation and header file with the usual #if !defined … #endif directives. Double click on MyClass.cpp and delete all of the code in the main window. Do the same for the MyClass.h file. We don’t need this code!

    Now, in the code window for MyClass.h, type in the following:

    class CMainWin : public CFrameWnd

    {

    public:

    CMainWin();

    DECLARE_MESSAGE_MAP()

    };

    class CApp : public CWinApp

    {

    public:

    BOOL InitInstance();

    };


    As you can probably guess, we’ve just created two classes which are derived from two other classes: CframeWnd and CwinApp. These are MFC classes that are defined in afxwin.h (More on this file soon). The CApp class creates an instance of an application, while the CMainWin class creates an instance of a windowed dialog. That’s all you need to know about them for now.

    In the CMainWin class, we have publicly declared the constructor and the DECLARE_MESSAGE_MAP() macro. This is defined internally by MFC and will allow our application to send and accept messages to and from windows.

    All C++ applications are created around the simple concept of sending and posting messages. Let’s say that, for example, you click on a button in an application. Internally, windows will recognize this click because your application has sent windows a unique message identifier letting it know that the button was clicked. Windows responds by sending a message to your application letting it know that a certain button was clicked. Your application “catches” the message and calls a function to handle the click. If you’ve had some exposure to Visual Basic, then it’s similar to writing code in the Command1_Click() sub routine for the click of a button.

    Not all messages that windows sends to your application have to be responded to. If your app doesn’t respond to a message, then windows will create a default implementation for it. This is an important point, which you should try and remember.

    The last class definition in our MyClass.h header file, CApp, is derived from the MFC class CwinApp. The InitInstance() function is publicly overwritten and will contain the code which will be requested when the application is initially instantiated.

    More C++ Articles
    More By Mitchell Harper


       · Hi,Its a good article. I am new to MFC and learning it by reading MFC ebooks....
     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek