Windows API one-day training (93) getsavefilename Function
The previous section describes how to open the Select File Read dialog box. When the software you design requires users to choose to save the file, they need to select their desired file name to save it. Then you need to use the save file dialog box, that is, the API function getsavefilename. The following shows how to use this API function.
The getsavefilename function declaration is as follows:
Wincommdlgapi bool apientry getsavefilenamea (lpopenfilenamea );
Wincommdlgapi bool apientry getsavefilenamew (lpopenfilenamew );
# Ifdef Unicode
# Define getsavefilename getsavefilenamew
# Else
# Define getsavefilename getsavefilenamea
# Endif //! Unicode
An example of calling a function is as follows:
#001 // obtain the name of the file to be saved by the user.
#002 // Cai junsheng 2007/12/25 QQ: 9073204 Shenzhen
#003 void testgetsavefilename (void)
#004 {
#005 //
#006 openfilename ofn; // structure of the public dialog box.
#007 tchar szfile [max_path]; // Save the buffer for obtaining the file name.
#008
#009 // initialize the select file dialog box.
#010 zeromemory (& ofn, sizeof (ofn ));
#011 ofn. lstructsize = sizeof (ofn );
#012 ofn. hwndowner = m_hwnd;
#013 ofn. lpstrfile = szfile;
#014
#015 //
#016 ofn. lpstrfile [0] = _ T ('/0 ');
#017 ofn. nmaxfile = sizeof (szfile );
#018 ofn. lpstrfilter = _ T ("All/0 *. */0 text/0 *. txt/0 ");
#019 ofn. nfilterindex = 1;
#020 ofn. lpstrfiletitle = NULL;
#021 ofn. nmaxfiletitle = 0;
#022 ofn. lpstrinitialdir = NULL;
#023 ofn. Flags = ofn_showhelp | ofn_overwriteprompt;
#024
#025 // The select file dialog box is displayed.
#026 if (getsavefilename (& ofn ))
#027 {
#028 // display the selected file.
#029 outputdebugstring (szfile );
#030 outputdebugstring (_ T ("/R/N "));
#031}
#032}