Several common class--cfiledialog in MFC

Source: Internet
Author: User

1 Introduction The CFileDialog class encapsulates the Windows Common Files dialog box. The frequently used file dialogs provide a simple file opening and File Save dialog box that is consistent with Windows standards. You can use CFileDialog in a way that the constructor provides, or you can derive your own dialog class from CFileDialog and write a constructor to suit your needs. In each case, the dialog box works just like the standard MFC dialog box. Because they are all derived classes of the Ccommondialog class. To use CFileDialog, first construct an object with the CFileDialog constructor, and when you have created a dialog box, you can set or modify any value in the M_OFN structure to initialize the value or state of the dialog box control. The M_OFN structure is of the openfilename type. For more information, refer to the OPENFILENAME structure in the online documentation "Win32 SDK". After initializing the dialog box control, call the DoModal member function to display the dialog box and enable the user to enter the path and file. DoModal returns whether the user selects the OK (IDOK) or Cancel (IDCANCEL) button. When DoModal returns IDOK, you can use the public member function of one of the CFileDialog to get the information entered by the user. CFileDialog contains a number of protection members that allow you to handle common sharing conflicts, file name legality checks, and list box change notifications. These protection members are of little use to many applications because the default processing is automatic. For these functions, the message-map portals are unnecessary because they are standard virtual functions. You can use the Windows Commdlgextenderror function to determine whether an error occurred while initializing the dialog box and get more information about the error. Destructors a CFileDialog object is automatic without calling Cdialog::enddialog. To enable users to select multiple files, set the OFN_ALLOWMULTISELECT flag before calling DoModal. You should provide a file name buffer to place a list of multiple filenames that are returned, which is done by replacing m_ofn.lpstrfile with an assigned buffer pointer, before calling DoModal after the CFileDialog has been created. In addition, you must set M_ofn.nmaxfile with the number of buffer bytes that m_ofn.lpstrfile points to. CFileDialog relies on the COMMDLG.DLL in Windows3.1 and later versions. If a new class is derived from CFileDialog, the message map can be used for processing. To extend message handling, from CWnd, you can add a message map to the new class and provide a member function for the new message without having to provide a hook function to customize the dialog box. To customize a dialog box, derive an object from CFileDialog, provide a custom dialog template, add a message map from the extension control, and process the notification message. Any unhandled messages are passed to the base class. No need to customize the hook function. Members of the #include <afxdlgs.h> CFileDialog class2 Inheritance System Cobject└ccmdtarget└cwnd└cdialog└ccommondialog└cfiledialog3 data members m_ofn the WINDOWS openfilename structure, providing access to basic file dialog box parameters 4 member functions   CFileDialog constructs a CFileDialog object operation DoModal displays a dialog box and enables the user to make a selection getpathname returns the full path of the selected file GetFileName returns the file name of the selected files Getfileext returns the extension file name of the selected file GetFileTitle returns the title of the selected file Getnextpathname returns the full path of the next selected file Getreadonlypref returns the read-only status of the selected file GetStartPosition returns the name of the first element in the list of file names that can be overridden by a function onshareviolation when a sharing violation occurs Onfilenameok confirm the type of filename in the dialog box Onlbselchangednotify call Oninitdone process wm_notify cdn_initdone message when list box selection changes OnFileNameChange process wm_notify Cdn_selchange message Onfolderchange handles the use of WM_NOTIFY cdn_folderchange messages Ontypechange processing wm_notify cdn_typechange Message File Selection dialog box: First constructs an object and provides the corresponding parameters, The constructor prototype is as follows: Cfiledialog::cfiledialog (BOOL bopenfiledialog, LPCTSTR lpszDefExt = null, LPCTSTR lpszFileName = null, DWORD DW Flags = Ofn_hidereadonly | Ofn_overwriteprompt, LPCTSTR lpszfilter = null, cwnd* pParentWnd = null); The parameters are as follows: Bopenfiledialog is true to display the Open dialog box, or False to display the Save Dialog File dialog box. LPSZDEFEXT Specifies the default file name extension. LPSZFILENAME Specifies the default file name. DwFlags indicates some particular style. Lpszfilter is the most important parameter that indicates the type of file to choose from 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 (*. *) |*.*| | "; File type description and extension between | Delimited, can be used between extensions of the same type of file; Split, between each file type | Delimited, End with | | Specified. pParentWnd is the parent window pointer. The Create File dialog box can use DoModal (), which can be returned after the user chooses the following function: CString cfiledialog::getpathname () Gets the full file name, including the directory name and extension such as C: \ test\ Test1.txt CString Cfiledialog::getfilename () get the full file name, including extension such as: Test1.txt CString cfiledialog::getextname () to get the full file extension, such as: TXT CString cfiledialog::getfiletitle () get the full file name, excluding directory name and extension such as: Test1 POSITION cfiledialog::getstartposition () For cases where multiple files are selected, the first file location is obtained. CString cfiledialog::getnextpathname (position& Pos) Gets the next file location for cases where multiple files are selected, and returns the current file name at the same time. However, you must have already called position cfiledialog::getstartposition () to get the initial position variable. For example {CString filepathname; CFileDialog dlg (TRUE);///true is the Open dialog box, False is the Save As dialog box if (dlg. DoModal () ==idok) Filepathname=dlg. GetPathName (); Related information: CFileDialog several member functions for taking a file name: If the selected file is C:windowstest. EXE: (1) getpathname (); Take the full file name, including the complete path. Retrieve the C:\WINDOWS\TEST. EXE (2) GetFileName (); Take file Full name: TEST.EXE (3) GetFileTitle (); retrieve TEST (4) Getfileext (); Take extension EXE supplement: Use this class under the consoleTo set up the use of MFC in a static library, then construct AfxSetResourceHandle (GetModuleHandle (NULL)); Related header files #include <Afxdlgs.h> 5 style details   DWFLAGS flags A set of bit flags you can use to initialize the dialog box. When the dialog box returns, it sets these flags to indicate the user's input. This member can be a combination of the following flags. OFN_ALLOWMULTISELECT specifies that the File Name list box allows multiple selections. If you also set the Ofn_explorer flag, the dialog box uses the Explorer-style user interface; Otherwise, it uses the Old-style user interface. If the user selects more than one file, the lpstrfile buffer returns the path to the current directory followed by the FIL E names of the selected files. The Nfileoffset member is the offset, in bytes or characters, to the first file name, and the Nfileextension member are not Used. For Explorer-style dialog boxes, the directory and file name strings is null separated, with an extra NULL character afte R the last file name. This format enables the Explorer-style dialog boxes to return a long file names that include spaces. For Old-style Dialog boxes, the directory and file name strings is separated by spaces and the function uses short file names for file names With spaces. You can use the FindFirstFile function-to-convert between long and short file names. If you specify a custom template for an Old-style dialog box, the definition of the File Name list box must contain the LB S_extendedsel value. Ofn_createprompt If The user specifies a file that does not exist, this flag causes the dialog box to prompt the user for Permission to create the file. If the user chooses to create the file, the dialog box closes and the function returns the specified name; Otherwise, the dialog box remains open. If You use this flag with the OFN_ALLOWMULTISELECT flag, the dialog box allows the user to specify only one nonexistent fi Le. Ofn_dontaddtorecent Windows 2000/xp:prevents the system from adding a link to the selected file in the file system direct Ory that contains the user's most recently used documents. To retrieve the location of this directory, call the SHGetSpecialFolderLocation function with the csidl_recent flag. Ofn_enablehook enables the hook function specified in the Lpfnhook member. Ofn_enableincludenotify Windows 2000/xp:causes The dialog box to send cdn_includeitem notification messages to your Ofnho Okproc Hook procedure When the user opens a folder. The dialog box sends a notification for each item in the newly opened folder. These messages enable you to control which items, the dialog box displays in the folder ' s item list. ofn_enablesizing windows 2000/XP, Windows 98/me:enables the Explorer-style dialog box to be resized using either the MOUS E or the keyboard. By default, the Explorer-style Open and Save As dialog boxes allow the dialog box to being resized regardless of whether this Flag is set. This flag was necessary only if you provide a hook procedure or custom template. The Old-style dialog box does not permit resizing. Ofn_enabletemplate indicates that the Lptemplatename member are a pointer to thE name of a dialog template resource in the module identified by the HINSTANCE member. If the Ofn_explorer flag is set, the system uses the specified template to create a dialog box, which is a child of the DEFA Ult Explorer-style dialog box. If the OFN_EXPLORER flag is no set, the system uses the template to create a Old-style dialog box that replaces the DEFA Ult dialog box. Ofn_enabletemplatehandle indicates that the HINSTANCE member identifies a data block that contains a preloaded dialog box Template. The system ignores Lptemplatename if this flag is specified. If the Ofn_explorer flag is set, the system uses the specified template to create a dialog box, which is a child of the DEFA Ult Explorer-style dialog box. If the OFN_EXPLORER flag is no set, the system uses the template to create a Old-style dialog box that replaces the DEFA Ult dialog box. Ofn_explorer indicates that any customizations made to the Open or Save as dialog box use the new Explorer-style Customiza tion methods. FoR more information, see Explorer-style Hook procedures and Explorer-style Custom Templates. By default, the Open and Save as dialog boxes with the Explorer-style user interface regardless of whether this flag is set . This flag was necessary only if you provide a hook procedure or custom template, or set the OFN_ALLOWMULTISELECT flag. If you want the Old-style user interface, omit the OFN_EXPLORER flag and provide a replacement old-style template or hook Procedure. If you want the old style but does not need a custom template or hook procedure, simply provide a hooks procedure that always Returns FALSE. Ofn_extensiondifferent specifies that the user typed a file name extension this differs from the extension specified by LP Strdefext. The function does not use this flag if Lpstrdefext is NULL. OFN_FILEMUSTEXIST Specifies that the user can type is names of existing files in the File Name entry field. IF this flag was specified and the user enters an invalid name, the dialog box procedure dispLays a warning in a message box. IF This flag was specified, the OFN_PATHMUSTEXIST flag is also used. This flag can is used in an Open dialog box. It cannot is used with a Save as dialog box. Ofn_forceshowhidden Windows 2000/xp:forces the showing of system and hidden files, thus overriding the user setting to SH ow or not show hidden files. However, a file is marked both system and hidden are not shown. Ofn_hidereadonly hides the Read only check box. Ofn_longnames for Old-style dialog boxes, this flag causes the dialog box to use long file names. If this flag isn't specified, or if the OFN_ALLOWMULTISELECT flag is also set, Old-style dialog boxes use short file name S (8.3 format) for file names with spaces. Explorer-style dialog boxes ignore this flag and always display long file names. Ofn_nochangedir restores the current directory is original value if the user changed the directory while searching for Files. Windows NT 4.0/2000/xp:this flag is ineffective for getopenfilename. Ofn_nodereferencelinks directs the dialog box to return the path and file name of the selected shortcut (. LNK) file. If This value isn't specified, the dialog box returns the path and file name of the file referenced by the shortcut. Ofn_nolongnames for Old-style dialog boxes, this flag causes the dialog box to use short file names (8.3 format). Explorer-style dialog boxes ignore this flag and always display long file names. Ofn_nonetworkbutton hides and disables the Network button. Ofn_noreadonlyreturn specifies that the returned file does not has the Read only check box selected and was not in a write -protected directory. OFN_NOTESTFILECREATE Specifies that the file was not created before the dialog box was closed. This flag should is specified if the application saves the file on a create-nonmodify network share. When an application specifies this flag, the library does not check for write protection, a full disk, an open drive door, or network protection. Applications using this FLag must perform file operations carefully, because a file cannot be reopened once it is closed. OFN_NOVALIDATE Specifies that the common dialog boxes allow invalid characters in the returned file name. Typically, the calling application uses a hook procedure that checks the file name by using the fileokstring message. If the text box in the edit control was empty or contains nothing but spaces, the lists of files and directories was update D. If the text box in the edit control contains anything else, Nfileoffset and nfileextension is set to values generated By parsing the text. No default extension is added to the text, nor is the text copied to the buffer specified by Lpstrfiletitle. If the value specified by Nfileoffset was less than zero, the file name is invalid. Otherwise, the file name is valid, and nfileextension and Nfileoffset can are used as if the OFN_NOVALIDATE flag had not being En specified. Ofn_overwriteprompt causes the Save as dialog box to generate a message box if the SELECTED file already exists. The user must confirm whether to overwrite the file. OFN_PATHMUSTEXIST Specifies that the user can type only valid paths and file names. IF This flag is used and the user types a invalid path and file name in the File Name entry field, the dialog box Functio N Displays a warning in a message box. Ofn_readonly causes the Read only check box to being selected initially when the dialog box is created. This flag indicates the state of the "Read only" check box when the dialog box is closed. Ofn_shareaware specifies that if a call to the OpenFile function fails because of a network sharing violation, the error I s ignored and the dialog box returns the selected file name. IF This flag isn't set, the dialog box notifies your hook procedure when a network sharing violation occurs for the file Name specified by the user. If you set the OFN_EXPLORER flag, the dialog box sends the CDN_SHAREVIOLATION message to the hook procedure. If you does not set Ofn_explorer, the dialog box sEnds the sharevistring registered message to the hook procedure. Ofn_showhelp causes the dialog box to display the Help button. The hWndOwner member must specify the window to receive the helpmsgstring registered messages that the dialog box sends WH En the user clicks the Help button. An Explorer-style dialog box sends a CDN_HELP notification message to your hooks procedure when the user clicks the Help BU Tton. Ofn_useshellitem does not use.

Several common class--cfiledialog in MFC

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.