One-day Windows API training (92) getopenfilename Function
To open a previously saved file, you need to use the select file dialog box. In fact, there is already such a perfect conversation in the Windows API. You only need a simple call to use it, instead of writing a lot of other code. The following shows how to use the file selection dialog box.
The getopenfilename function declaration is as follows:
Wincommdlgapi bool apientry getopenfilenamea (lpopenfilenamea );
Wincommdlgapi bool apientry getopenfilenamew (lpopenfilenamew );
# Ifdef Unicode
# Define getopenfilename getopenfilenamew
# Else
# Define getopenfilename getopenfilenamea
# Endif //! Unicode
Lpopenfilenamea refers to the structure of the file selection dialog box.
An example of calling a function is as follows:
#001 // obtain the name of the selected file.
#002 // Cai junsheng 2007/12/21 QQ: 9073204 Shenzhen
#003 void testgetopenfilename (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_pathmustexist | ofn_filemustexist;
#024
#025 // The select file dialog box is displayed.
#026 if (getopenfilename (& ofn ))
#027 {
#028 // display the selected file.
#029 outputdebugstring (szfile );
#030 outputdebugstring (_ T ("/R/N "));
#031
#032}
#033}