Contact VC 3: 1

zhaozj2021-02-16  49

Part III: MFC Based on Dialog Box Program

Recently, I am hobbing, putting a set of Visual Studio.net (but piracy, Microsoft is quite tragic. But if it is genuine, then the tragic will not be Microsoft, but I am. No money). To install 2213m, the three drives on the hard disk are less than 2G. However, the interface is quite beautiful, and it is powerful, it is worthy of comfort. I can finally see the base class of class, as well as the base class's implementation code. Not only that, but the best is that all VC base articles on the included MSDN are Chinese, translated than the hope of the Publishing House. What documents, box plus window, multi-view, there should be all. So I suggest that everyone will use .Net, pay attention to the same three of the seven plates, the Beta version, the VC function is incomplete.

This part should say the specific program of MFC. Because I use .NET, the code may be slightly different from 6.0, but it is not critical, and it does not hinder the overall structure. I will also be careful about code compatibility.

Ok, pick up the VC on your hand. Let me look at all code based on dialog program.

If you are a friend of 6.0, first select New, select the MFC AppWizard in the Project tab, and name Dialog in the project name, press OK. Select a dialog (Dialog Based) in the first step of the wizard, you can press it directly.

If you are .NET friends, select New> project on the menu, select the Visual C project in the project type, select the MFC application in the template, enter Dialog in the name, press OK. Select the dialog based on the application type, then press.

So a dialog program is done. For the first time I use MFC's friends, I will definitely. I have been programmed from zero for a long time, and I may not be used to generating a code for us. "The first image is chaos", this is my classmate to my answer. It doesn't matter, we can see and understand the code that VC gives us a little bit. After all, it saves us a lot of time to play the WindowsDK framework code.

Please open the classview (ClassView), if you are wrong, we can see three classes. The three classes of Caboutdlg, CDialogApp, CDialogdlg, respectively.

Among them, CDialogApp is the most important class. Double click CDIALOGAPP to open its definition. We will see it is so defined:

Class CDialogapp: Public CWINAPP

We can see this class is derived from CWINAPP. In MFC programming, this situation is a lot of see, inherited class library classes to add yours needed, and then use it. In the MFC application, CWINAPP is used in this way. Check a class library about CWINAPP descriptions, this:

The main application class in the MFC encapsulates the initialization, operation, and termination of the application for the Windows operating system. Based on the framework-generated application must have some objects that are derived from the CWINAPP. Construct this object before creating a window.

CWINAPP is derived from CwinThread, which indicates the main execution thread of an application that may have one or more threads. In the latest version of MFC, InitInstance, Run, ExitInstance, and OnIdle member functions are actually located in the CWINTHREAD class. These functions are discussed here because of the role of the object as an application object rather than the main thread because it is discussed.

Like any program for Windows operating systems, the framework application also has a WinMain function. However, you don't have to write WinMain in the framework application. It is provided by class libraries and calls when the application starts. WinMain executes standard services such as registration window classes. Then it calls the member function of the application object to initialize and run the application. (You can rewrite the CWINAPP member function called by WinMain from defining Winmain.) For the initialization application, the WinMain calls the application object's initApplication and InitInstance member functions. To run the message loop of the application, WinMain calls the RUN member function. At termination, WinMain calls the ExitInstance member function of the application object.

The frame application referred to in this paragraph, including our dialog application. As MSDN said, the MFC class library has provided us with a Winmain function without having to add it. This is why the MFC program does not see the principle of the main function. Look at this sentence "The frame-generated application must have some objects that are derived from the CWINAPP. The object is constructed before creating the window.", Open the global (glotbals), will find one TheApp global variable (or object, I always think that variables and objects can be classified as a class, should have a unified name). Double-click it, you can see the definition of cdialogapp theapp. Because global variables and objects are first created in the program, so that the CWINAPP object is constructed before creating a window (because CDIALogApp is derived from CWINAPP, TheApp is also a CWINAPP object). This global object is very useful, because CWINAPP itself integrates all program resources WinAPI, we can use it to get the resource of the program (eg icon, image, predefined string, etc.). Generally, this global object is usually used, not directly using THEAPP, but calls: AFXGetApp () to get the pointer to this global object.

The MFC default principal function, first call the INITAPPLICATION and InitInstance member functions of theApp object, to initialize the initialization in the program, usually override the initInstance function. Then, create a message loop, which is different to call theApp's RUN member function in the loop. After receiving WM_QUIT, exit the While loop. Finally, execute theApp's ExitInstance member function to end the entire application.

Let's expand the CDialogApp class in the class view (click That Symbol), we can see that CDialogApp rewrites the initInstance () function. It is used to initialize the application main thread. Double-click InitInstance () in the view to view the definition of this function. The function I here is defined as follows:

000: bool cdialogapp :: initInstance ()

001: {

002: // If a application code running on Windows XP is specified

003: // Enable visualization using COMCTL32.DLL version 6 or higher,

004: // requires INITCOMMONCONTROLS (). Otherwise, the window will not be created.

005: INITCOMMONCONTROLS ();

006:

007: cwinapp :: initInstance (); // Call the parent class's initInstance to perform the default initialization

008:

009: AFXENABLECONTROLCONTAINER ();

010:

011:

012: CDialogdlg DLG; // Create a dialog object, cdialogdlg is our custom dialog class 013: m_pmainwnd = & dlg; // Set the main window of this thread (ie program main thread) to this dialog

014: int_ptr nresponse = DLG.Domodal (); // Display this dialog box until the dialog is closed

015: if (nresponse == idok) // If the dialog is turned off with OK, then

016: {

017: // Todo: Place the processing with "OK" to close

018: // Code of the dialog

019:}

020: Else if (NRESPONSE == iDCancel) // If the dialog is turned off with cancellation, then

021: {

022: // Todo: Place the process to close with "cancel"

023: // Code of the dialog

024:}

025:

026: // Since the dialog is closed, FALSE will return to exit the application.

027: // Instead, not the message pump that launches the application.

028: Return False;

029:}

Because the end of the end return value of the initInstance () function is False, the application will exit immediately. That is, only the dialog is displayed, and the program will end when the dialog is turned off. At this time, the InitInstance function is a bit of the master's taste.

Below, let's take a look at the definition of the CDIALOGDLG class, which is derived from CDIALOG. It rewrites the following functions

CDialogdlg (CWND * PParent = NULL); custom constructor

Virtual Bool OnInitDialog (); dialog Initialization message operation function

AFX_MSG Void OnSysCommand (Uint Nid, Lparam Lparam); System Menu Message Response Function

AFX_MSG void onpaint (); dialog box redrawing response function

AFX_MSG HCURSOR ONQUERYDRAGICON (); Minimize icon inquiry response function

In addition, it is important to pay attention to the definition of such an enumeration in the defined body of the CDIALOGDLG class:

ENUM {IDD = IDD_DIALOG_DIALOG};

It shows that the dialog template used by this CDIALOGDLG class is IDD_DIALOG_DIALOG.

CDialogdlg derived level is as follows:

CDialogdlg => cdialog => cwnd => ccmdtarget => COBJECT

Let's take a look at the constructor:

CDialogdlg :: cdialogdlg (cdialogdlg (cwnd * pparent / * = null * /)

: Cdialog (cdialogdlg :: IDD / * This IDD is the value of the enumeration * /, pparent)

{

m_hicon = AFXGetApp () -> loadicon (iDR_mainframe);

}

First, first, the constructor of the parent class CDialog is called to complete the default constructor. Second, it uses the AFXGetApp function to get the global CWINAPP object TheApp's pointer and use its LoadICON function to get the IdR_mainframe icon resource in the program and assign the member variable m_hicon. This icon can be found and set in the ICON of the resource view.

In CDIALOGDLG implementation file cDialogdlg.cpp, you can find the following statement

Begin_MESSAGE_MAP (CDIALOGDLG, CDIALOG) ON_WM_SYSCOMMAND ()

ON_WM_PAINT ()

ON_WM_QUERYDRAGICON ()

//}} AFX_MSG_MAP

END_MESSAGE_MAP ()

This is a message mapping macro definition segment. Indicates that this dialog class can respond to WM_SYSCOMMAND, WM_PAINT, WM_QUERYDRAG-ICON message. Their response functions, the system defaults for OnsysCommand, ONPAINT, ONQUERYDRAGICON. This means that if the CDIALOGDLG class dialog has received the WM_SYSCOMMAND message, you will call oversysCommand. Other messages take this as an example. However, these response sections are generally not used by our own manual, which is managed by the system. VK JS You get a MFC program, this piece is a good entry point, you can clearly see what information can be in response to this program. These macros can be reviewed in MSDN.

Next paragraph

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

New Post(0)