Issue of cfiledialog file storage extension in MFC

Source: Internet
Author: User

Void cmapfiledlg: onbnclickednew2 ()

{

Static char based_code szfilter [] = "Map Files (*. Map) | *. Map | mapcfg files (*. mapcfg) | *. mapcfg | ";

Cfiledialog DLG (false, null, null, ofn_hidereadonly | ofn_overwriteprompt ,/
Szfilter, afxgetmainwnd ());
Cstring strpath;
Cfileexception E;

If (DLG. domodal () = idok) // determines whether to press the "save" button
{
Strpath = DLG. getpathname (); // obtain the file storage path, which is convenient for right (4), instead of getfilename ();

// Determine the added extension based on the selected extension drop-down box
Switch (DLG. m_ofn.nfilterindex) // you can obtain the filter string used in the extension drop-down box.
{
Case 1:
{
If (strpath. Right (4 )! = _ T (". Map") | strpath. Right (7 )! = _ T (". Map") // determine the file extension
Strpath + = _ T (". Map ");
Break;
}
Case 2:
{
If (strpath. Right (7 )! = _ T (". mapcfg") | strpath. Right (7 )! = _ T (". mapcfg") // determine the file extension
Strpath + = _ T (". mapcfg ");
Break;
}
}

If (access (strpath, 0) = 0) // # include <Io. h> // The above cfiledialog can only check whether a file with the same name already exists. This code is used to check whether there is a file with the same name after the file name is added to the folder, however, if the folder contains a file named "1" and the file name to be saved is also "1", the system will prompt that the file cannot be saved. However, the actual situation is, it can still be saved because the extension will be added.
{
Cstring strquery;
Strquery. Format ("% s already exists. Do you want to replace it? ", Strpath );
If (IDNO =: messageboxa (m_hwnd, strquery, "File Overwrite", mb_iconquestion | mb_yesno ))
{
Onbnclickednew2 ();
}
}

 

 

Cfile file (strpath, cfile: modecreate | cfile: modereadwrite); // create a file

 

.......................

 

}

 

Cfiledialog: m_ofn

Note:
M_ofn is the structure of the openfilename type. This structure is used to display the appearance of the initialization file opening and storage dialog box after creation and using the domodal member function. For example, you can set the lpstrtitle member of m_ofn as the expected dialog box title.

 

The openfilename structure contains GetopenfilenameAnd GetsavefilenameThe function is used to initialize the information of the open or Save As dialog box. After the user closes the dialog box, the system returns the user selection information to this structure.
   Struct prototype:
Typedef struct tagofn {
DWORD lstructsize;
Hwnd hwndowner;
Hinstance;
Lptstr lpstrfilter;
Lptstr lpstrcustomfilter;
DWORD nmaxcustfilter;
DWORD nfilterindex;
Lptstr lpstrfile;
DWORD nmaxfile;
Lptstr lpstrfiletitle;
DWORD nmaxfiletitle;
Lpctstr lpstrinitialdir;
Lptstr lpstrtitle;
DWORD flags;
Word nfileoffset;
Word nfileextension;
Lpctstr lpstrdefext;
Lparam lcustdata;
Lpofnhookproc lpfnhook;
Lpctstr lptemplatename;
# If (_ win32_winnt> = 0x0500)
Void * pvreserved;
DWORD dwreserved;
DWORD flagsex;
# Endif // (_ win32_winnt> = 0x0500)
} Openfilename, * lpopenfilename;

Member

  Lstructsize
Specify the size of this structure, in bytes.
Windows 95/98 and Windows NT 4.0: Use openfilename_size_version_400 for this Member when compiling Windows 95/98 or Windows NT 4.0 with winver and _ win32_winnt> = 0x0500.
Windows 2000 and later versions: this parameter uses sizeof (openfilename ).
  Hwndowner
The handle pointing to the owner dialog box. This member can be any valid window handle, or it can be null if the dialog box has no owner.
  Hinstance
If the ofn_enabletemplatehandle tag is set in the flags member, the hinstance Member points to the memory object that contains a dialog box template. If the ofn_enabletemplate tag is set, hinstance is a module pointing to the dialog box template named by the lptemplatename member. If neither of them is set, this member is ignored.
If the ofn_explorer flag is set, the system uses the subwindow of the default explorer dialog box as the specified template to create a dialog box. If the ofn_explorer tag is not set, the system uses the template to create an Old Style dialog box.
  Lpstrfilter
A buffer pointing to a filter string ending with null characters. The last string in the buffer must end with two null characters.
The first character string is the display string of the filter description (for example, "text file"), and the second character specifies the filter style (for example, "*. txt "). To specify multiple filter styles for a display string, use semicolons (";") to separate the styles (for example, "*. txt; *. Doc; *. Bak "). A style string can contain valid file name characters and asterisk (*) wildcards. The style string cannot contain spaces.
The system cannot change the filter order. It is displayed in the file type combo box in the order specified by lpstrfilter.
If lpstrfilter is null, no filter is displayed in the dialog box.
  Lpstrcustomfilter
Point to a static buffer that contains a pair of filter strings ending with null characters to retain the filtering style selected by the user. The first string describes the display string of the custom filter, and the second string is the filter style selected by the user. In the first application creation dialog box, the first string you specify can be any non-empty string. When you select a file, copy the Current Filter style to the second string in the dialog box. The reserved filter style can be one of the styles specified in the lpstrfilter buffer or the filter style entered by the user. When the next dialog box is created, the system uses this string to initialize the User-Defined file filter. If the nfilterindex member is 0, the custom filter is used in the dialog box.
If the member is null, the dialog box cannot retain the custom filter style.
If the member is not null, the value of the nmaxcustfilter member must specify the buffer size of lpstrcustomfilter in tchars. For the ANSI version, it is the number of bytes; For the Unicode version, it is the number of characters.
  Nmaxcustfilter
Specify the buffer size in tchars specially prepared for lpstrcustomfilter. For the ANSI version, it is the number of bytes; For the Unicode version, it is the number of characters. This buffer should be at least 40 characters long. If the lpstrcustomfilter member is null or a string pointing to null, this member is ignored.
  Nfilterindex
Specifies the index of the selected filter in the file type control. The buffer points to a pair of defined filter strings contained by lpstrfilter. The index value of the first string of the filter is 1, and the second string is 2. The 0 index indicates that the custom filter specified by lpstrcustomfilter is used. You can specify an index in the dialog box as the initial filter description and filter style. When you select a file, nfilterindex returns the index of the currently displayed filter.
If nfilterindex is 0 and lpstrcustomfilter is null, the system uses the first filter in the lpstrfilter buffer. If all three members are 0 or null, the system does not use any filters and does not display any files in the list file of the dialog box.
  Lpstrfile
Point to the buffer that contains the file name used by the initialization file name editing control. If no initial value is required, the first character of the buffer must be null. When the getopenfilename or getsavefilename function returns a success, the buffer contains the drive, path, file name, and the selected file extension.
If the ofn_allowmultiselect mark is set and multiple files are selected, the buffer contains the file name of the selected file in the current directory. For the explorer Style dialog box, the directory and file name strings are separated by null, and there is an additional null after the file name. In the Old Style dialog box, strings are separated by spaces and the function uses short file names for names with spaces. You can use the findfirstfile function to convert between Long and Short file names. If you select only one file, the lpstrfile string is not separated between the path and the file name.
If the buffer size is too small, the function returns false and the commdlgextendederror function returns fnerr_buffertoosmall .. In this case, the first two bytes of the lpstrfile buffer contain the required size (in bytes or characters ).
  Nmaxfile
Specify the buffer size of lpstrfile, in tchars. For the ANSI version, it is the number of bytes; For the Unicode version, it is the number of characters. This buffer must be sufficient for storing the path and file name strings, including the ending null characters. If the buffer size is too small, the getopenfilename and getsavefilename functions return false. The minimum buffer length should be 256 characters.
  Lpstrfiletitle
The buffer (without path information) that directs to the file name and extension of the selected file ). This member can be null.
  Nmaxfiletitle
Specify the buffer size of lpstrfiletitle, in tchars. For the ANSI version, it is the number of bytes; For the Unicode version, it is the number of bytes. If lpstrfiletitle is null, this member is ignored.
  Lpstrinitialdir
Point to a string ending with an empty character. You can specify the initial directory in this string. Pointer to a NULL terminated string that can specify the initial directory. on different platforms, there are different operating rules for selecting the initial directory.
Windows 2000:
If lpstrfile contains a path, this path is the initial directory.
Otherwise, lpstrinitialdir specifies the initial directory.
If lpstrinitialdir is null and the current directory contains files of the specified filter type, the initial directory is the current directory.
Otherwise, if the application used to open the "Save As dialog box" in the past, use the recently selected path as the initial directory. However, if an application has not been running for a long time, the path it saves will be discarded.
Otherwise, the initial directory is the private file directory (my documents) of the current user ).
Otherwise, the initial directory is a desktop folder.
Windows 98:
Lpstrinitialdir specifies the initial directory.
If lpstrinitialdir is null and lpstrfile contains a path, this path is the initial directory.
Otherwise, if the current directory contains some files of the specified filtering type, the initialization directory is the current directory.
Otherwise, the initial directory is the private file directory (my documents) of the current user ).
Earlier versions of Windows and Windows NT/2000:
Lpstrinitialdir specifies the initial directory.
If lpstrinitialdir is null and lpstrfile contains a path, this path is the initial directory.
Otherwise, the initial directory is the current directory.
  Lpstrtitle
Point to the string placed in the title bar of the dialog box. If this member is null, the system uses the default title (save as or open)
  Flags
Bit Flag settings, you can use to initialize the dialog box. When the dialog box returns, the tags it sets indicate the user input. This member can be a combination of the following tags. Mark meaning
  Ofn_allowmultiselectYou can select multiple names in the specified file name list box. If you have set the ofn_explorer tag at the same time, the dialog box uses the Explorer-style user interface; otherwise, it uses the old-style user interface.
If you select more than one file, the lpstrfile buffer returns the names of all selected files in the current directory. The nfileoffset member is the offset (in bytes or characters) to the first file name, And the nfileextension member is not used. For the explorer Style dialog box, the directory and file name are separated by null, And the last file name is followed by an additional null. This format enables the Explorer-Style dialog box to return long file names containing spaces. For the Old Style dialog box, the directory and file string are separated by spaces. The function uses short file names for file names with spaces. You can use the findfirstfile function to convert short and long file names.
If you specify a custom template for the Old Style dialog box, the definition of the file name list must contain the lbs_extendedsel value.
  Ofn_createpromptIf you specify a file that does not exist, the mark dialog box prompts you whether to create the file. If you choose to create this file, the dialog box is closed and the function returns the specified name. Otherwise, the dialog box continues. If you useOfn_allowmultiselectThe dialog box allows you to specify a non-existent file.
  Ofn_dontaddtorecentWindows 2000: prevents the system from adding a shortcut to the recently used document for the selected files. To retrieve the directory location, call the shgetspecialfolderlocation function marked by csidl_recent.
  Ofn_enablehookActivate the hook function specified in the lpfnhook member.
  Ofn_enabletransferdeno=Windows 2000: when a user opens a folder, the dialog box prompts the cdn_includeitem notification message to your ofnhookproc program. The dialog box sends a notification to each project in the recently opened folder. These messages allow you to control the list of folder items displayed in the dialog box.
  Ofn_enablesizingWindows 2000, Windows 98: enables the Explorer-Style dialog box to resize with the mouse or keyboard. The explorer-style open and save as dialog box allows you to adjust the size regardless of whether the flag is set. This flag is only required when you provide a hook program or custom template. The Old Style dialog box cannot be resized.
  Ofn_enabletemplateIt is pointed out that the lptemplatename member is the name pointing to the template resource of the dialog box. This template resource can be recognized by the hinstance member in the module.
If the ofn_explorer flag is set, the system uses the specified template to create a dialog box, which is a subwindow of the default explorer Style dialog box. If the ofn_explorer tag is not set, the Old Style dialog box is used to replace the default dialog box.
  Ofn_enabletemplatehandleIndicates that the hinstance member can recognize data blocks that contain the pre-loaded dialog template. If this tag is specified, the system ignores lptemplatename.
If the ofn_explorer flag is set, the system uses the specified template to create a dialog box, which is a subwindow of the default explorer Style dialog box. If the ofn_explorer tag is not set, the system uses the template to create an Old Style dialog box to replace the default dialog box.
  Ofn_explorerIndicates that any new Explorer-style user modules are used in the open or Save As dialog box. For more information, see Explorer-style hook procedures and explorer-style custom templates.
By default, the open and save as dialog box uses the Explorer-style user interface, regardless of whether this flag is set. This tag is required only when you provide a hook program or custom template or set the ofn_allowmultiselect tag.
If you want to use the old style interface, omit the ofn_explorer tag and provide a replacement of the old style template or hook program. If you want to use the old style but do not need a custom template or hook program, simply provide a hook program and let it return false.
  Ofn_extensiondifferentThe extension of a file entered by the specified user is different from that specified by lpstrdefext. If lpstrdefext is null, the function does not use this flag.
  Ofn_filemustexistYou can only enter the name of an existing file in the Login Field of the file name. If the tag is specified and the user enters an invalid name, the dialog box shows a waiting message box. If this tag is specified, the ofn_pathmustexist tag is also used.
  Ofn_forceshowhiddenWindows 2000: forces the display of system and hidden attribute files to overwhelm the display or hidden file set by the user. Otherwise, files with system and hidden tags are not displayed.
  Ofn_hidereadonlyHide the read-only check box.
  Ofn_longnamesFor the Old Style dialog box, this mark causes the dialog box to use a long file name. If this tag is not specified, or ifOfn_allowmultiselectThe tag is also set. The Old Style dialog box uses a short file name (in 8.3 format) for a file name with spaces ).
Ignore this flag in the explorer Style dialog box. A long file name is usually displayed.
  Ofn_nochangedirIf you change the directory when searching for a file, restore the initial value of the current directory.
  Ofn_nodereferencelinksThe Guide dialog box returns the path and file name for the selected shortcut (. lnk) file. If this value is not specified, the dialog box returns the path and file name of the file referenced by this shortcut.
  Ofn_nolongnamesFor the Old Style dialog box, This identifier causes the dialog box to use the short file name (8.3 format ).
Ignore this flag in the explorer Style dialog box. A long file name is usually displayed.
  Ofn_nonetworkbuttonHide and display style buttons.
  Ofn_noreadonlyreturnSpecify that the returned file does not contain the read-only check box and is not in the write-protected directory.
  Ofn_notestfilecreateThe specified file is not created before the dialog box is closed. If the application saves the file to a created non-image sharing, this tag should be specified. When an application specifies this flag, the library cannot check write protection, the disk is full, open the drive door or network protection. The application must be careful when using this flag, because once the file is closed, it cannot be re-opened.
Ofn_novalidate specifies that the public dialog box allows invalid characters in the returned file name. Typically, the program being called uses a hook program to check the file name through the fileokstring message. If the text box in the editing control is empty or contains only spaces, the file and directory list boxes are updated. If the text box in the editing control contains something else, the nfileoffset and nfileextension settings are generated by analyzing the text. No Default extension is added to the text, and no text is copied to the buffer specified by lpstrfiletitle.
If the value specified by nfileoffset is smaller than 0, the file is invalid. Otherwise, the file name is valid. If ofn_novalidate is not specified, nfileextension and nfileoffset can be used.
  Ofn_overwritepromptIf the selected file already exists, use the Save As dialog box to generate a message box. You must confirm whether the file is overwritten.
  Ofn_pathmustexistSpecify the path and file name that the user can only enter. If this tag is used and you type an effective path and file name in the input field of the file name, the function of the dialog box displays a waiting message.
  Ofn_readonlyWhen the dialog box is created, the selected read-only check box is displayed. This flag indicates the status of the read-only check box when the dialog box is closed.
  Ofn_policawareIndicates that if the openfile function fails to be called because of a network sharing conflict, this error is ignored and the selected file name is returned in the dialog box.
If this flag is not set, when the selected file name conflicts with network sharing, the dialog box will send a notification to your hook program. If you have set the ofn_explorer flag, the dialog box will send the cdn_1_violation message to the hook program. If you have not set ofn_explorer, the dialog box will send messages registered with vistring to the hook program.
  Ofn_showhelpDisplay the Help button in the dialog box. The hwndowner member must specify a window, which serves as the helpmsgstring registered message sent in the receive dialog box. When the user clicks the Help button, the dialog box sends the message.
When you click the Help button, An Explorer-Style dialog box sends a cdn_help notification message to your hook program.
  Nfileoffset
The object name string specified from the path to the lpstrfile is 0-based offset, in tchars. For the ANSI version, it is the number of bytes; For the Unicode version, it is the number of characters. For example, if lpstrfile points to the following string "C:/dir1/dir2/file. Ext", this member contains the position value 13 indicating the "file. Ext" string offset.
If you select more than one file, nfileoffset is the offset to the first file name.
  Nfileextension
The offset from the path to the file name string specified by lpstrfile Based on 0, in tchars. For the ANSI version, it is the number of bytes; For the Unicode version, it is the number of bytes. For example, if lpstrfile points to the following string "C:/dir1/dir2/file. Ext", this member contains a value of 18. If you do not enter an extension and lpstrdefext is null, the offset specified by this member is the end character null. If you enter a "." As the final character in the file name, the Member is 0.
  Lpstrdefext
Point to the buffer containing the default extension. If you forget to enter the extension, getopenfilename and getsavefilename will be appended to the file name. This string can be any length, but only the first three characters are appended. A string cannot contain a period (.). If the member is null and the user forgets to enter an extension, no extension is appended.
  Lcustdata
Specifies the data defined by the application. This data is a hook program that can be uploaded by the system recognized by lpfnhook members. When the system sends the wm_initdialog message to the program, the lparam parameter of the message points to the openfilename structure specified when the dialog box is created. The hook program can use this pointer to obtain the value of lcustdata.
  Lpfnhook
Points to a hook program. Unless the flags member contains the ofn_enablehook tag, the Member is ignored.
If the ofn_explorer flag in the flags member is not set, lpfnhook points to an ofnhookprocoldstyle hook program, which intentionally receives messages from the dialog box. The hook program returns false to pass a message to the default Dialog Box program or returns true to discard the message.
If ofn_explorer is set, lpfnhook points to an ofnhookproc hook program. This hook program receives notifications from the dialog box. This hook program also receives messages from additional controls defined by a subdialog template. The hook program does not intentionally receive messages from standard controls in the default dialog box.
  Lptemplatename
Point to a string ending with an empty character. The string is the name of the template resource in the dialog box. The resource is saved in a module that can be recognized by hinstance members. For limited dialog box resources, this can be the value returned by makeintresource. Unless the ofn_enabletemplate tag is set in the flags member, the Member is ignored.
If the ofn_explorer flag is set, the system uses the specified template to create a dialog box, which is a subwindow of the default explorer Style dialog box. If the ofn_explorer tag is not set, the system uses the template to create an Old Style dialog box to replace the default dialog box.
  Pvreserved
Retained.
  Dwreserved
Retained.
  Flagsex
Windows 2000: Set the bit flag. You can use it to initialize the dialog box. This member can be a combination of the following tags. Mark meaning
  Ofn_ex_noplacesbarIf this tag is set, the location bar is not displayed. If this tag is not set, the explorer-Style dialog box contains the location bar of commonly used file icons, such as favorites and desktop.

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.