HOME
Login
Change Info
Logout


TUTORIALS

C, C++
Win32
Java
Visual Basic
MFC
DCOM
Networking
C#
Perl
HTML
XML
ASP
PHP
Javascript
Other

DOWNLOADS
ITCLib
SourceVizor meets the notification, reporting, and admin needs of teams using Microsoft Visual SourceSafe.
Free 30-day trial!


Creating a Simple MFC Program

by Alan Oursland

Questions, comments, suggestions - We want to hear from you!Comment on this article Get the Acrobat PDF File For This Article (31 KB) Get Adobe Acrobat Reader
Most downloads under 500KB


Introduction

MFC was created to make programming in Windows easier. As an object-oriented wrapper for Win32, it automates many routine programming tasks (mostly passing references around). Paradigms like the document/view architecture were added to automate even more tasks for the programmer, but in the process, some control was taken away.

This tutorial will show you how to create a very simple MFC program and how to customize it for your application. This program will be a base for other tutorials that I write for OpenGL and DirectX. I will be going through this tutorial in Visual C++ 6.0.

We will begin by creating a standard MFC Doc/View application using AppWizard. Select New from the File menu, click on the Projects tab, and select MFC AppWizard (exe). Enter SimpleMFC as the project name and press OK.

  1. Select Single Document and press Next.

  2. Select None for database support and press Next.

  3. Select None for compound document support and remove support for ActiveX controls. Press Next.

  4. Deselect all of the check boxes in Step 4. Press Next.

  5. Generate comments and use a shared DLL. Press Next.

  6. Press Finish.

We now have a standard Doc/View application with the following classes:

CAboutDlg
CMainFrame
CSimpleMFCApp
CSimpleMFCDoc
CSimpleMFCView

Go to the file view and remove the following files from the project and delete them from the directory:

SimpleMFCView.h
SimpleMFCView.cpp
SimpleMFCDoc.h
SimpleMFCDoc.cpp

You can also remove IDR_SIMPLETYPE from the icon resource tab and delete the associated file SimpleMFCDoc.ico (be sure to do it in that order).

We now have a project that doesn't compile.

Edit SimpleMFC.cpp:

  • Remove the #includes for SimpleMFCView.h and SimpleMFCDoc.h.

  • Edit the constructor to look like this:
  • 
    CSimpleMFCApp::CSimpleMFCApp()
    {
       m_pMainWnd = NULL;
       // Place all significant
       // initialization in InitInstance
    }
    
    
  • Edit initInstance to look like this:
  • 
    BOOL CSimpleMFCApp::InitInstance()
    {
       // Standard initialization
       // If you are not using these features
       // and wish to reduce the size
       // of your final executable, you should
       // remove from the following
       // the specific initialization routines
       // you do not need.
    
    #ifdef _AFXDLL
       Enable3dControls();
       // Call this when using MFC in a shared DLL
    #else
       Enable3dControlsStatic();
       // Call this when linking to MFC statically
    #endif
    
       // The one and only window has been initialized,
       // so show and update it.
       m_pMainWnd = new CMainFrame();
       m_pMainWnd->ShowWindow(SW_SHOW);
       m_pMainWnd->UpdateWindow();
    
       return TRUE;
    }
    
    
  • Remove everything below InitInstance. This includes CAboutDlg and the code to bring up the About dialog. (If you would like to leave the About dialog in, feel free to do so. You will be able to activate it later.)

  • Remove the items in the message map at the top of the file. This section should look like this:
  • 
    BEGIN_MESSAGE_MAP(CSimpleMFCApp, CWinApp)
       //{{AFX_MSG_MAP(CSimpleMFCApp)
          // NOTE - the ClassWizard will add
          // and remove mapping macros here.
          // DO NOT EDIT what you see in these
          // blocks of generated code!
       //}}AFX_MSG_MAP
       // Standard file based document commands
    END_MESSAGE_MAP()
    
    

Remove the definition for OnAppAbout from SimpleMFC.h.

We still have a program that doesn't compile. CMainFrame is expecting to be created dynamically within the Doc/View architecture, thus its constructor is protected. We will fix that.

Edit MainFrm.h:

  • Remove "DECLARE_DYNCREATE(CMainFrame)"

  • Change "protected: // create from serialization only" to "public:"

Edit MainFrm.cpp:

  • Remove "IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)".

  • Edit the constructor to look like this:
  • 
    CMainFrame::CMainFrame()
    {
        RECT r;
        r.left = 100;
        r.top = 100;
        r.right = 200;
        r.bottom = 200;
    
        Create(NULL, "Simple MFC App",
            WS_POPUP|WS_THICKFRAME,
            r, NULL, NULL, 0, NULL );
    }
    
    

If you run the program, a window with a border will appear. It will not have a title or menu bar. This is about the simplest Windows application you can make. Press Alt-F4 and Windows will send a WM_CLOSE message to the application to close it.

Let's take a look at what is going on here. CSimpleMFCApp is your application derived from CWinApp. CWinApp contains a pointer to the main application window. We have a global instance of CSimpleMFCApp. When it is created it instantiates an instance of our CMainFrame and assigns it as the CWinApp main window. It then displays the window. This is all that the application has to do. We will not be modifying it further.

CMainFrame creates the actual internal system window. Remember that MFC is just a wrapper for Win32. CMainframe, which is derived from CFrameWnd, is not actually an MS-Windows window. It just manages the handle to a window that MS-Windows creates. (The fact that the product is named Windows and the objects themselves are called "windows" may make this confusing.)

In the Doc/View architecture, the window handle is created "dynamically" (which basically means the base class creates it in a predefined way). Since we are not using the Doc/View architecture, and we want more control over the window, we create the window handle ourselves by calling CFrameWnd::Create in the CFrameWnd constructor. CFrameWnd::Create eventually calls the Win32 create, but it manages the window handle for us. Create takes several parameters which are the key to customizing this window.


BOOL Create(   LPCTSTR lpszClassName,
               LPCTSTR lpszWindowName,
               DWORD dwStyle = WS_OVERLAPPEDWINDOW,
               const RECT& rect = rectDefault,
               CWnd* pParentWnd = NULL,
               LPCTSTR lpszMenuName = NULL,
               DWORD dwExStyle = 0,
               CCreateContext* pContext = NULL );

  • lpszClassName - This is a string that describes the "window class" that you want to use. The window class tells Windows if this window will be a frame, or a button, or a scrollbar, or whatever. You can also define your own classes. This value can not be changed once the window has been created. We pass in NULL to use the default window class for a CFrameWnd.

  • lpszWindowName - This is the name of the window. If the window has a title bar, it will display this string.

  • dwStyle - This is probably the most important parameter in defining how your window will look. But don't worry, you can always change the styles once the window is created. We'll play with some different styles later on. The styles we use create a basic top level (popup) window with a resizable border.

  • rect - This defines the size and position of the window on the screen.

  • pParentWnd - This is the parent of the window you are creating. Since we are creating a top level window, we have no parent.

  • lpszMenuName - This tells the application which resource to use for the menu. If you have a menu resource defined you can pass in "#128", where 128 is the resource id of your menu. (128 should be the resource id of the menu in this particular example.)

  • dwExStyle - These are extended styles and are very useful in further controlling the appearance of your window. You can set these at any time after the window has been created.

  • pContext - This is used in the Doc/View architecture. Just pass in NULL for this parameter.

That's about it. Once the window is created, it uses a function inside MFC for the message handling, so you don't need to do anything there. In the next section, we'll play around with this window, so you can customize it to suit your needs.

Previous Page
Return to beginning of article

Next Page


Featured Article

An Introduction to C#
By Joey Mingrone

Register Today!


100% FREE

Members enjoy these benefits:
Access to ITI Downloads
Access to more articles and tutorials
Optional weekly newsletter
And more...

Click here to register
Or
Click here to log in



© 2010 Interface Technologies, Inc. All Rights Reserved
Questions or Comments? devcentral AT iticentral DOT com
PRIVACY POLICY