Author: liguisen
Blog: http://blog.csdn.net/liguisen/
In the MFC slimming code, you may have discovered two special files, stdafx. HS and tdafx. cpp. They are just include header files and have nothing to do. If you are careful enough, you will also find that stdafx. H is included at the top of each CPP file. What exactly does this mean? Can I remove it? We removed the # include "stdafx. H" sentence in test1.cpp and found the following error during compilation: Fatal error c1010: unexpected end of file while looking for precompiled header directive. The pre-compilation instructions are not found. What is the purpose of this file? In fact, wizard has already told us the answer. The answer is in the "comment! What? Comment? Have you deleted it? Yes, it is in the comment we have deleted. Therefore, everything that wizard does is justified. Why should we delete the comment? Not just to let you focus on the key things! Do you still remember the backup project? Open and check:
// Stdafx. h: Include File for standard system include files,
// Or project specific include files that are used frequently,
// Are changed infrequently
Oh, it was originally used to contain some standard system header files and some frequently used but rarely changed header files in the project (here I can see what I said, you should understand the importance of English ). Let's look at the notes of stdafx. cpp:
// Stdafx. cpp: source file that has des just the standard has des
// Test1.pch will be the pre-compiled Header
// Stdafx. OBJ will contain the pre-compiled type information
Look at the first line. Indeed, there is only one line in stdafx. cpp # include "stdafx. H ". Test1.pch is the precompiled header (which can be found in the debug folder under the project directory). What is the role of test1.pch? The answer is: Generally, the header files contained in the VC ++ program are complex. If every compilation analyzes each line of code (many of the Code remains unchanged ), it takes a lot of time to repeat the analysis. Therefore, it is necessary to save some unchanged results for the time being, so as to save Compilation Time. How to Use test1.pch? We need to specify a header file stdafx. h. Of course, it can also be another name. On the C/C ++ page in project> Settings, select precompiled headers for category, and select use precompiled header file, you can modify the name in the through header. I guess it is because the. h file cannot be compiled, so a CPP (stdafx. cpp) file is also needed to generate the PCH file.
Since this PCH pre-compiled header is used to save time, can we also use it? Of course, we have time. First, on the C/C ++ page in project-> Settings, select precompiled headers for category, select not using precompiled headers, and then in test1.cpp and test1dlg. # include "stdafx. H "removed, and added stdafx. the content in H is deleted in the project. H and stdafx. CPP, compilation and running are all correct! Of course, this is not a clever practice! Don't be confused, and restore the pre-compilation modification.
Another small problem: Why should stdafx. H be included in CPP instead of in. h? This is to avoid repeated inclusion of header files.
Next let's take a look at how the program is executed. For beginners, I have a better method, which is called the breakpoint method ". We add breakpoints to all the code, and then F5 performs debugging. In this case, you will find that the VC will prompt you that some breakpoints are invalid. Okay, go, until the program runs, the window is displayed, and then close. I listed the steps for executing the program:
//////////////////////////////////////// ////////////////
Test1.cpp:
# Include .....
Ctest1app theapp;/* Step 2 */
Bool ctest1app: initinstance ()
{/* Step 2 */
Ctest1dlg DLG;/* Step 2 */
DLG. domodal ();/* Step 2 */
Return false;/* Step 2 */
}/* Step 2 */
//////////////////////////////////////// ////////////////
Test1dlg. cpp:
# Include .....
Ctest1dlg: ctest1dlg (cwnd * pparent)
: Cdialog (ctest1dlg: IDD, pparent)
{/* Step 2 */
}/* Step 2 */
You must click "OK" or "cancel" in the dialog box or click "×" in the title bar to exit the program.
Step 2:
Take a look at the ctest1app theapp Note: // The one and only ctest1app object. See comments again? You can open another VC and view the project at the beginning. Theapp is an object of the application class, representing an application, which is a separate global object. Tip: You can change it to another name.
Step 2:
Enter an initinstance () function of the application class (Note: // ctest1app initialization, which can be seen from the function name ).
Step 2:
Declare a ctest1dlg object DLG.
Steps 2 and 5:
After the ctest1dlg class declares an object, it immediately calls its constructor (for constructor, you need to read relevant C ++ books), which in turn calls the constructor of its parent class cdialog. Note that the original code is as follows:
Declare ctest1dlg (cwnd * pparent = NULL) in. h );
Implementation in. cpp
Ctest1dlg: ctest1dlg (cwnd * pparent/* = NULL */)
: Cdialog (ctest1dlg: IDD, pparent)
Have you noticed? Pparent is null. (For details, see cdialog: cdialog in msdn)
We focus on the ctest1dlg: IDD parameter. Where did I come from, where did I come from? In the header file: Enum {IDD = idd_test1_dialog}, you can change the name as much as possible, for example, to myidd:
Enum {myidd = idd_test1_dialog };
Ctest1dlg: ctest1dlg (cwnd * pparent/* = NULL */)
: Cdialog (ctest1dlg: myidd, pparent)
You can also do this:
Ctest1dlg: ctest1dlg (cwnd * pparent/* = NULL */)
: Cdialog (myidd, pparent)
You can even open resource. h to find the definition of idd_test1_dialog:
# Define idd_test1_dialog 102
Then you can:
Ctest1dlg: ctest1dlg (cwnd * pparent/* = NULL */)
: Cdialog (102, pparent)
What does this mean? Note that this parameter is only the ID of the dialog box template resource, so we can change this ID to let the Program Load another dialog box template resource. Right-click the dialog folder in resourceview and Choose Insert dig1 to insert a dialog box. Its default ID is idd_dialog1. Let's do this:
Enum {IDD = idd_dialog1 };
Ctest1dlg: ctest1dlg (cwnd * pparent/* = NULL */)
: Cdialog (ctest1dlg: IDD, pparent)
Run it. So we get an inspiration: different panels can be displayed in the initinstance () function according to different conditions.
Step 2:
The ctest1dlg object DLG calls its member function domodal () to display the dialog box.
Steps 2 and 8:
Exit the program.
The process of the MFC program is so simple (at least we can see it on the surface). What are you afraid? It is also easy to learn MFC. Haha, this is not the case. Next time I will combine F11 and F11 to show you more things.