TNB Library
TnbDirectorySelectDlg.h
[詳解]
1#pragma once
11#include <shlobj.h>
12
13
14
15//TNB Library
16namespace TNB
17{
18
19
20
32{
33public:
34
40 struct IListener
41 {
43 virtual ~IListener(void) {}
44
50 virtual void OnDirectorySelectDlgInitialized(HWND hWnd) = 0;
51
60 virtual bool OnDirectorySelectDlgSelectChanged(HWND hWnd, LPCTSTR lpszPath) const = 0;
61 };
62
64 CDirectorySelectDlg(void) : m_piListener(NULL)
65 {
66 m_path[0] = 0;
67 }
68
75 {
76 m_piListener = I;
77 }
78
84 LPCTSTR GetSelectedPath(void) const
85 {
86 return m_path;
87 }
88
133 int DoModal(LPCTSTR lpszDirectory, LPCTSTR lpszTitle, UINT uFlag = 0, HWND hWnd = NULL)
134 {
135 m_path[0] = 0;
136 LPMALLOC lpMalloc = NULL;
137 HRESULT hr = ::SHGetMalloc(&lpMalloc);
138 if ( FAILED(hr) )
139 {
140 return IDCANCEL;
141 }
142 // ブラウズ情報受け取りバッファ領域の確保
143 LPTSTR lpBuffer = static_cast<LPTSTR>(lpMalloc->Alloc(MAX_PATH));
144 if ( lpBuffer == NULL )
145 {
146 return IDCANCEL;
147 }
148 int r = IDCANCEL;
149 HRESULT hCom = ::CoInitialize(NULL);
150 // BROWSEINFO構造体の初期値設定
151 BROWSEINFO bi = { 0 };
152 bi.hwndOwner = hWnd;
153 bi.pidlRoot = NULL;
154 bi.pszDisplayName = lpBuffer;
155 bi.lpszTitle = lpszTitle;
156 bi.ulFlags = uFlag;
157 bi.lpfn = ms_BrowseCallbackProc; // コールバック関数のアドレスを設定
158 TParam param;
159 param.lpszDefaultDir = lpszDirectory;
160 param.piListener = m_piListener;
161 bi.lParam = reinterpret_cast<LPARAM>(&param);
162 // フォルダ選択ダイアログの表示
163 LPITEMIDLIST pidlBrowse = ::SHBrowseForFolder(&bi);
164 if ( pidlBrowse != NULL )
165 {
166 // PIDL形式の戻り値のファイルシステムのパスに変換
167 if ( ::SHGetPathFromIDList(pidlBrowse, lpBuffer) )
168 {
169 // 取得成功
170 _tcscpy(m_path, lpBuffer);
171 r = IDOK;
172 }
173 // SHBrowseForFolderの戻り値PIDLを解放
174 lpMalloc->Free(pidlBrowse);
175 }
176 // クリーンアップ処理
177 lpMalloc->Free(lpBuffer);
178 if ( hCom == S_OK )
179 {
180 ::CoUninitialize();
181 }
182 //
183 return r;
184 }
185
186private:
187 IListener* m_piListener;
188 TCHAR m_path[MAX_PATH];
190 struct TParam
191 {
192 LPCTSTR lpszDefaultDir;
193 IListener* piListener;
194 };
202 static int CALLBACK ms_BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
203 {
204 const TParam* P = reinterpret_cast<const TParam*>(lpData);
205 if ( uMsg == BFFM_INITIALIZED )
206 {
207 ::SendMessage(hWnd, BFFM_SETSELECTION, TRUE, reinterpret_cast<LPARAM>(P->lpszDefaultDir));
208 if ( P->piListener != NULL )
209 {
210 P->piListener->OnDirectorySelectDlgInitialized(hWnd);
211 bool r = P->piListener->OnDirectorySelectDlgSelectChanged(hWnd, P->lpszDefaultDir);
212 ::SendMessage(hWnd, BFFM_ENABLEOK, 0, r);
213 }
214 }
215 else if ( uMsg == BFFM_SELCHANGED )
216 {
217 if ( P->piListener != NULL )
218 {
219 TCHAR path[MAX_PATH];
220 bool r = false;
221 if ( ::SHGetPathFromIDList(reinterpret_cast<LPCITEMIDLIST>(lParam), path) )
222 {
223 ::SendMessage(hWnd, BFFM_SETSTATUSTEXT, 0, reinterpret_cast<LPARAM>(path));
224 r = P->piListener->OnDirectorySelectDlgSelectChanged(hWnd, path);
225 }
226 ::SendMessage(hWnd, BFFM_ENABLEOK, 0, r);
227 }
228 }
229 else if( uMsg == BFFM_VALIDATEFAILED )
230 {
231 }
232 return 0;
233 }
234};
235
236
237
238};//TNB
239
240
241
ディレクトリ選択ダイアログ
CDirectorySelectDlg(void)
コンストラクタ
LPCTSTR GetSelectedPath(void) const
[取得] 選択パス取得.
void SetListener(IListener *I)
[設定] リスナー設定.
int DoModal(LPCTSTR lpszDirectory, LPCTSTR lpszTitle, UINT uFlag=0, HWND hWnd=NULL)
[実行] 選択画面表示.
TNB Library
Definition: TnbDoxyTitle.txt:2
ディレクトリ選択ダイアログリスナー
virtual void OnDirectorySelectDlgInitialized(HWND hWnd)=0
[通知] ダイアログ初期化通知.
virtual ~IListener(void)
デストラクタ
virtual bool OnDirectorySelectDlgSelectChanged(HWND hWnd, LPCTSTR lpszPath) const =0
[通知] 選択変更通知.