"Visual C ++ MFC Concise Tutorial" ---- Part I: A simple MFC program

xiaoxiao2021-03-06  35

Part II: A simple MFC program In this will, we will study a place in the MFC app mentioned in the next place so that it can understand its structure and conceptual framework. We will introduce the MFC first, then describe how to use MFC to create an application. MFC introduction MFC is a large, extended C class hierarchy, which makes it easier to develop Windows applications. MFC is compatible throughout the Windows family, that is, whether Windows3.x, Windows95 is still Windows NT, and the MFC used is compatible. Whenever the new Windows version occurs, the MFC will be modified to make the old compiler and code work in a new system. The MFC also returns to the extension, adding new features, making it easier to build applications. In contrast to traditionally accessing the Windows API, the advantage of using MFC and C is that the MFC already contains and compresses all standard "model file" code, which is required for all WINDOWS programs written in C. Therefore, the program written in MFC is much smaller than the program written in C language. In addition, the performance of the program written by the MFC has no loss. If necessary, you can also call the standard C function directly because MFC does not modify the basic structure of the Windows program. The biggest advantage of using MFC is that it has made all the most difficult things. The MFC contains thousands of rows of correct, optimized, and powerful Windows code. Many member functions you call have completed the work you might hard. From this, MFC greatly accelerates your program development speed. MFC is very large. For example, approximately 200 different classes are included in version 4.0. Fortunately, you don't need to use all functions in a typical program. In fact, you may only need to use different categories in more than 10 MFCs to create a very beautiful program. This hierarchy can be divided into several different types: application framework graphic drawing Drawing object file service exception processing structure - List, Array and Map Internet Service OLE 2 Database Universal Class In this tutorial, we will focus on discussion Visual object. The following list gives some classes: COBJECT CCMDTARGET CWINTHREAD CWINAPP CSTATICEWND CDIALOG CVIEW CSTATIC CBUTTON Clistbox CCOMBOX CEDIT CSCROLLBAR ATV in the list, there are several points to pay attention. First, most of the classes in the MFC are inherited from the base class COBJECT. This class contains data members and member functions that most of the MFC classes. Second, it is the simplicity of the list. The CWINAPP class is to use in your creation of an application and use it once in any program. The CWND class brings together all common features, dialogs, and control in Windows. The CFRAMEWND class is inherited from CWND and implements a standard framework application. CDIALOG can handle two types of dialogs that have two types and models, respectively. CVIEW is used to allow users to access documents via windows. Finally, Windows supports six control types: Static text box, editable text box, buttons, scroll bars, list boxes, and combo boxes (an extension list box). Once you understand these, you can better understand the MFC. Other classes in the MFC implements other features such as memory management, document control, and more. In order to establish an MFC application, you must use these classes directly, and usually you need to inherit new classes from these classes. In inherited classes, you can create new member functions, which can be more applicable to your own needs.

You have seen this inheritance process in the simple example of the first lecture, and will be described in detail below. ChelloApp and Chellowindow are inherited from existing MFC classes. Designing a program before discussing the code itself, we need some workers to briefly describe the process of designs in the following MFC. For example, if you want to compose a program to display "Hello World" information to the user. This is of course very simple, but still needs some consideration. "Hello World" application first needs to create a window on the screen to display "Hello World". Then you need to put "Hello World" on the window. We need but objects to complete this task: an application object, used to initialize the application and hang it on Windows. The application object handles all lower level events. A window object comes as the main window. A static text object is used to display "Hello World". Each program created by MFC will contain two objects. The third object is for the application. Each application defines its own group of user interface objects to display input information for the application and collecting the application. Once you have completed the interface design and decide to implement the control required for this interface, you need to write code to create these controls on the screen. You will also write code to handle information generated by users to operate these controls. In the "Hello World" application, there is only one control. It is used to output "Hello World". Complex programs may require hundreds of controls in their main windows and dialogs. It should be noted that there are two different methods in the application to establish user control. What is introduced here is to establish control with a C code method. However, in a relatively large application, this method is not feasible. Therefore, the control is established in the usual case of the graphic editor of the resource file. This method is much easier. Understand the "Hello World" code lists the code you have entered, compiled, and running in the previous lecture. The addition line number is for convenience of discussion. Let's take a line to study it, you will better understand the way MFC builds the application. If you haven't compiled and run the code yet, you should do it on the way.

1 //hello.cpp 2 #include 3 // Declare the application class 4 class CHelloApp: public CWinApp 5 {6 public: 7 virtual BOOL InitInstance (); 8}; 9 // Create an instance of the application class 10 CHelloApp HelloApp ; 11 // Declare the main window class 12 class CHelloWindow: public CFrameWnd 13 {14 CStatic * cs; 15 public: 16 CHelloWindow (); 17}; 18 // The InitInstance function is called each 19 // time the application first executes . 20 BOOL CHelloApp :: InitInstance () 21 {22 m_pMainWnd = new CHelloWindow (); 23 m_pMainWnd-> ShowWindow (m_nCmdShow); 24 m_pMainWnd-> UpdateWindow (); 25 return TRUE; 26} 27 // The constructor for the window Class 28 Chellowindow :: CHELLOWINDOW () 29 {30 // Create The Window Itself 31 Create (Null, 32 "Hello World!", 33 WS_OVERLAPPEDWINDOW, 34 CRECT (0,0,200,200)); 35 // Create A Static Label 36 CS = New cstatic (); 37 cs-> Create ("Hello World", 38 WS_CHILD | WS_VISIBLE | SS_CENTER, 39 CRECT (50, 80, 150, 150), 40 this); 41} You look at the code above to get a whole impression. The procedure consists of six small parts, and every part is a very important role. First, the program contains the header file AFXWIN.H (line 2). The header file contains all types, classes, functions, and variables used in the MFC. It also includes other headers such as Windows API libraries. Articles 3 to 8 Row from the standard application class CWINAPP description of the MFC inherits new application class chelloApp. This new class is to overload the InitInstance member function in CWINAPP. InitInstance is an overloaded function to be called when an application starts execution. In Chain 10, an application is explained as a global variable. This example is important because it will affect the execution of the program. The establishment of the global variable will perform the default constructor of the CWINAPP class when the application is loaded into memory and start execution. This constructor automatically calls the initInstance function defined in the 18 to 26 row. In 11 to 17, the Chellowindow class is inherited from the CFrameWnd class in the MFC. Chellowindow is a window as an application on the screen. Establish a new class to implement constructor, destructuring functions, and data members. The 18th to 26 lines implements the initInstance function. This function generates an example of a CHELLOWINDOW class, thus executing the constructor of the category 27 to 41 lines. It also puts a new window on the screen. Section 27 to 41 implements the constructor of the window. This constructor is actually a window, and then establishes a static text control.

It should be noted that there is no main or winmain function in the program, and there is no event loop. However, we know that it also handles events from the previous lecture. Windows can maximize or minimize, mobile windows, and more. All of these operations are hidden in the primary application class CWINAPP, and we don't have to worry about its event, it is automated, which is invisible in the MFC. In the next section, the part of the program will be described in detail. You may not be able to fully understand very well: But you'd better finish it to get the first impression. In the next lecture, some special examples will be introduced, and the combination of each fragment will help you better understand. Each application established by the program object with MFC includes a single application object inherited from the CWINAPP class. This object must be explained to a global (line 10) and can only appear once in your program. Objects inherited from CWINAPP classes are mainly to process the application's initialization, while also processing the application main event loop. CWINAPP classes have several data members and several member functions. In the above program, we only overload a virtual function in CWINAPP initInstance. The purpose of the application object is to initialize and control your program. Because Windows allows multiple instances of the same application to be executed at the same time, the MFC divides the initialization process into two parts and uses two functions initApplication and InitInstance to process it. Here, we only use an initInstance function because our procedure is very simple. A new example will be called when the application is called. The code of the 3rd to 8th line has established a class called ChelloApp, which is inherited from cwinapp. It contains a new InitInstance function, which is overloaded from the existing functions existing in CWINAPP: 3 // Declare the Application Class 4 Class ChelloApp: Public CWINAPP 5 {6 public: 7 Virtual Bool InitInstance ); 8}; in the overloaded InitInstance function, the program uses ChelloApp's data member m_pmainwnd to create and display window: 18 // Time The Application First Executes. 20 BOOL CHelloApp :: InitInstance () 21 {22 m_pMainWnd = new CHelloWindow (); 23 m_pMainWnd-> ShowWindow (m_nCmdShow); 24 m_pMainWnd-> UpdateWindow (); 25 return TRUE; 26} InitInstance function returns TRUE if the initialization has succeeded carry out. If false returns, it indicates that the application will terminate immediately. In the next section we will see the detailed process of the window initialization. When the application object is established at the 10th line, its data member (inherited from CWINAPP) will automatically initialize. For example, m_pszappname, m_lpcmdline, and m_ncmdshow include appropriate initialization values. You can see the MFC's help file to get more detailed information. We will use one of these variables. Window object MFC defines two types of windows: 1) Frame window, it is a full-featured window that can change the size, minimize, maximize, etc.; 2) dialog window, it cannot change the size. The frame window is a typical primary application window.

In the code below, inheriting a new class CHELLOWDOW: 11 // declare the main window Class 12 CFRAMEWND: public cframeWnd 13 {14 CSTATIC * CS; 15 public: 16 chellowindow (); 17}; it Includes a new constructor, and there is a data member that pointing the unique user interface control used in the program. Each application you create will have a unique set of controls in the main window. Therefore, inheritance classes will have an overloaded constructor to create all the controls needed by the main window. In a typical case, this class will contain a destructor to delete them when the window is closed. We did not use the destructor here. In the fourth lecture, we will see that the inheritance window will also explain a message processing function to handle the messages generated by the user event. Typically, an application will have a primary application window. Therefore, the CHELLOAPP application class defines a named m_pmainwnd member variable to point to the main window. In order to establish the main window of the program, the initInstance function (line 18 to 26) establishes a checkowindow case and uses m_pmainwnd to point to a new window. Our CHelloWindow object is established on line 22: 18 // The InitInstance function is called each 19 // time the application first executes 20 BOOL CHelloApp :: InitInstance () 21 {22 m_pMainWnd = new CHelloWindow (); 23 m_pMainWnd. -> ShowWindow (M_ncmdshow); 24 m_pmainwnd-> UpdateWindow (); 25 return true; 26} Only a simple frame window is not enough. Also make sure the window can appear correctly on the screen. First, the code must call the window's showWindow function to appear on the screen (line 23). Second, the program must call the UpdateWindow function to make sure that each of the controls and outputs in the window can appear on the screen (line 24). You may be strange, the showWindow and UpdateWindow functions are defined. For example, if you want to view to understand them, you might want to see the CFraMewnd definition section in the MFC's help file. However, these member functions are not included in the cframewnd. CframeWnd is inherited from the CWND class. You can view the CWnd in the MFC document and you will find that it contains more than 200 different member functions. Obviously, you can't grasp these functions in a few minutes, but you can master a few of them, such as ShowWindow and UpdateWindow. Now let's take a few minutes to see the CWnd :: ShowWindow function in the MFC help file. To do this, you can click the Search button in the help file and enter "showwindow". After you found it, you will notice that ShowWindow has only one parameter, you can set different parameter values. We set it into the data member variable M_ncmdshow (line 23) of ChelloApp in our program. The m_ncmdshow variable is used to initialize the application started window display mode. For example, the user may launch an application in the Program Manager, and can tell the Program Properties dialog to tell the Manager application to maintain a minimize state when starting.

The m_ncmdshow variable will be set to sw_showminimized, and the application is started in the form of an icon, that is, after the program is started, it is an icon representing the program. M_ncmdshow variables are a way to communicate with the application. If you like, you can use different m_ncmdshow values ​​to test the effect of ShowWindow. But to recompile the program to see the effect. Chapter 22 is the initialization window. It assigns memory to call the New function. At this point, the program calls the CHELLOWINDOW constructor when executed. This constructor is called when the case is assigned each time the case is assigned. In the interior of the window constructor, the window must establish itself. It is achieved by calling CFrameWnd's Create member function (31st line): 27 // THE constructor for the window Class 28 Chellowindow :: Chellowindow () 29 {30 // Create The Window Itself 31 Create (NULL, 32 " Hello World! ", 33 WS_OVERLAPPEDWINDOW, 34 CRECT (0,0,200,200)); establish a function of four parameters. By viewing the MFC document, you can learn about different types. NULL parameters represent the use of default class names. The second parameter is a title that appears on the window title bar. The third parameter is the type attribute of the window. This program uses normal, overwritable types of windows. Type properties will be described in detail in the next lecture. The fourth parameter indicates that the window should be placed on the screen and the upper left corner (0,0), the initialization size is 200 × 200 pixels. If you use RectDefault, Windows will automatically place the window and size for you. Because our program is too simple, it only creates a static text control in the window. See Sections 35 to 40. The following will be described in detail below. The static text control program describes a member type CSTATIC and its constructor when inheriting the CHELLOWIDOW class from the CFRAMEWND class. As seen in the previous, the chellowindow constructor mainly do two things. The first is to establish a window of the application by calling the Create function (line 31). Then allocate and establish controls that belong to the window. In our program, only one control is used. Building an object in the MFC will always take two steps. The first is to allocate memory for the class, and then call the constructor to initialize the variable. Next, call the CREATE function to actually create an object on the screen. Code uses these two steps to assign, construct and create a static text object (36-40 line): 27 // THE constructor for the window class 28 checeowindow :: Chellowindow () 29 {30 // Create the window Itself 31 CREATE (NULL, 32 "Hello World!", 33 WS_OVERLAPPEDWINDOW, 34 CRECT (0,0,200,200)); 35 // Create a static label 36 cs = new cstatic (); 37 cs-> create ("Hello World", 38 WS_CHILD | WS_Visible | SS_CENTER, 39 CRECT (50, 80, 150, 150), 40 this); 41} The CSTATIC constructor is called to assign memory, and then call the CREATE function to create a CSTATIC control window. The parameters used by the Create function are similar to the parameters used by the window establishment function (line 31). The first parameter specifies the text content you want to display in the control. The second parameter specifies the type attribute. Type properties will be described in detail in the next lecture.

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

New Post(0)