General dialog box

Source: Internet
Author: User

In VC, cfiledialog is used for file operations. Generally, you do not need to derive a new class because the base class already provides common functions. In addition, after the dialog box is created and waits for the completion of the dialog box, you can use the member function to obtain the user's selection in the dialog box.

Use of the cfiledialog file selection dialog box: first, construct an object and provide relevant parameters. The constructor prototype is as follows:
Cfiledialog: cfiledialog (bool bopenfiledialog, lpctstr success = NULL, lpctstr lpszfilename = NULL, DWORD dwflags = Success | fail, lpctstr lpszfilter = NULL, cwnd * pparentwnd = NULL ); the parameters have the following meanings:

If bopenfiledialog is set to true, the open dialog box is displayed. If it is set to false, the SAVE dialog file dialog box is displayed.
Lpszdefext specifies the default file extension. If this parameter is not specified in the file name column, the extension is automatically added to the file name (for saving)
Lpszfilename specifies the default file name, and the file name appears in the file name column.
Dwflags indicates some specific styles.
Lpszfilter is the most important parameter. It specifies the available file type and the corresponding extension. The parameter format is as follows:
"Chart files (*. xlc) | *. xlc | worksheet files (*. XLS) | *. XLS | data files (*. xlc ;*. XLS) | *. xlc ;*. XLS | all files (*. *) | *. * | "; the file type description and extension are separated by |. files of the same type can be separated by the extension; separated by |, and ended with |.
Pparentwnd is the parent window pointer.
In the create file dialog box, you can use domodal (). After the result is returned, you can use the following function to get the user to choose from:
Cstring cfiledialog: getpathname () to obtain the complete file name, including the directory name and extension C:/test/test1.txt
Cstring cfiledialog: getfilename () to obtain the complete file name, including the extension test1.txt.
Cstring cfiledialog: getextname () to obtain the complete file extension, for example, txt
Cstring cfiledialog: getfiletitle () to get the complete file name, excluding the directory name and extension such as test1
Position cfiledialog: getstartposition () gets the first file location when multiple files are selected.
Cstring cfiledialog: getnextpathname (Position & Pos) gets the next file location when multiple files are selected, and returns the current file name at the same time. However, you must have already called position cfiledialog: getstartposition () to obtain the initial position variable.

 

Call the open file dialog box:

Void csimpledlg: onfileopen ()
{

Cfiledialog * lpszopenfile; // defines a cfiledialog object
Lpszopenfile = new
Cfiledialog (true, ",", ofn_filemustexist | ofn_hidereadonly, "file type (*. yourfiletype) | *. yourfiletype |"); // generate a dialog box
If (lpzsopenfile-> domodal () = id_ OK) // if you click OK in the dialog box
{
Cstring szgetname;
Szgetname = lpszopenfile-> getpathname (); // obtain the path to open the file.
Setwindowtext (szgetname); // display the path on the window title
}
Delete lpszopenfile; // release the allocation dialog box
}

 

Some functions are described as follows:

Cfiledialog (
Bool bopenfiledialog. // if the value is true, the "open" dialog box is displayed. If the value is false, the "save" dialog box is displayed.
Lpctstr lpszdefext = NULL, // default file name extension
Lpctstr lpszfilename = NULL,
// When the dialog box is opened, the default file name appears in the text box by default. If it is null, the default file name is not displayed.
DWORD dwflags = ofn_hidereadonly | ofn_filemustexist
// A combination of file attributes and other definitions,
Lpctstr lpszfilter = NULL, // file type that can be applied, such as *. MP3
Cwnd * pparentwnd = NULL,
// A pointer to the file dialog-box object's parent or owner window.
DWORD dwsize = 0
/* The size of the openfilename structure. this value is dependent on the operating system version, so MFC can determine the appropriate kind of dialog to create (for example, new Windows 2000 dialogs as opposed to NT4 dialogs ). */

);

 

 

//###################################### ######################################## ###
Using the cfile class and cfiledialog in MFC can easily load and save files ......

Use of the cfiledialog file selection dialog box:

First, construct an object and provide corresponding parameters. The constructor prototype is as follows:

Cfiledialog: cfiledialog (

Bool bopenfiledialog, // if it is true, the open dialog box is displayed. If it is false, the SAVE dialog box is displayed.

Lpctstr lpszdefext = NULL, // default file extension

Lpctstr lpszfilename = NULL, // default file name

DWORD dwflags = ofn_hidereadonly | ofn_overwriteprompt, // set the style

Lpctstr lpszfilter = NULL, // list available file types and corresponding extensions

Cwnd * pparentwnd = NULL // parent window handle pointer

);
 

 

The format of the lpszfilter parameter is as follows:/"Chart files (*. xlc) | *. xlc | worksheet files (*. XLS) | *. XLS | data files (*. xlc ;*. XLS) | *. xlc ;*. XLS | all files (*. *) | *. * |/"; file types and extensions are separated by |. Extensions of files of the same type can be separated by |, | indicates the end.

In the create file dialog box, you can use domodal (). After the result is returned, you can use the following function to get the user to choose from:

Cstring cfiledialog: getpathname () to obtain the complete file name, including the directory name and extension C: // test // test1.txt

Cstring cfiledialog: getfilename () to get the complete file name, including the extension:

Test1.txt

Cstring cfiledialog: getextname () to obtain the complete file extension, for example, txt

Cstring cfiledialog: getfiletitle () to get the complete file name, excluding the directory name and extension such as test1

Position cfiledialog: getstartposition () gets the first file location when multiple files are selected.

Cstring cfiledialog: getnextpathname (Position & Pos) gets the next file location when multiple files are selected, and returns the current file name at the same time. But it must have already been called.

Position cfiledialog: getstartposition () to obtain the initial position variable.

 

The following is the implementation code:

// Open the file
// Create a file name cache (filebuffer) and a file read cache (rbuffer)

// If the cache is not set to 0, an error will occur in the subsequent settings. If the request is dynamic, use memset or for loop to clear the array.

Char filebuffer [5010] = {0}, rbuffer [5010] = {0 };

Int rbsize;

// Define a cfile class to process files

Cfile in;

// Define a cfiledialog class to display the standard open file dialog box

Cfiledialog indlg (false, null, _ T (/"custom file type (*. XXX) | *. xxx | all files (*. *) | *. * |/"), null );

Indlg. m_ofn.lpstrtitle = _ T (/"open a custom file /");

// Set a file name cache, because the built-in cfiledialog file name cache length is only 200, but in many cases, the file path is much larger than this number. To be safe, you must set a file name cache by yourself.

Indlg. m_ofn.lpstrfile = filebuffer;

// Set the cache Length

Indlg. m_ofn.nmaxfile = 5000;

// Display the dialog box and process the events with the OK button
 
If (indlg. domodal () = idok ){

// Open the file in read-only mode

If (in. Open (indlg. getpathname (), cfile: moderead )){

// Obtain the file length to determine the size of the read cache.

Rbsize = in. getlength ();

// Avoid array overflow. You can also dynamically apply for cache space here.

If (rbsize> 5000) rbsize = 5000;

// Read data

N. Read (rbuffer, rbsize );

// Close the file

In. Close ();

MessageBox (/"file opened successfully/",/"done/", mb_iconexclamation | mb_ OK );

} Else {

MessageBox (/"failed to open the file/",/"failed/", mb_iconstop | mb_ OK );

}

}

// Save the file

// Create a file name cache (filebuffer) and a file write cache (wbuffer)

// If the cache is not set to 0, an error will occur in the subsequent settings. If the request is dynamic, use memset or for loop to clear the array.

Char filebuffer [5010] = {0}, wbuffer [5010] = {0 };

Int wbsize;

Cfile out;

Cfiledialog outdlg (false, null, _ T (/"custom file type (*. XXX) | *. xxx | all files (*. *) | *. * |/"), null );

// Set the title of the Save dialog box

Outdlg. m_ofn.lpstrtitle = _ T (/"Save the custom file /");

// Use the custom file name cache

Outdlg. m_ofn.lpstrfile = filebuffer;

// Set the default suffix for saving the file. If you have a suffix, enter the suffix.

Outdlg. m_ofn.lpstrdefext =/"xxx /";

// Set the cache size

Outdlg. m_ofn.nmaxfile = 5000;

// Display dialog box

If (outdlg. domodal () = idok ){

// Get the file name and open it with creation and writing

If (out. Open (outdlg. getpathname (), cfile: modewrite | cfile: modecreate )){

// Write the content of the file you want to write to the cache here. If the written content is too long, you can write it in multiple times.

// Get the length of the content written into the current Cache

Wbsize = strlen (wbuffer );

// Write a file

Out. Write (wbuffer, wbsize );

// Close the file

Out. Close ();

MessageBox (/"file saved successfully/",/"done/", mb_iconexclamation | mb_ OK );

} Else {MessageBox (/"failed to save the file/",/"failed/", mb_iconstop | mb_ OK );

}

}

Related Article

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.