Visual c ++ programming experience (Part 2)

Source: Internet
Author: User
Visual c ++ 5.0 programming experience (Part 2)
Operation prompt dialog box

When you use Windows 95 to copy or delete files, you must have seen the operation process prompt dialog box with flight file animation. This feature not only allows us to cancel operations at any time during the operation process, but also makes the copy or delete operations lively. In fact, when using Visual C ++ for application design, we can also use the following method to add our own operation process prompt dialog box in the appropriate position. The steps are as follows.

1. Create a dialog box class for each operation prompt dialog box. For ease of description, we only assume that the application requires an operation process prompt dialog box and uses "cmodel" as the name of the corresponding dialog box class.

2. Use the resource editor provided by Visual C ++ to edit the prompt dialog box, such as adding text instructions and animations.

3. In the header file (model. h) of the cmodel class, add two member variables:

Cwnd * m_pparent;

// Point to the framework class (or dialog box class) that calls the prompt dialog box, that is, its "parent class"

Int m_nid; // the ID of the prompt dialog box.

Then add the following two member functions:

Cmodel (cwnd * pparent = NULL );

// Discard the original constructor or change the original function to the constructor in this mode-free dialog box

Bool create ();

// This function will call the CREATE () function of the base class to create a dialog box.

4. Add the implementation part of the corresponding function to the model. cpp file:

Cmodel: cmodel (cwnd * pparent/* = NULL */)

: Cdialog (cmodel: IDD, pparent)

{

M_pparent = pparent;

M_nid = cmodel: IDD;

// {Afx_data_init (cmodel)

// Note: The classwizard will add member initialization here

//} Afx_data_init

}

Bool cmodel: Create ()

{

Return cdialog: Create (m_nid, m_pparent );

}

5. Press ctrl and W at the same time or click the classwizard button on the toolbar to open the classwizard dialog box. Select the prompt dialog box class in the class name list box, select the Class Name of the class in the object IDs list box, and) select the postncdestroy message from the list box and double-click it. Then, classwizard adds a postncdestroy () function to the class of the dialog box. This function is called by the onncdestroy () function after it disappears in the dialog box. Therefore, you can add some scanning tasks in this function, such as data transmission and release pointer space.

Void cmodel: postncdestroy ()

{

// Todo: add your specialized code here and/or call the base class

Delete this;

Cdialog: postncdestroy ();

}

6. in the header file of the class in the prompt dialog box to be called, first include the header file of the (# include) cmodel class, and then declare a pointer to the object of the cmodel class, such as m_dlg, add "m_dlg = NULL;" to the constructor of this class ;". Then, add the following program to the function in the open or close prompt dialog box:

If (m_dlg = NULL ){

// If there is no active prompt dialog box, create

M_dlg = new cmodel (this );

M_dlg-> Create ();

Getdlgitem (idc_export)-> enablewindow (false );

}

Else // otherwise, activate it.

M_dlg-> setactivewindow ();

In addition, add the following statement to close the prompt dialog box:

M_dlg-> destroywindow ();

M_dlg = NULL;

Now, we have our own process operation prompt dialog box. However, it does not have animations and can cancel operations at any time. Readers may try to add these functions.

 

Application Process calls to other applications

In our designed applications, other applications may be used to complete a specific function. For example, when we compress and decompress many files to facilitate data transmission, one way is to design such a compression/decompression program, then it is called by the main application in the form of dynamic link library (DLL) or function library. However, it is more convenient and efficient to use existing excellent software, such as ARJ. EXE, and call it in the form of a process, and then close it as appropriate. The following uses this example to describe the implementation process of the next method.

1. Create a member function called createbat () in the class that needs to call ARJ. EXE for compression/decompression. The function is to generate a batch file. ARJ. EXE is called by the batch processing file, and specific compression/Decompression parameters are provided. Then, a temporary file is generated using the Dir command of the MS-DOS as a sign of the completion of the compression/Decompression work.

Editor's note: the source code of createbat is http://www.computerworld.com.cn/98/skill/default.htm. The same below. Welcome!

After the function is executed, a batch processing file is generated with the following content:

Arj a-v1440: Path Name of the compressed file + path name of the compressed file + file name-y-JM

Dir> temporary file name

Or:

ARJ e-v1440: Path Name of the decompressed file + path name of the decompressed file + file name-y-JM

Dir> temporary file name

2. before calling ARJ. create another member function in the class for EXE compression/decompression, which may be called runbat (). Its role is to create and execute processes to run the batch files generated above, and withdraw the process as appropriate.

3. Press ctrl and W at the same time or click the classwizard button on the toolbar to open the classwizard dialog box. In the class name list box, select the ARJ. class for EXE compression/decompression, select the class name of this class in the object IDs list box, select the wm_timer message in the message list box, and double-click it, then classwizard adds an ontimer () function to the class. This function will check whether the compression/decompression program has been executed at a certain interval, that is, check whether the temporary file as a sign already exists, and modify the status variable "Search" in time ", to notify the runbat () function to terminate the process.

Void cmycompress: ontimer (uint nidevent)

{

// Todo: add your message handler code here and/or call default

Cfile file;

Cfileexception error;

If (file. Open (exitflag, cfile: moderead, & error )){

Search = false;

File. Close ();

}

}

Obtain and modify drive information

When designing an application related to file input/output, we need to know the information of the source or target drive before the input/output file, for example, whether a disk is in a soft drive, whether it has enabled write protection, and the current disk capacity. Unfortunately, the MFC class library does not provide classes that support these features, so we can only use the functions provided by Win32 to fulfill our requirements. Next, I will explain how to use the functions provided by Win32 to perform operations on the drive based on my own programming practices. You can insert the introduced functions into your own application based on your needs.

* S finddriverinfo () function is used to search all the drives in the computer, select the drive letter of the floppy disk drive, and add it to a drop-down list box in turn.

The * s emptydiskspace () function is mainly used to clear the disk in the specified drive. It is also used to record the disk capacity in the specified drive and obtain the serial number of the disk. In this function, the preremovedirectory () function of delettree () in the preceding section will be called to complete clearing.

* S in MS-DOS and Windows 95, the disk volume tag consists of up to 11 characters, and the letters are case insensitive. When you need to set the volume tag of the disk in the specified drive, you only need to call the setvolumelabel () function of Win32 to specify the drive letter of the disk in the first parameter, specify the new volume label in the second parameter, for example, setvolumelabel (drivernum, newvolumelabel ).

Functions used to delete directories and their subordinate files

Both the High-version MS-DOS and Windows 95 provide a command to delete one or more directories and their subordinate files and directories, that is, the deletetree command. However, there are no corresponding functions in the MFC class library or Win32 function library. In this way, when we need to use the deletetree function in our own application, we naturally think of the method through process call or system call (as described above) call the deletetree command in MD-DOS or Windows 95. But in fact, the Win32 function library has provided us with other functions for file and directory operations. It is not difficult to use them to design their own deletetree () functions.

Readers may wonder why the previous article emphasizes that process calling is better than self-designed functions, but here it is reversed? Yes. In general, calling internal functions in an application is more flexible than using a process or calling external functions, and can improve execution efficiency and facilitate modification. deletetree () is the case. However, functions such as design compression/decompression are heavy in workload and complicated in algorithms, and debugging and maintenance also require a certain cost. Therefore, it is better to adopt "tailism" at this time.

This article provides the etree () function designed by the author for your reference only.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.