var
SelDirInitPath : WideString; // 初始目录,在回调函数中通过发送消息设定
function BrowseCallbackProc(hwnd: HWND;uMsg: UINT;lParam: Cardinal;lpData: Cardinal): integer; stdcall;
begin
if uMsg=BFFM_INITIALIZED then
result :=SendMessageW(Hwnd,BFFM_SETSELECTIONW,Ord(TRUE),longint(PWideChar(SelDirInitPath)))
else
result :=1
end;
function SelDir(h:THandle; const Root: WideString):WideString;
var
WindowList: Pointer;
BrowseInfo: TBrowseInfoW;
Buffer: PWideChar;
RootItemIDList, ItemIDList: PItemIDList;
ShellMalloc: IMalloc;
IDesktopFolder: IShellFolder;
Eaten, Flags: LongWord;
begin
Result := '';
FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then
begin
Buffer := ShellMalloc.Alloc(MAX_PATH);
try
RootItemIDList := nil;
if Root <> '' then begin // 设定目录选择对话框中的顶级目录,默认为桌面
SHGetDesktopFolder(IDesktopFolder);
IDesktopFolder.ParseDisplayName(Application.Handle, nil, POleStr(Root), Eaten, RootItemIDList, Flags);
end;
with BrowseInfo do begin
hwndOwner := h;
pidlRoot := RootItemIDList;
pszDisplayName := Buffer;
lpszTitle := '选择歌曲所在的目录:';
ulFlags := BIF_RETURNONLYFSDIRS;
lpfn :=@BrowseCallbackProc;
lParam :=BFFM_INITIALIZED;
end;
WindowList := DisableTaskWindows(0);
try
ItemIDList := ShBrowseForFolderW(BrowseInfo);
finally
EnableTaskWindows(WindowList);
end;
if ItemIDList <> nil then begin
ShGetPathFromIDListW(ItemIDList, Buffer);
ShellMalloc.Free(ItemIDList);
Result := Buffer;
end;
finally
ShellMalloc.Free(Buffer);
end;
end;
end;
|
int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lParam,LPARAM lpData)
{
HWND hh = GetDlgItem(hwnd, lpData);
switch(uMsg)
{
case BFFM_SELCHANGED :
char path[256];
SHGetPathFromIDList((LPITEMIDLIST)lParam,path);
::SetWindowText( GetDlgItem(hwnd, lpData), path);
return 0;
default:
return 0;
}
}
void SelectDirectory( HWND hwnd, LPCTSTR title, LONG editid)
{
BROWSEINFO bi;
char path[MAX_PATH]={"C:\\"};
bi.hwndOwner =hwnd;
bi.lpszTitle =title;
bi.pszDisplayName =path;
bi.ulFlags =BIF_RETURNONLYFSDIRS;
bi.lpfn =BrowseCallbackProc;
bi.pidlRoot =NULL;
bi.lParam =editid;
InitCommonControls();
LPITEMIDLIST a = SHBrowseForFolder(&bi);
if( a != NULL )
{
char path[MAX_PATH];
SHGetPathFromIDList( a, path);
::SetWindowText(GetDlgItem(hwnd,editid),path);
}
}
void CCopyexDlg::OnBTNSource()
{
SelectDirectory( this->m_hWnd, "Source Directory", IDC_EDIT_Source );
}
|