Preliminary understanding "single document program framework" (1)

zhaozj2021-02-16  56

Preliminary understanding "single document program framework" (1)

People who beginhand VC feel headaches, especially for the program framework generated by MFC AppWizard. Indeed, those weird code are enough for you to watch. In fact, it is necessary to say simple, you can do not pay attention to those code, just join your legal code in need, you can make your own application applet. However, if you don't understand the code creating the wizard, it is difficult to make a larger program. Because you don't really understand the Windows programming mechanism, you can't grasp the essence of MFC programming. So, if you plan to continue to use the MFC to develop a Windows application, you still read the code that is creating it, and you will be slowly grasped as the practical practice.

In fact, I have learned VC soon, it is not deep enough to understand - I can only make "preliminary understanding" (both notes). This time I posted it, I hope to discuss with beginners, and I hope to get a guidance of a high person. In short, co-improvement is the purpose of our study.

In addition, one of the reason why the code looks strange is to use the Hungarian Name Rules, and it is used to it. If you don't know the Hungarian Naming Rules, please refer to the relevant books.

The project is mainly included in the project: application, main framework, document class, view class, etc. The declarations and definitions of each class are located in .h and .cpp files. Let us first look at the application class.

Open the VC, generate the default "single document" application with MFC AppWizard (.exe). When I was established, the project was named WL, and the remaining steps were default.

Open the WL.h file after completion, you will see the following. Chinese Note is that I have went, I hope to help everyone understand the code.

// wl.h: main header file for the WL Application

// This is the header of the application, mainly for declaring variables and functions.

#if! defined (AFX_WL_H__5DDF8EDB_C6DA_11D5_B729_00E04C69899E__included_)

#define AFX_WL_H__5DDF8EDB_C6DA_11D5_B729_00E04C69899E__included_

#iF _MSC_VER> 1000

#pragma overce

#ENDIF / / 100 m _ _ _

#ifndef __afxwin_h__

#error include 'stdafx.h' Before Including this file for pch

#ENDIF

#include "resource.h" // main symbols

/

// CWLAPP:

// See Wl.cpp for the importation of this class

/ / The project name just typed is evolved into the application class name of this project, both CWLAPP

Class CWLAPP: Public CWINApp // Declare the application class of this program, derived from CWINAPP

{

PUBLIC:

CWLAPP ();

// The following is the virtual function overload statement, automatically manages system, generally no modification

// Overrides

// ClassWizard Generated Virtual Function Overrides

// {{AFX_VIRTUAL (CWLAP)

PUBLIC:

Virtual Bool InitInstance ();

//}} AFX_VIRTUAL

// The following is the message mapping declaration made by the wizard. Want to understand the role, see the Windows message mapping mechanism.

// Implementation

// {{AFX_MSG (CWLAP)

AFX_MSG void onappout (); // Note - The ClassWizard Will Add and Remove Member Functions Here.

// Do Not Edit What You See in these Blocks of generated code!

//}} AFX_MSG

Declare_message_map ()

}

/

// {{AFX_INSERT_LOCATION}}

// Microsoft Visual C Will Insert Additional Declarations Immediate Line.

#ENDIF /! Defined (AFX_WL_H__5DDF8EDB_C6DA_11D5_B729_00E04C69899E__included_)

Let's take a look at the WL.cpp file, it is located in Source File:

// wl.cpp: defines the class behaviors for the application.

// The following contains the header files required to run the application class.

#include "stdafx.h"

#include "wl.h"

#include "mainfrm.h"

#include "WLDOC.H"

#include "wlview.h"

#ifdef _Debug

#define new debug_new

#undef this_file

Static char this_file [] = __file__;

#ENDIF

/

// CWLAPP

// Start message mapping

Begin_MESSAGE_MAP (CWLAPP, CWINAPP)

// {{AFX_MSG_MAP (CWLAPP)

ON_COMMAND (ID_APP_ABOUT, ONAPPABOUT) // "About" message mapping in the Help menu

// Note - The Classwizard Will Add and Remove Mapping Macros Here.

// Do Not Edit What You See in these Blocks of generated code!

//}} AFX_MSG_MAP

// Standard File Based Document Commands

"New" message mapping in on_command (id_file_new, cwinapp :: onfilenew) // "File" menu

ON_COMMAND (ID_FILE_OPEN, CWINAPP :: OnfileOpen) // "Open" message mapping in the "File" menu

// Standard Print Setup Command

ON_COMMAND (ID_FILE_PRINT_SETUP, CWINAPP: ONFILEPRINTSETUP) // "Print Settings" message mapping in the "File" menu

END_MESSAGE_MAP ()

// End message mapping

/

// Cwlapp Construction

CWLAPP :: CWLAPP () // Define constructor, you can join your code, use it when initialization

{

// Todo: Add Construction Code Here,

// Place All Significant Initialization in InIntinstance

}

// the one and only cwlapp object

CWLAPP theApp; // Defines the object of the application class. Note that this is a global object, and the program is automatically founded automatically when running.

/

// Cwlapp Initialization

Bool cwlapp :: InitInstance () // The virtual function is overloaded, and the system calls it to complete the initialization work when running the program. {

AFXENABLECONTROLCONTAINER ();

// Standard Initialization

// if you are not usning these features and hope to reduce the size

// of Your Final Executable, You Should Remove from the FOLLOWING

// the specificin itization routines you do not need.

#ifdef _AFXDLL

Enable3dControls (); // Call thisime by the sale mfc in a shared dll

#ELSE / / Allow Dynamic 3D Controls

Enable3dControlsStatic (); // call thisime1 linking to mfc staticly

#ENDIF / / Allows the use of static 3D controls

// Change The Registry Key Under Which Our Settings Are Stored.

// Todo: you shop modify this string to becomething appropriate

// Such as the name of your company or Organization.

// The default registry related operation.

SetRegistryKey (_T ("Local Appwizard-Generated Applications));

LoadStdprofileSettings (); // load Standard Ini File Options (Including MRU)

// register the application's document templates. Document Templates

// Serve As The Connection Between Documents, Frame Windows And Views.

CSINGLEDOCTEMPLATE * PDOCTEMPLATE; // Single document template pointer definition

PDOCTemplate = New CSINGLEDOCTEMPLATE (// New Single Document Application Template

IDR_MAINFRAME,

Runtime_class (cwldoc),

Runtime_class (cmainframe), // run the macro definition of the class, monitors the information of this object and its base class during the program operation

Runtime_class (cWLVIEW));

AddDDoCTemplate (PDOCTemplate); // Add a new template to the application

// Parse Command Line for Standard Shell Commands, DDE, File Open

CCommandLineInfo cmdinfo;

ParseCommandLine (cmdinfo); // Save the command line parameter into the object cmdinfo.

// Dispatch Commands Specified on The Command Line

IF (! ProcessShellcommand (cmdinfo))

Return False;

// The one and only window has been initialized, so show and update it.

// Initialization of the main window

m_pmainwnd-> showwindow;

m_pmainwnd-> UpdateWindow ();

Return True;

}

/

// Caboutdlg Dialog Used for App About

/ / When you click on the "About" menu item, the dialog will pop up, it is derived from base class CDialog

Class Caboutdlg: Public CDIALOG

{

PUBLIC:

Caboutdlg ();

// Dialog Data

// {{AFX_DATA (Caboutdlg)

Enum {IDD = IDD_ABOUTBOX}; // "About" menu item default id identity

//}} AFX_DATA

// ClassWizard Generated Virtual Function Overrides

// {{AFX_VIRTUAL (caboutdlg)

protected:

Virtual Void DodataExchange (CDataExchange * PDX); // DDX / DDV Support

//}} AFX_VIRTUAL

// Implementation

protected:

// {{AFX_MSG (caboutdlg)

// no message Handlers

//}} AFX_MSG

Declare_message_map ()

}

Caboutdlg :: Caboutdlg (): cdialog (Caboutdlg :: IDD)

{

// {{AFX_DATA_INIT (Caboutdlg)

//}} AFX_DATA_INIT

}

Void Caboutdlg :: DODATAEXCHANGE (CDataExchange * PDX)

{

CDIALOG :: DODATAEXCHANGE (PDX);

// {{AFX_DATA_MAP (Caboutdlg)

//}} AFX_DATA_MAP

}

Begin_Message_Map (Caboutdlg, CDialog) // Message Map

// {{AFX_MSG_MAP (Caboutdlg)

// no message Handlers

//}} AFX_MSG_MAP

END_MESSAGE_MAP ()

// app command to run the dialog

Void cwlapp :: onappaBout () // User Click on "About" Tune this function

{

Caboutdlg Aboutdlg;

Aboutdlg.Domodal (); // Domodal () is the most commonly used function of the Mode dialog box, which is responsible for the display and termination of the dialog.

}

/

// Cwlapp Message Handlers

The code is briefly commented here, and the functions and some mechanisms can be found in MSDN (very good things). There is also a common problem, in the above code, I don't see the WinMain () function required for the Windows program runtime, because it is packaged in the box, which is transparent to the user, you can't afford it. If you want to know the running mechanism of the WinMain () function, you can refer to other Windows programming books. I can recommend two: "Windows Programming" and "Windows API Reference Manual" can more understand the formation mechanism.

An engineering "application class" is the most basic class, which completed the initialization of the application and other preparations, which is the premise of the normal operation of the program. Therefore, only simple understanding is not enough, but also to understand other comprehensively understanding can truly understand the mystery, all of which requires us to work hard in practice. We must remember this truth: practice is the only effective way to improve the level of programming.

Welcome to exchange, guide: ml457@163.com

转载请注明原文地址:https://www.9cbs.com/read-28112.html

New Post(0)