Win32 Application Steps
A standard Win32 windows program contains the following minimal steps:
- Define a Custom Window Class
- Define a WndProc() callback for this the window
- Register the Main Window Class
- Create the registered overlapped window
- Show and Update Window
- Wait for the user messages and dispatch to the window procedure
- Handle user required events in WndProc
MFC Application Steps
We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC framework. This is the advantage of using MFC framework as MFC implements most of the steps inside its framework.
MFC Classes
MFC is object oriented programming framework and most of the facilities are exposed as MFC classes. CWinApp is the class to take care WinMain and application initialization and running. Top level window display and management are done via CFrameWnd and CWnd classes.
CWinApp
MFC application should inherit CWinApp class and implement pure virtual function Initinstance. Application should define an application object in global space. MFC framework calls Initinstance using base class pointer of CWinApp from WinMain and programmer should override Initinstance. Initinstance is the callback event where programmer should define window class then register class and create top level window. Initinstance should return TRUE so that MFC framework can further process message queues and dispatch.
CFrameWnd
Programmer should create a Window object which is derived from CFrameWnd. CFrameWnd takes care of defining window class and registering it during construction. CFrameWnd provides Create() function to do window creation. Create() is a overloaded funstion and programmers need not to provide all arguments. It takes /mfc defaults if arguments are not given.
MFC Basic Steps
- Derive CWinApp and Implement Initinstance
- Define a global object of application class
- Derive CFrameWnd and declare a window class
- Allocate window object and assign to m_pMainWnd
- Call Create() or call Create() from window constructor
- Call ShowWindow() and UpdateWindow() and return true from Initinstance.
- CWinApp implements GetMessage and Dispatch message in MFC framework.
- CFrameWnd has built-in WindowProc to handle messages.
- All messages are handled by DefWindowProc of CFrameWnd
- User can declare additional message maps and handle events of their own.
Basic MFC Program with VC++
Create a new Win32 Application in Visual C++ editor. Name the project as "MyBasicMFC".
Select this project as "A Simple Win32 Application".
This wizard will create the project and C++ source and header file. Add this below code in the C++ source file (MyBasicMFC.CPP).
VC++ editor uses a precompiled header file called StdAfx.h. Add the include file afxwin.h replacing the line containing #include windows.h. Disable pre compiled header of MyBasicMFC.CPP file in case the above code is directly copied and replaced in the file.
This project uses MFC framework so we need to add the MFC library support to the application for the linker to resolve the symbols. Select "Use MFC in a Static Library" or "Use MFC in a Shared DLL".
Now compile and link the project. Once successful the program is okay to run.
Program output
About our authors: Team EQA
Further readings
Where is WinMain() function in MFC application ?
MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.
What is the utility of CWinApp class?
This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...
Basic steps in Win32 GUI Application with source code.
Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.
What is a Window CallBack procedure and what is its utility?
DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.
What are LPARAM and WPARAM in window proc function?
LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.
What are the basic steps of a typical MFC based application?
We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...