[MFC Windows programming (version 2)] Chapter 1 code comments.

Source: Internet
Author: User
/* CMyApp inherits the CWinApp class, so it is an application object. The CWinApp class provides message loop and scheduling. An MFC application can have only one application object. This object must be valid in the global declaration. */Class CMyApp: public CWinApp {public: virtual BOOL InitInstance ();/* The above function is overwritten by CWinApp. CWinApp itself has this function, our class inherits this function */} Here; class CMainWindow: public CFrameWnd {public: CMainWindow (); protected: afx_msg void OnPaint (); DECLARE_MESSAGE_MAP ();};

Hello. h later

Hello. cpp labels:

# Include <afxwin. h> # include "Hello. h "CMyApp myApp; // see it, combined with the header file comment/* myApp is the instance of CMyApp, and this instance must be globally valid, therefore, we declare it a global variable. */BOOL CMyApp: InitInstance () {/* this function is just mentioned. The specific implementation of the function inherited from CWinApp. Virtual functions ). Please note! This function is owned by the CWinApp itself. Because of the inheritance relationship, the CMyApp has this function. This function is called very early. It can be seen that the following operations are all window operations after the application starts and the window is created. Pdf 16, Page 35/1187 in fact, the application will not create a window unless the InitInstance creates a window. This is why even the smallest MFC application must derive a class from the CWinApp class and override the function CWinApp: InitInstance. In CWinApp (not CMyApp here !) InitInstance is a virtual function. Only return TRUE. The purpose of this function is to give the application an opportunity for initialization. The value returned by this function determines the next action of the application framework. If this function returns FALSE, the application will be closed. If the initialization is normal, TRUE is returned and the application continues to execute. This function is the place where Initialization is executed. Here, this job is to create a window. */M_pMainWnd = new CMainWindow;/* Here, instantiate the CMainWindow class to create a window. Here, a CMainWindow object is created to copy its address to the m_pMainWnd data member of the Application object. After the window is created, InitInstance will call ShowWindow and UpdateWindow through the CMainWindow pointer to display it. * // M_pMainWnd-> ShowWindow (argument lists ....); // m_pMainWnd-> UpdateWindow (argument lists ....); /* ShowWindow and UpdateWindow are the CWnd member functions shared by all window objects. CFrameWnd is included, and CMainWindow is derived from CFrameWnd. These functions are almost packaged for API functions with the same name. * // * If you want to call a win api function in an MFC application, add the ":" identifier before the function name. For example, UpdateWindow (argument lists...) ensures that API functions can be called even if the object has a function with the same name. This is the case for calls in other codes. * // * ShowWindow only accepts one parameter, which indicates the window display condition, whether to maximize, or minimize or not. M_nCmdShow is passed directly here according to the provisions in the Windows App design protocol. m_nCmdShow here is usually done by SW_SHOWNORMAL. */M_pMainWnd-> ShowWindow (m_nCmdShow);/* UpdateWindow is used to repaint the window to start the job of ShowWindow. */M_pMainWnd-> UpdateWindow ();/* after completion, InitInstance returns TRUE to allow the application to continue execution. */Return TRUE;} BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) ON_WM_PAINT () END_MESSAGE_MAP () CMainWindow: CMainWindow () {/* here, the Framework Window creates a window object. The method is to call the Create FUNCTION. The _ T function converts the string to neutral. Create is a CMainWindow member function inherited by CFrameWnd. In CFrameWnd, there are about 20 functions defined by it and inherited from CWnd. Create is one of them. For the prototype of CFrameWnd: Create, see P19. */Create (NULL, _ T ("The Hello Application") ;};/* this function is responsible for responding to The WM_PAINT message. For details, see p21-v/void CMainWindow: OnPaint () {CPaintDC dc (this); CRect rect; GetClientRect (& rect); dc. drawText (_ T ("Hello, MFC"),-1, & rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER );}

Related Article

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.