by イグトランス » 2006年6月13日(火) 17:06
次はIDListからパス文字列へ変換するSHGetPathFromIDListのラッパ、IDListToPathStrと
それを使ってフォルダ選択ダイアログの結果をStringで返すBrowseForFolder_Strです。
[ここをクリックすると内容が表示されます] [ここをクリックすると非表示にします]コード: 全て選択
'-------------------------------------------------------------------------------
' IDListToPathStr, BrowseForFolder_Str
TypeDef UINT = DWord
Type SHITEMID
cb As WORD
abID[ELM(1)] As Byte
End Type
Type ITEMIDLIST
mkid As SHITEMID
End Type
TypeDef PITEMIDLIST = *ITEMIDLIST
Function GetMaxPath(pv As VoidPtr) As DWord
GetMaxPath = MAX_PATH
End Function
Function GetPathFromIDList(pv As VoidPtr, buf As *Byte, len As DWord) As DWord
If len < MAX_PATH Then
buf[0] = 0
Exit Function
End If
SHGetPathFromIDList(pidlBrowse, buf)
GetPathFromIDList = lstrlen(buf)
End Function
Function IDListToPathStr(pidl As *ITEMIDLIST) As String
IDListToPathStr = GetString(
VarPtr(dqi),
AddressOf (GetMaxPath) As GetStr_GetLenProc,
AddressOf (GetPathFromIDList) As GetStr_GetStrProc)
End Function
Function BrowseForFolder_Str(hwnd As HWND, pszTitle As BytePtr, flags As UINT) As String
Dim bi As BROWSEINFO
'BROWSEINFO構造体の初期化
'ZeroMemory(VarPtr(bi), Len(bi))
With bi
.hwndOwner = hwnd
.lpszTitle = pszTitle
.ulFlags = flags
End With
'「フォルダの参照」ダイアログボックスを表示
Dim pidlBrowse As *ITEMIDLIST
pidlBrowse = SHBrowseForFolder(bi)
If pidlBrowse Then
BrowseForFolder_Str = IDListToPathStr(pidlBrowse)
CoTaskMemFree(pidlBrowse)
End If
End Function
なんの面白みも無いですが,使用例です。
コード: 全て選択
Dim folderPath As String
folderPath = BrowseForFolder_Str(hMainWnd, "フォルダの選択", BIF_RETURNONLYFSDIRS)
MessageBox(hMainWnd, StrPtr(folderPath), "選択したフォルダのパス", MB_OK)
BrowseForFolder_Strは最初の引数がウィンドウハンドル,次にダイアログのタイトル,最後にフラグ指定の引数としました。
次はIDListからパス文字列へ変換するSHGetPathFromIDListのラッパ、IDListToPathStrと
それを使ってフォルダ選択ダイアログの結果をStringで返すBrowseForFolder_Strです。
[hide][code]'-------------------------------------------------------------------------------
' IDListToPathStr, BrowseForFolder_Str
TypeDef UINT = DWord
Type SHITEMID
cb As WORD
abID[ELM(1)] As Byte
End Type
Type ITEMIDLIST
mkid As SHITEMID
End Type
TypeDef PITEMIDLIST = *ITEMIDLIST
Function GetMaxPath(pv As VoidPtr) As DWord
GetMaxPath = MAX_PATH
End Function
Function GetPathFromIDList(pv As VoidPtr, buf As *Byte, len As DWord) As DWord
If len < MAX_PATH Then
buf[0] = 0
Exit Function
End If
SHGetPathFromIDList(pidlBrowse, buf)
GetPathFromIDList = lstrlen(buf)
End Function
Function IDListToPathStr(pidl As *ITEMIDLIST) As String
IDListToPathStr = GetString(
VarPtr(dqi),
AddressOf (GetMaxPath) As GetStr_GetLenProc,
AddressOf (GetPathFromIDList) As GetStr_GetStrProc)
End Function
Function BrowseForFolder_Str(hwnd As HWND, pszTitle As BytePtr, flags As UINT) As String
Dim bi As BROWSEINFO
'BROWSEINFO構造体の初期化
'ZeroMemory(VarPtr(bi), Len(bi))
With bi
.hwndOwner = hwnd
.lpszTitle = pszTitle
.ulFlags = flags
End With
'「フォルダの参照」ダイアログボックスを表示
Dim pidlBrowse As *ITEMIDLIST
pidlBrowse = SHBrowseForFolder(bi)
If pidlBrowse Then
BrowseForFolder_Str = IDListToPathStr(pidlBrowse)
CoTaskMemFree(pidlBrowse)
End If
End Function[/code][/hide]
なんの面白みも無いですが,使用例です。
[code]Dim folderPath As String
folderPath = BrowseForFolder_Str(hMainWnd, "フォルダの選択", BIF_RETURNONLYFSDIRS)
MessageBox(hMainWnd, StrPtr(folderPath), "選択したフォルダのパス", MB_OK)[/code]
BrowseForFolder_Strは最初の引数がウィンドウハンドル,次にダイアログのタイトル,最後にフラグ指定の引数としました。