MFC OOP and WinMain
MFC applications manage applications in object oriented fashion. It hides WinMain in its framework. MFC library has source folder and WinMain() is located in VC98\MFC\SRC\APPMODUL.CPP. This part of the code gets compiled and statically linked to each and every application we write.
CWinApp Global Instance
CWinApp class maintains a global pointer in a static member. This object should be declared in global scope. This is constructed when other C++ global objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the Microsoft Foundation Class Library.
WinMain to InitInstance
Programmer derives an application class from CWinApp must override the InitInstance member function and must create application's main window object. InitInstance is a pure virtual function and it should be implemented otherwise linking error happens. MFC library implements WinMain() in APPMODULE.CPP and obtains global pointer using AfxGetApp() and then calls InitInstance() virtual function using base pointer of type CWinApp.
We have a demo source code to mimic MFC framework. It gives us the concept of how MFC implements WinMain and calls InitInstance(). It contains one header file and two C++ source file. To compile and build this demo create an empty Win32 Application (not console based) workspace and add these files.
- MyApp.CPP - Our demo application displaying
- afxwin.h - MFC header contains prototype of CWinApp
- APPMODULE.CPP - MFC demo implementation of CWinApp and winMain
About our authors: Team EQA
MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.
This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...
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.
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.
LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.
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 ...
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Draw on printer Memory leaks