C#
  Home arrow C# arrow Page 4 - An Overview of MFC, Part 1
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
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#

An Overview of MFC, Part 1
By: Digvijay Chauhan
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2005-02-15

    Table of Contents:
  • An Overview of MFC, Part 1
  • Getting Started with MFC
  • What is MFC anyway?
  • Using the Windows Event Driven Model
  • Building our First Application

  • 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


    An Overview of MFC, Part 1 - Using the Windows Event Driven Model


    (Page 4 of 5 )

    When we use MFC we’re provided with placeholders, in the form of member functions of a class. These are invoked when an event occurs, and they are essentially the Event Handlers we’re just talking about.

    Say you made a Dialog and created a button for it. How do you handle the clicking of this button? When using C/SDK you’ll have to find a place in the big switch statement under the WM_COMMAND switch to write the handler after checking the wParams and the lParams. But in the MFC world you can make an entry in the MessageMap and implement your handler as a function of the class in which the control (Button) is created.

    If you’re not following everything, don’t worry. It will be clear in a little while.

    Understanding MFC Code

    The best way to see MFC in action is to understand the structure and style of a typical MFC program and see it running. I assume that you have a copy of Visual Studio or Visual Studio .NET to write MFC programs. The code listing below is a simple "hello world" program for MFC. If you’ve never seen any MFC code before it may seem cryptic to you, but you will find it very logical once you understand why is it that way.

    //helloworld.cpp
    #include <afxwin.h>

    // Declare the main application class derived from CWinApp
    class CHelloWorldApp : public CWinApp
    {
      public:
        virtual BOOL InitInstance();
    };

    // Create a Global instance of the application class
    CHelloWorldApp HelloWorldApp;

    // Declare the main window class derived from CFrameWnd
    class CMainWindow : public CFrameWnd
    {
        CStatic* cs;
      public:
        // Constructor
        CMainWindow ();
    };

    // The InitInstance function is called each
    // time the application first executes.
    BOOL CHelloWorldApp::InitInstance()
    {
      m_pMainWnd = new CMainWindow ();
      m_pMainWnd->ShowWindow(m_nCmdShow);
      m_pMainWnd->UpdateWindow();
      return TRUE;
    }

    // The constructor for the main window class
    CMainWindow:: CMainWindow ()
    {
      // Create the window itself
      Create(NULL,
        "Hello World App",
        WS_OVERLAPPEDWINDOW,
        CRect(0,0,200,200));

      // Create a static label
      cs = new CStatic();
      cs->Create("Hello, World",
          WS_CHILD|WS_VISIBLE|SS_CENTER,
          CRect(50,50,150,75),
          this);
    }

    This 15-20 line program does three things visibly:

    1. It creates an "application object." Each MFC program needs this in order to handle the initialization details of MFC and Windows.

    2. The application creates a single window on the screen to act as the main application window for the rest of the application session. If your application does not need user interface elements you may choose a different path, but you’ll do the processing in the constructor itself as shown above.

    3. Finally, on the main window created in step 2, the application also creates a single static control (like a label in Visual Basic) containing the text "Hello, World."

    In the next article we’ll explore these CWinApp and CFrameWnd in great detail -- details that you should know to become a good MFC programmer. But for now we must discuss how to compile and run this little application.

    More C# Articles
    More By Digvijay Chauhan


       · Hi I'm Digvijay ....... the author of this article.Please feel free to ask any...
       · This article should be filed under C++ and not under C#Regards,Anand...
     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





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