Play Win32 development with me (19): browse and open files

Source: Internet
Author: User

In the ApplicationProgram. In this article, I will introduce two ideas: the first method is more complex, and the second method is simpler.

Method 1: Old Rules

This is a traditional method. If you use the getopenfilename or getsavefilename function, you can see the name. The former is used to open the file, and the latter is used to save the file. The usage of the two functions is the same. Therefore, I will show only one of them.

No matter which function you use, a struct -- openfilename must be involved. I will not talk about its members one by one. I will pick a few useful tips.

Lstructsize: The size of the structure. Just get a sizeof.

Lpstrfilter: Set the filter. Note that this filter string and.. NET is written differently ,. net is inherited from VB6 and can be written as "junk files | *. RBS | mouse file | *. mos ". We do not use" | "to separate them, but use" \ 0 "to separate them.The end is two null, that is, two "\ 0".

Nfilterindex: Filter index. N suffixes are set for filtering, which one is selected by default, the first one is 1, the second one is 2, and the third one is 3. So on, it is from 1, not 0.

Lpstrfile: File name, including the complete path. When the dialog box is closed, we retrieve the selected file name from this Member.

Nmaxfile: The length of the file name, which must be large enough, otherwise it will not be able to be installed. For the path length, the system has a limitation. You can use the max_path macro, wchar myfilename [max_path].

Flags: Flag. It mainly determines the characteristics and behaviors of the dialog box, such as checking whether the target file already exists.

Oh, you may not feel it. Let's look at the example.Code.

 
Openfilename opfn; wchar strfilename [max_path]; // stores the file name // initializes zeromemory (& opfn, sizeof (openfilename); opfn. lstructsize = sizeof (openfilename); // struct size // set the opfn filter. lpstrfilter = l "all files \ 0 *. * \ 0 text file \ 0 *. TXT \ 0mp3 file \ 0 *. MP3 \ 0 "; // The default filter index is set to 1opfn. nfilterindex = 1; // the first character must be set to \ 0opfn for the file name field. lpstrfile = strfilename; opfn. lpstrfile [0] = '\ 0'; opfn. nmaxfile = sizeof (strfilename); // sets the flag to check whether the directory or file contains opfn. flags = ofn_filemustexist | ofn_pathmustexist; // opfn. lpstrinitialdir = NULL; // The displayed dialog box selects the file if (getopenfilename (& opfn) {// display the file path hwnd hedt = getdlgitem (hdlg, idc_edtfilename) in the text box ); sendmessage (hedt, wm_settext, null, (lparam) strfilename );}

The zeromemory function has been mentioned before. You just need to initialize the structure as it is.

You may wonder why the lpstrfilter string ends with two null characters. Why is there only one character string in the code? Because the string you entered will automatically add '\ 0' in the back, so we can add one to OK, and then add one to the back, and there will be two.

Is the execution result:

 

 

The usage of getsavefilename is the same. If you are interested, you can go home and have a try.

 

 

Method 2: New Rules

Who set this new rule? Haha, I agree.

It is said that knowledge can be used comprehensively. Therefore, the following method is to use knowledge flexibly. Why? Remember, the. NET class library is actually encapsulated for us. If you have used Windows Forms for development, you must know that it is under system. Windows. forms. Yes. Since there is a ready-made product, why don't we use it.

First, open the project properties and follow the method to enable project support/CLR, and then click "application ".

 

Reference the required assembly.

 

Introduce the namespace in the code file.

 
Using namespace system; using namespace system: Windows: forms;

Then enable the file function.

 
If (loword (wparam) = idc_btnopwtclr) {openfiledialog ^ DLG = gcnew openfiledialog; DLG-> filter = l "all files | *. * | image file | *. jpg "; DLG-> filterindex = 1; DLG-> checkfileexists = true; If (DLG-> showdialog () = dialogresult: OK) {// retrieve file name }}

However, when obtaining the file name, we encountered a serious problem. In CLR, the obtained file name is of the system: String ^ type, however, the Win32 program here requires a string of the wchar_t type. What should we do? Can it be converted?

Microsoft has long thought of this problem. Therefore, it has written a header file vcclr. H, which contains a ptrtostringchars function to convert the hosted string into a standard string.

 
If (DLG-> showdialog () = dialogresult: OK) {// get the file name pin_ptr <const wchar> strfilename = ptrtostringchars (DLG-> filename ); hwnd hedt = getdlgitem (hdlg, idc_edtfilename); sendmessage (hedt, wm_settext, null, (lparam) strfilename );}

However, the problem persists. You will be pleasantly surprised to receive an exception message after running.

 

Lucky. How can this problem be solved? Open the Project Properties window again,Find the "linker"-"advanced" and change the CLR thread model to sta..

 

Run the application now. It is estimated that there is no problem.

 

 

I will upload the code later.

Okay, okay. Now we're done. Bye bye.

 

 

 

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.