Vistaでは、従来のGetOpenFileName/GetSaveFileNameに代わってFile Dialogというものが導入されています。せっかくなので試してみました。

#console

Function GetSaveFileDialog() As String
	Dim fd As IFileDialog
	Dim hr = CoCreateInstance(
		CLSID_FileSaveDialog, 0, CLSCTX_ALL,
		IID_IFileDialog, fd)
	If FAILED(hr) Then Exit Function

	Try
		hr = fd.Show(0)
		If FAILED(hr) Then Exit Function
		Dim si As IShellItem
		hr = fd.GetResult(si)
		If FAILED(hr) Then Exit Function
		Dim psz As PWSTR
		hr = si.GetDisplayName(SIGDN_FILESYSPATH, psz)
		If FAILED(hr) Then Exit Function
		GetSaveFileDialog = New String(psz)
	Finally
		fd.Release()
	End Try
End Function

CoInitialize(0)
Print GetSaveFileDialog()
System.Console.ReadLine()
CoUninitialize()

ファイルダイアログ(保存)とりあえず名前を付けて保存ダイアログを表示し、入力されたファイル名をコンソールにPrintしているだけです。

さて、ここで1つ重要なお知らせがあります。ABプログラムでは、通常IFileDialogを使う必要はありません。GetOpenFileName/GetSaveFileNameを直接呼ぶので、そのままでFile Dialogと同じ外観になります。なお、OFN_ENABLEHOOKを使うとだめになるので、そのときがやっとIFileDialogの出番です。

もしかしたら、そのうちGetOpenFileName/GetSaveFileNameをラップしたクラスを作るかもしれませんが、その際もVistaで新スタイルになるよう留意して製作します。もしダメだったら、Vistaではファイルダイアログを使うというコードを書きますけどね。

最後にこれに必要な宣言類を置いておきます。

Dim CLSID_FileSaveDialog = [&hC0B4E2F3, &hBA21, &h4773, [&h8D, &hBA, &h33, &h5E, &hC9, &h46, &hEB, &h8B]] As CLSID
Dim IID_IFileDialog = [&h42F85136, &hDB7E, &h439C, [&h85, &hF1, &hE4, &h07, &h5D, &h13, &h5F, &hC8]] As IID

Const Enum SIGDN
	SIGDN_NORMALDISPLAY = 0
	SIGDN_PARENTRELATIVEPARSING = &h80018001
	SIGDN_DESKTOPABSOLUTEPARSING = &h80028000
	SIGDN_PARENTRELATIVEEDITING = &h80031001
	SIGDN_DESKTOPABSOLUTEEDITING = &h8004c000
	SIGDN_FILESYSPATH = &h80058000
	SIGDN_URL = &h80068000
	SIGDN_PARENTRELATIVEFORADDRESSBAR = &h8007c001
	SIGDN_PARENTRELATIVE = &h80080001
End Enum

TypeDef SFGAOF = DWord
TypeDef SICHINTF = DWord

Interface IShellItem
	Inherits IUnknown

	Function BindToHandler(
		/* [unique][in] */ pbc As IBindCtx,
		/* [in] */ ByRef bhid As GUID,
		/* [in] */ ByRef riid As GUID,
		/* [iid_is][out] */ ByRef ppv As Any) As HRESULT
	Function GetParent( 
		/* [out] */ ByRef si As IShellItem) As HRESULT
	Function GetDisplayName( 
		/* [in] */ sigdnName As SIGDN,
		/* [string][out] */ ByRef pszName As LPWSTR) As HRESULT
	Function GetAttributes( 
		/* [in] */ sfgaoMask As SFGAOF,
		/* [out] */ ByRef sfgaoAttribs As SFGAOF) As HRESULT
	Function Compare( 
		/* [in] */ si As IShellItem,
		/* [in] */ hint As SICHINTF,
		/* [out] */ ByRef iOrder As Long) As HRESULT
End Interface

Interface IModalWindow
	Inherits IUnknown

	Function Show( 
		/* [in] */ hwndParent As HWND) As HRESULT
End Interface


TypeDef COMDLG_FILTERSPEC = DWord
TypeDef FDAP = DWord

Interface IFileDialog
	Inherits IModalWindow
	
	Function SetFileTypes( 
		/* [in] */ cFileTypes As DWord,
		/* [size_is][in] */ rgFilterSpec As * /*const*/ COMDLG_FILTERSPEC) As HRESULT
	Function SetFileTypeIndex( 
		/* [in] */ iFileType As DWord) As HRESULT
	Function GetFileTypeIndex( 
		/* [out] */ ByRef iFileType As DWord) As HRESULT
	Function Advise( 
		/* [in] */ fde As IUnknown /*IFileDialogEvents*/,
		/* [out] */ ByRef dwCookie As DWord) As HRESULT
	Function Unadvise( 
		/* [in] */ dwCookie As DWord) As HRESULT
	Function SetOptions( 
		/* [in] */ fos As DWord) As HRESULT
	Function GetOptions( 
		/* [out] */ ByRef fos As DWord) As HRESULT
	Function SetDefaultFolder( 
		/* [in] */ si As IShellItem) As HRESULT
	Function SetFolder( 
		/* [in] */ si As IShellItem) As HRESULT
	Function GetFolder( 
		/* [out] */ ByRef si As IShellItem) As HRESULT
	Function GetCurrentSelection( 
		/* [out] */ ByRef si As IShellItem) As HRESULT
	Function SetFileName( 
		/* [string][in] */ pszName As LPCWSTR) As HRESULT
	Function GetFileName( 
		/* [string][out] */ ByRef pszName As LPWSTR) As HRESULT
	Function SetTitle( 
		/* [string][in] */ pszTitle As LPCWSTR) As HRESULT
	Function SetOkButtonLabel( 
		/* [string][in] */ pszText As LPCWSTR) As HRESULT
	Function SetFileNameLabel( 
		/* [string][in] */ pszLabel As LPCWSTR) As HRESULT
	Function GetResult( 
		/* [out] */ ByRef si As IShellItem) As HRESULT
	Function AddPlace( 
		/* [in] */ si As IShellItem,
		/* [in] */ fdap As FDAP) As HRESULT
	Function SetDefaultExtension( 
		/* [string][in] */ pszDefaultExtension As LPCWSTR) As HRESULT
	Function Close( 
		/* [in] */ hr As HRESULT) As HRESULT
	Function SetClientGuid( 
		/* [in] */ ByRef guid As GUID) As HRESULT
	Function ClearClientData() As HRESULT
	Function SetFilter( 
		/* [in] */ Filter As IUnknown /*IShellItemFilter*/) As HRESULT
End Interface
スポンサード リンク

この記事のカテゴリ

  • ⇒ ファイルダイアログを試してみた
  • ⇒ ファイルダイアログを試してみた
  • ⇒ ファイルダイアログを試してみた