One-day Windows API training (92) getopenfilename Function

Source: Internet
Author: User
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}

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.