Introduction to VS2013/MFC Programming (MFC Application Framework Analysis) __ Programming

Source: Internet
Author: User
Tags message queue

The previous lecture was about the composition of the files in the VS2013 application project. This section provides an analysis of the running process of the MFC application framework.

I. SDK application vs. MFC application running Process

Program to run all have the import function, in the previous C + + tutorial is the main function, and the Windows application of the entry function is WinMain function, MFC program is also starting from the WinMain function. Here's a "HelloWorld" program written in the Windows SDK that compares the application framework to better understand how the framework works. Windows SDK Development Program is not using MFC class libraries, directly using Windows API functions for software development. This talk is not to explain the SDK development, just for comparison and simple introduction, as for the SDK development can be learned after MFC to choose whether to study, generally have a simple understanding on it.

SDK Application

       first, give the Windows SDK application "HelloWorld" source:   C + + code #include  < windows.h>        Lresult callback mywndproc (hwnd hwindow,  Uint msg, wparam wparam, lparam lparam);          Int winapi winmain (hinstance hinstance, hinstance  hprevinstance, pstr szcmdline, int icmdshow)        {          const static tchar appname[] = text ("hello  World ");         WNDCLASSEX myWin;          mywin.cbsize = sizeof (Mywin);         mywin.style =  CS_HREDRAW | CS_VREDRAW;         mywin.lpfnwndproc =  myWndProc;       &NBSP;&Nbsp;mywin.cbclsextra = 0;         myWin.cbWndExtra = 0;          myWin.hInstance = hInstance;         myWin.hIcon = 0;         mywin.hiconsm  =  0;         myWin.hCursor = 0;         myWin.hbrBackground =  (Hbrush) (color_window + 1);          myWin.lpszMenuName = 0;          mywin.lpszclassname = appname;         //Register          if  (! RegisterClassEx (&mywin))  return 0;         const hwnd  hwindow = createwindow (           appname,           appName,            WS_OVERLAPPEDWINDOW,           cw_usedefault,            CW_USEDEFAULT,            CW_USEDEFAULT,           CW_USEDEFAULT,            0,           0,            hInstance,            0);         showwindow (hwindow,icmdshow);         updatewindow (Hwindow);         {           MSG msg;           while (GetMessage) (& Amp; msg,0,0,0))            {              translatemessage (&msg);              dispatchmessage (&msg);           }            return  (int) msg.wparam;          }      }             lresult  Callback mywndproc (hwnd hwindow, uint msg, wparam wparam, lparam  LParam)        {         if  (msg==wm_paint)           {           paintstruct ps ;           const hdc hdc = beginpaint (HWindow, &AMP;PS);           RECT rect;            getclientrect (hwindow,&rect);            DrawText (Hdc,text ("Hello world"), -1,&rect, dt_singleline | dt_center | dt_ vcenter);           endpaint (hwindow,&ps);           return 0;         }          else if  (Msg==wm_destroy)          {            postquitmessage (0);            return 0;         }         return defwindowproc (Hwindow,msg,wparam,lparam);      }  

The procedure above runs is: Enter WinMain function-> initialize wndclassex, call registerclassex Function Register window class-> call ShowWindow and UpdateWindow function to display and update Windows-> Enter the message loop. About the message loop simply put, Windows applications are message-driven, when a system or user sends a message to an application for an operation or completes a task, enters the program's message queue, and then the message loop takes the message out of the message queue and gives it to the appropriate window process. The window procedure function of this program is the Mywndproc function, which completes an operation or task when the window process function finishes processing the message. This example shows the "Hello World" string, the UpdateWindow function sends a WM_PAINT message, but the message is sent directly to the window process instead of the message queue, and the "Hello World" string is eventually drawn in the window procedure function.

MFC applications

The following is an MFC application run process that is parsed through code in the MFC library:

       first defines the global object Theapp:chelloworldapp Theapp in HelloWorld.cpp;. After calling the CWinApp and Chelloworldapp constructors, enter the WinMain function (located in Appmodul.cpp). C + + code extern  "C"  int winapi    _tWinMain (hinstance hinstance, hinstance  hPrevInstance,        _in_ lptstr lpcmdline, int ncmdshow)    #pragma  warning (suppress: 4985)     {       //  call shared/exported WinMain        return afxwinmain ( Hinstance, hprevinstance, lpcmdline, ncmdshow);   }  

        in TCHAR.h, there is this definition: #define _twinmain   winmain, so here's the _ Twinmain is the WinMain function. It calls the Afxwinmain function (located in WinMain.cpp). C + + code Int afxapi afxwinmain (hinstance hinstance, hinstance hprevinstance,lptstr  lpcmdline, int ncmdshow)    {             ............. Slightly           // App global initializations  (rare )            if  (papp != null &&  !papp->initapplication ())                   goto InitFailure;      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.