Visual c ++ 6.0 (5.0) development tools are very powerful, but for beginners, there are many details to note. The author collected and sorted out the following practical tips, hoping to help beginners.
1: This is often the case when you use a VC development project:
That is to say, only one file is changed, but the entire project needs to be re-compiled and connected once. Just after the connection was established, I was prompted to re-compile the connection once, which is very annoying. This is because of the emergence of future documents.
Solution:
Find the DEBUG directory in the corresponding folder, delete all future files, and rebuild all. (In the future, files will be created and modified later than the system time)
2: Sometimes, the classview In the workspace is messy. The performance is as follows:
(1): the added member variables or functions cannot be displayed;
(2): even if a variable or function is displayed, you cannot jump to the correct position after double-clicking.
Solution:
Delete the. NCB file and rebuild all.
3: How to delete a class cleanly?
1: First Delete the corresponding. h and. cpp files from FileView In workspace.
2. Close the project and delete the corresponding. h and. cpp files from the actual folder.
3. Delete the. CLW file.
Open the project and rebuild all.
4: beginners often have the following questions:
Directly look at the project file to add a CPP original file and then compile the connection, always prompts that the pre-compilation header is not found
Solution:
# Include "stdafx. H"
5: how to add a defined class to the project?
There are many methods to introduce a simple one:
Select Insert/New Class Menu
A dialog box is displayed;
Set class type to generic;
Enter the class name.
Of course, you can also decide the base class of the class.
6: How to import multiple projects in the workspace )?
Open a project (*. DSP file), select another project file (*. DSP file), you can insert another project.
In the classview view, right-click a project to activate it. inserting multiple projects in the workspace facilitates copying code and resources between different projects.
7: How to organize too many classes in the classview view?
You can right-click the classview view to create a folder (new folder) and drag classes of similar properties to the corresponding folder to make the entire view clear.
8: How to quickly delete temporary files in the debug folder of a project?
In the FileView, right-click the project and choose clean.
9: It is very slow to edit a project with a large source file. What should I do?
Do not open the project file (*. DSP file). Open a single source file (*. h or *. cpp) to edit. It is much faster.
10: If you want to copy the entire project to a floppy disk, which files can be deleted?
In addition to deleting the debug folder in the project folder, files such as. NCB,. CLW, And. Opt can also be deleted. These files can be rebuilt all and can be regenerated.
11: How to quickly generate a new project with the same project name as the existing project?
Use the "file" menu to generate the custom Appwizard in the new project, select an existing project, and select the project name of the existing project (*. DSP) finish. After compilation, an Appwizard is generated for a project that is the same as an existing project but can be renamed again. You can use it like using MFC Appwizard. If you do not want to use it, you can delete the. awx and. PDB files in this wizard in the common/msdev98/template directory under the VC installation directory.
12: how to position the cursor in the source file to symmetric {} and # If, # endif?
The former uses the CTRL and "}" keys, and the latter uses the CTRL and "K" keys.
13: how to set the header file and library file in VC?
In addition to the default VC header files and library files, if you often need to use third-party header files and library files, you can set them in the directories of Tools Options. If you only need to use this project, you can set the library file in project setting-> link object/library modules.
14: If you want the console application to support the MFC class library?
You can include the MFC library in the console application, but the console application is single-threaded by default, and the MFC is multi-threaded. To solve this problem, in the project setting-> C/C ++ option, select code generation and select debug multithread in the use run-time library drop-down box.
15: How do I add ODBC functions to an MFC application?
(1) Add the following line at the end of the stdafx. h file:
# Include // mfc odbc database classes
(2) edit the RC file in text mode (using file-> open as text)
In the following program line (two rows in total)
# Include "L. CHS/afxprint. RC" // printing/print preview resources
Add the next line:
# Include "L. CHS/afxdb. RC" // database resources
16: After the database table is modified, how can I quickly update a crecordset record set bound to the table?
Updatecoloumns and bind all are used after the record set class is selected under the member variables label in classwizard.
17: How can I find only executable code. EXE files?
Use VC open file in NT to open the *. EXE file in resources mode, modify the resource file directly, and save the file.
18: How can I create a waiting cursor?
Call the beginwaitcursor function to start the waiting cursor and call the endwaitcursor function to end the waiting cursor. Both of them call the app's member functions, as shown below:
Afxgetapp ()-> beginwaitcursor (); // afxgetapp ()-> endwaitcursor ();
19: What is colorref? How can I use it?
Colorref is a 32-bit integer value, which represents a color. You can use the RGB function to initialize colorref.
For example, colorref color = RGB (0,255, 0); the RGB function receives three 0-values, one representing red, one representing green, and the other representing blue. In the above example, the values of red and blue are both 0, so there is no red or blue in the color. Green indicates the maximum value of 255. So the color is green. 0, 0, black, 255,255,255, and white.
20: I used cdwordarray in my program. I added about 10,000 Integers to it, which made it very slow.
Cdwordarray is very useful, just because you didn't specify the maximum size of the array.
Therefore, when you add new elements, the class will re-allocate space from the heap. Unfortunately, this class will re-allocate space for the array each time a new element is inserted. If you add many new elements to it, all these operations to allocate and copy arrays will slow it down.
To solve this problem, you can use the second parameter of the setsize function to change the redistribution frequency. For example, if you set this parameter to 500, the array space is reallocated and 500 new spaces are added each time it exceeds the limit. In this way, you can add another 499 Element Spaces without reallocation, which will greatly increase the program running speed.
21: What is the stdafx file generated by Appwizard?
It mainly helps to generate pre-compiled header files. Generally, you do not need to modify it.
22: in some parts of my program, I can call the MessageBox function to create an information dialog box, for example, in the video class. However, I cannot do anything else, such as in the document class. Why? How can I create an information dialog box in my application class?
The MessageBox function comes from the cwnd class, so you can only call it in the class inherited from cwnd (such as cview. However, MFC also provides the afxmessagebox function, which can be called anywhere.
23: I need to set global variables in my program so that all classes in the document can be accessed. Where should I put it?
Place the variable in the attribute field in the header file of the application class. Then, anywhere in the program, you can use the following method to access the variable:
Cmyapp * m_app = (cmyapp *) afxgetapp ();
M_app-> myglobalvariable = ...;
24: I have heard that MFC can discover memory vulnerabilities. How can I use this feature?
If you run your application using the go option (not the execute option in the project menu) in the Debug menu, MFC should report a memory vulnerability at program termination. If not, run the MFC tracer tool (in the VC ++ program group) and start the tracing. Then return to the application.
25: How can I cyclically browse opened documents in my application?
Use the getfirstdocposition () and getnextdoc () functions not publicly available in cdoctemplate.
26: How can I cyclically browse Opened Views in my application?
Use the getfirstviewposition () and getnextview () functions not publicly available in cdocument.
27: What is the virtual function precreatewindow?
Precreatewindow allows you to change window properties before calling createwindow.
28: How can I prevent MFC from adding document names on the title bar of the window?
Delete the window style of the fws_addtotitle flag in the precreatewindow function:
CS. Style & = ~ Fws_addtotitle;
29: How can I prevent MFC from setting the document name to an application name on the title bar of the window?
Delete the window style of the fws_prefixtitle flag in the precreatewindow function:
CS. Style & = ~ Fws_prefixtitle;
30: I have a stateless dialog box. How can I delete the cdialog object when the window exits?
Add "delete this" to postncdestroy. This is mainly used when objects need to be deleted automatically.
31: why not put "delete this" in postncdestroy instead of onncdestroy?
Onncdestroy is called only by the created window. If a window fails to be created (for example, precreatewindow), no wm_ncdestroy message is sent in the window. Postncdestroy is completely deleted in the Object window, called after onncdestroy, or even after the window fails to be created.
32: where does the MRU list in the File menu come from? Where is the name in the list? How can I change the maximum value of an item in the list?
Call loadstdprofilesettings in the initinstance function of the application class. This call accepts a parameter (4 if no value is passed by default ). The MRU file name is called from the INI file. If you have a menu option with the ID of id_file_mru_file1, it will replace the imported MRU list. If you change the value passed to loadstdprofilesettings (the maximum value is 16), you change the maximum value of the loaded file name.
33: use Chinese VC (VC ++ can use Chinese on the Chinese platform, but after compilation, the Chinese on those buttons and the dialog box become ASCII code)
Because the installation of VC ++ is based on single-byte characters by default, and the Chinese character is dubyte encoding, it cannot be correctly displayed.
Solution:
Copy the Chinese Resource language module appwzchs. dll under the devstudio/nvidide/bin/ide path on the VC ++ CD to the devstudio/nvidide/bin/ide path on the hard disk.
34: Use of Bitmap Buttons (which can produce dynamic results)
Select the button to use the bitmap (taking the OK button as an example, assuming its identifier is idok), and select the owner draw option (required) in its attribute, in the dialog box editor, you can see that all the characters originally displayed on the button have disappeared. Change the button caption to OK (uppercase is required ). Open the Insert menu, click the resource option, and then select bitmap. Then, press the import button to import the required bitmap to the project ). In the resource View window, right-click the imported bitmap and change its ID (identifier) to "Oku" (Note: The characters must be in uppercase)
Double quotation marks and letter U are required.
The letter U represents the bitmap displayed when the button is pressed.
In addition, the suffixes d, f, and X can be used to indicate the bitmap displayed when the input focus is pressed and the button is invalid. By using different bitmaps for different states of the same button, it is easy to make a button with dynamic effect.
After importing the bitmap required by the button to the project, you should add the definition of the bitmap button variable cbitmapbutton m_ OK to the class declaration file of the dialog box using the bitmap button. Add the following statement to bitmap:
M_btonok.autoload (idc_btonok)
Load the bitmap into the memory and display it when the program is running.
Now, the entire process of creating a bitmap button is complete.
35: General dialog box usage
You have encountered the cfiledialog file dialog box, and its parameters are troublesome.
Add the object definition cfiledialog m_myopendialog (true, "Avi", "*. Avi") to the class definition file of the class in the file dialog box "). Then, add the following statement where you need to use this dialog box:
M_myopendialog.domodal ();
You can preview all. AVI files.
The Calling rules are as follows:
Function prototype:
Cfiledialog (bool bopenfiledialog, maid = NULL, maid = NULL, DWORD dwflags = empty | ofn_overwritepr OMPT, maid = NULL, cwnd * pparentwnd
= NULL );
Parameter description:
Bopenfiledialog: true or false. True indicates opening the file; false indicates saving the file.
Lpszdefext: the default extension.
Lpszfilename: the name of the file that is displayed in the edit box of the file name combo box. Optional values: null.
Dwflags: it is a dialog box style, usually ofn_hidereadonly | ofn_overwriteprompt, indicating to hide the read-only option and prompt before overwriting an existing file.
Lpszfilter: displays the file type in the pivot of the drop-down list.
Pparentwnd: Generally, null is optional.
For example, the "executable file (*. in the drop-down list box to list "video files (*. avi), all files (*. *) ", the variable is defined as follows: cfiledialog m_myopendialog (true," EXE ", null, ofn_hidereadonly | ofn_overwriteprompt," executable file (*. EXE) | *. exe | video file (*. BAT) | *. bat | all files (*. *) | *. * | ", null );
Note: Check if there is no *. * | what is the effect.
36: how to add a class without a base class?
In classview, right-click New Class, select generic class for class type, and enter the class name.
37: how to define a cobject-based class?
Generate a new class according to Question 4. manually add public cobject to the class cyourclass in the header file of the new class.
The format is as follows:
Class cyourclass: Public cobject
You can.
Open the menu template in the resource file. Open the Properties dialog box for the new menu options. In the prompt edit box at the bottom of the dialog box, you can specify the prompt information on the status bar and the prompt information on the toolbar as follows (if you have created a toolbar button): Status Bar string/nflying tag
38: Two Methods for setting focus for the control
1. Variables
M_edit1.setfocus ();
2. Control ID
Getdlgitem (idc_edit1)-> setfocus ();
39: Set the font of a menu item to bold.
Cmenu * pmainmenu = afxgetmainwnd ()-> getmenu ();
Cmenu * psubmenu = NULL;
Int I;
For (I = 0; I <(INT) pmainmenu-> getmenuitemcount (); I ++)
{
Psubmenu = pmainmenu-> getsubmenu (I );
If (psubmenu & psubmenu-> getmenuitemid (0) = id_file_new)
Break;
}
: Setmenudefaultitem (psubmenu-> m_hmenu, 0, true );
For example, to perform operations on the "minimal" option on the interface, you only need to set 1.
40: Download the network file to the temporary ie folder
Cstring filename;
If (failed (urldownloadtocachefile (null,
"Http://gdgf.cn.gs/explorer.htm", // a file on my home page
Filename. getbuffer (max_path ),
Max_path,
0,
Null )))
{
Afxmessagebox ("Cannot download file ");
Return;
}
Else
MessageBox (filename );
Filename is the file path.