EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

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.

Program output

MFC basic application with user event

Source Code

#include <windows.h>
#include <afxwin.h>
class CMyWnd : public CFrameWnd
{
  public:
    CMyWnd()
    {
10        Create(NULL, _T("My MFC Window"));
11      }
12      afx_msg void CMyWnd::OnClose();
13 
14      DECLARE_MESSAGE_MAP()
15  };
16  BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd)
17  ON_WM_CLOSE()
18  END_MESSAGE_MAP()
19 
20  afx_msg void CMyWnd::OnClose()
21  {
22    if(MessageBox ("Want to exit from this application?", "Exit", MB_YESNO) == IDYES)
23    {
24       PostQuitMessage(0);
25    }
26  }
27   
28  class CMyApp : public  CWinApp 
29  {
30 
31    public:
32       virtual BOOL InitInstance()
33      {
34        m_pMainWnd = new CMyWnd();
35        m_pMainWnd->ShowWindow(SW_SHOW);
36        m_pMainWnd->UpdateWindow();
37        return TRUE;
38      }
39   };
40  CMyApp app;

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sun 27 Apr/2014 03:43:08(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.