Logo

A Simple Window

 

Introduction

The Document/View architecture provides its own technique of creating a program. An application is made of two primary sections: The application class and the frame.

The application class allows you to notify the operating system that your application exists, that your application needs memory and that it may use some resources. Based on this, to start an application, you must create your own class and derive it from the MFC's CWinApp. Here is an example:

#include <afxwin.h>

class CExerciseApp : public CWinApp
{
};

If you have done Win32 programming before, you may know that each application works by creating an instance. In the same way, if you create a Document/View architecture, when the application comes up, it must create an instance. To support this, the CWinApp class is equipped with the Boolean InitInstance() method that you should implement in your application. Here is an example:

class CExerciseApp : public CWinApp
{
	BOOL InitInstance()
	{
		return TRUE;
	}
};

The frame allows you to show the borders and probably the contents of the (main) window of your application. To create a frame, you can derive a class from the MFC's CFrameWnd class and you should implement its (default) constructor. In the definition of the constructor of your class, you should call the Create() method that allows you to formally create a frame. Only two pieces of information are required to create a frame, the name of the class, which should be passed as NULL, and the name of the window, which is the string that will be shown on the title bar.

After defining the frame class,, you should instantiate in the InitInstance() method of your application.

 

Practical Learning Practical Learning: Creating a Simple Window

  1. Start Microsoft Visual C++ .NET
  2. Create a Win32 Project named MFCSimpleWindow
     
    New Project
  3. Click OK
  4. Create it as an Empty Project and a Windows Application
     
  5. Click Finish
  6. On the main menu, click Project -> MFCSimpleWindow Properties...
  7. In the Use of MFC combo box, select Use MFC in a Shared DLL
     
    Properties
  8. Click OK
  9. To create a new source file, on the main menu, click Project -> Add New Item...
  10. In the Templates section, click C++ File (.cpp)

  11. Set the Name as Exercise
     
    Add New Item
  12. Click Open
  13. In the empty file type:
     
    #include <afxwin.h>
    
    struct CMainFrame : public CFrameWnd
    {
    	CMainFrame()
    	{
    		Create(NULL, "Windows Application Tester");
    	}
    };
    
    struct CExerciseApp : public CWinApp
    {
    	BOOL InitInstance()
    	{
    		CMainFrame *Frame = new CMainFrame();
    		m_pMainWnd = Frame;
    
    		Frame->ShowWindow(SW_NORMAL);
    		Frame->UpdateWindow();
    
    		return TRUE;
    	}
    };
    
    CExerciseApp theApp;
  14. Test the application

 

 

Home Copyright © 2004-2005 FunctionX, Inc.