Windows Vista for developers -- Part 6: New File Dialog Box

Source: Internet
Author: User

 

Author: Kenny Kerr

Translation: dflying Chen

Original article: http://weblogs.asp.net/kennykerr/archive/2006/11/10/Windows-Vista-for-Developers-_1320_-Part-6-_1320_-The-New-File-Dialogs.aspx

For more information, see Windows Vista for developers.

 

Just as the aero wizard is more friendly than the traditional wizard and the task dialog box is more friendly than the old message box, the latest file dialog box in Windows Vista also gives users a new experience, instead of those yearsGetopenfilenameAndGetsavefilenameFunction. The latest file dialog box is not only consistent with the appearance of Windows Vista, but also provides a new COM interface, which simplifies the usage and leaves sufficient space for future expansion.

In article 6 of the Windows Vista for developers series, let's take a look at these latestIfiledialogThe file dialog box API provided by related interfaces. This article will first look at the relevant interfaces, and then use a C ++ class template to simplify their usage. Before enteringCodeBefore that, let's take a look at what changes this new file dialog box can bring to users.

 

User Experience

The following is how to useGetopenfilenameThe traditional open file dialog box obtained by the function:

Most users are familiar with this dialog box, but for those users who are used to Windows Vista, this dialog box seems a bit confusing and seems incompatible with Windows Vista. The folder navigation method is different from the resource manager of Vista. Many new things in Vista, such as the built-in search box, are missing. In Vista, the resource manager style is as follows:

The following is a dialog box for the new version of Windows Vista to open a file:

Except the panel at the bottom, the other parts are very similar to the Resource Manager window. This consistency ensures that users are comfortable with using Windows Vista. Next let's take a look at how to applyProgramUse this latest open file dialog box.

 

Traditional Method

For reference, we first look at how most Windows developers (using C ++) create a traditional file dialog box. Most developers do not directly useGetopenfilenameOrGetsavefilenameFunction, but uses the MFC or wtlCfiledialogClass. The following is an example in wtl:

 
ClassOldsampledialog:PublicCfiledialog
 
{
 
Public:
 
Oldsampledialog ():
Cfiledialog (True)// Open dialog
 
{
 
// Todo: Customize dialog with m_ofn
 
}
 
};
 
Oldsampledialog dialog;
 
Int_ptr result = dialog. domodal ();

Both MFC and wtl do not support the new file dialog box, at least not when I write this article, however, one of the purposes of this article is to encapsulate a similar and convenient helper class in C ++. First, let's take a look at these new APIs.

 

New API

As I mentioned earlier, the latest file dialog API is provided to developers through a series of COM interfaces. Below are some of the most common ones:

    1. Ifileopendialog:FileopendialogCom class implementation, provides methods to open the file dialog box.
    2. Ifilesavedialog:FilesavedialogCom class implementation, provides methods for saving the file dialog box.
    3. Ifiledialog:IfileopendialogAndIfilesavedialogProvides the implementation of most of the functions that interact with and customize the dialog box.
    4. Imodalwindow:Ifiledialog. The show method is provided.
    5. Ifiledialogcustomize:FileopendialogAndFilesavedialogCom class implementation provides support for adding new controls in the dialog box.
    6. Ifiledialogevents: Implemented by the application to listen for events in the dialog box.
    7. Ifiledialogcontrolevents: Implemented by the application to implement the event function of the control added in the listener dialog box.

We may also use some APIs that are not brand new, suchIshellitem. This article will be discussed later.

We can useCcomptrClass template to create an open file dialog box, the Code is as follows:

 
Ccomptr <ifileopendialog> dialog;
Hresult result = dialog. cocreateinstance (_ uuidof (fileopendialog ));

Generally, we useIfiledialogThe inherited method is used to customize the dialog box. This part will be introduced later. You can useIfiledialogcustomizeFunctions provided by the interface:

 
Ccomptr <ifiledialogcustomize> customize;
 
Hresult result = dialog. QueryInterface (& customize );

After the preparation is complete, the dialog box is displayed:

 
Hresult result = dialog-> show (parentwindow );

The show method is blocked until the dialog box is closed.

 

File Type

We should know,IfiledialogNot used fromOpenfilenameOfReg_multi_szUse a more simpleComdlg_filterspecStructure to specify the types of files that the dialog box can receive (Open/Save:

 
Comdlg_filterspec filetypes [] =
 
{
{L"Text documents", L"*. Txt"},
 
{L"All Files", L"*.*"}
 
};
 
Hresult result = dialog-> setfiletypes (_ countof (filetypes ),
 
Filetypes );

This model simplifies the process of creating a list of file types dynamically, and does not need to introduce the headache string table. The following example demonstrates how to use string tables for Localization support:

 
Cstring textname;
 
Verify (textname. loadstring (ids_filter_text_name ));
 
Cstring textpattern;
 
Verify (textpattern. loadstring (ids_filter_text_pattern ));
 
Cstring allname;
 
Verify (allname. loadstring (ids_filter_all_name ));
 
Cstring allpattern;
Verify (allpattern. loadstring (ids_filter_all_pattern ));
 
Comdlg_filterspec filetypes [] =
 
{
 
{Textname, textpattern },
 
{Allname, allpattern}
 
};

SetfiletypeindexThe method allows us to set the selected file type in the dialog box. Note that this index starts from 1, rather than starting from 0, which is familiar to C or C ++ programmers.

 
Hresult result = dialog-> setfiletypeindex (2 );

You can callSetfiletypeindexMethod To change the selected file type. WhileGetfiletypeindexIt can be called when the dialog box is opened or closed.

 
Uint Index = 0;
 
Hresult result = dialog-> getfiletypeindex (& Index );

 

File Dialog Box options

The file dialog box provides many options. You can use these options to control the style and behavior of the dialog box. These options are declared as tags and encapsulated inDWORD. The options of the current application can beGetoptionsMethod. After modification, you can useSetoptionsMethod reset. Unless you are very clear about the meaning of each option, you 'd better callGetoptionsMethod, and then useSetoptionsMethod reset. In this way, you can avoid losing some preset options.

The following code demonstrates how to forcibly open the preview panel:

 
DWORD Options = 0;
 
Hresult result = dialog-> getoptions (& options );
 
If(Succeeded (result ))
 
{
 
Options | = fos_forcepreviewpaneon;
 
Result = dialog-> setoptions (options );
 
}

Of course, you can also manually display/hide the preview panel (via the organize drop-down menu on the toolbar ):

The document of the setoptions method details all the options we can use.

 

Label and text box controls

You can also change the default values of some text elements in the dialog box.

SettitleYou can set the title of the dialog box.SetokbuttonlabelTo set the text on the default button of the dialog box.SetfilenamelabelThis method is used to set the text on the label next to the file name text box control.

We can also useSetfilenameAndGetfilenameMethod To Set/obtain the text in the file name text box control.

 

 

Shell project

Many interfaces use shell projects instead of File System paths to reference folders to control file dialogs. This is because the dialog box can not only deal with the real folder in the file system, but also reference some "virtual" folders. For example, the "computer" folder contains Control Panel entries. To learn more about common shell projects (whether virtual projects or not), refer to my known folders browser.Article.

Shell projects are very flexible. With their help, we can easily acceptIshellitem. But if you only know the file system path, you must convert it to the corresponding Shell project. Fortunately, Windows Vista introduces a newShcreateitemfromparsingnameTo help you do this:

 
Ccomptr <ishellitem> shellitem;
Hresult result =: shcreateitemfromparsingname (L"D: \ samplefolder",
 
0,
 
Iid_ishellitem,
 
Reinterpret_cast <Void**> (& Shellitem ));

Similarly, if you want to get the file system path of a shell project, you can useGetdisplaynameMethod:

 
Autotaskmemory <wchar> buffer;
 
Hresult result = shellitem-> getdisplayname (sigdn_filesyspath,
 
& Buffer. P );

HereAutotaskmemoryIs a simple class template. In its destructorCotaskmemfreeMethod to releaseGetdisplaynameThe memory allocated by the function. Note that shell projects do not always point to a document system path. For example, if you pass ': {ED228FDF-9EA8-4870-83b1-96b02CFE0D52}'ShcreateitemfromparsingnameFunction to obtain the Shell project pointing to the "games" virtual folder. Because the virtual Folder does not exist in the file systemGetdisplaynameMethod will fail.

After a brief introduction to the shell project, we should also go back to the topic. Next let's take a look at how the file dialog box uses the Shell project.IfiledialogInterface to use shell projects to identify folders and select. For example, you can set the initial folder of the file dialog box according to the following code:

 
Cstring Path =// Load path...
 
Ccomptr <ishellitem> shellitem;
 
Hresult result =: shcreateitemfromparsingname (path,
 
0,
 
Iid_ishellitem,
Reinterpret_cast <Void**> (& Shellitem ));
 
If(Succeeded (result ))
 
{
 
Result = m_dialog-> setfolder (shellitem );
 
}

After the dialog box is opened, we can still callSetfolderMethod to navigate to the specified folder in the dialog box. WhileGetfolderYou can obtain the folder to be displayed in the file dialog box at the initial time. If the dialog box has already been called, you can get the file %

Related Article

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.