2013-05-08 72 views
1

文件夾我想從港口VCL用Delphi XE2選擇使用Firemonkey框架

with TFileOpenDialog.Create(nil) do 
    try 
    Title := 'Select Directory'; 
    Options := [fdoPickFolders, fdoPathMustExist, fdoForceFileSystem]; 
    OkButtonLabel := 'Select'; 
    DefaultFolder := FDir; 
    FileName := FDir; 
    if Execute then 
     ShowMessage(FileName); 
    finally 
    Free; 
end 

我找不到幫助進口用FM框架有關單位以下短代碼片段FM。至少filecrtl單元似乎不再可用 此外,解決方案relevant question也沒有在我的測試應用程序中出現一個窗口。

+2

哪個操作系統? – 2013-05-08 12:13:13

+0

WIN x64&WIN X32;只要DELPHI支持LINUX,我將把我所有的東西都改成這個操作系統 – Franz 2013-05-10 16:51:25

回答

0

由於您使用的是Windows,因此即使在FireMonkey應用程序中也可以使用問題中的代碼。我認爲你需要把代碼放入一個不使用任何FMX單元的單元中,但是否則它工作正常。你需要在該單元中使用Vcl.Dialogs。

1

這適用於在XE4中使用新的FireMonkey HD應用程序(在Win7 64上運行,既作爲Win32也作爲Win64目標)。

它當然是特定於Windows。您可以使用操作系統版本細節(WinVistaSelectFolderWinXPSelectFolder),或者只需調用通用的SelectFolder即可。

unit WinFolderSelectUtils; 

interface 

uses 
    SysUtils; 

function SelectFolder: string;  
function WinVistaSelectFolder: string; 
function WinXPSelectFolder: string; 

implementation 

uses 
    ShellAPI, ShlObj, ActiveX, Windows; 

function SelectFolder: string; 
begin 
    if TOSVersion.Check(6) then 
    Result := WinVistaSelectFolder 
    else 
    Result := WinXPSelectFolder; 
end; 

function WinXPSelectFolder: string; 
var 
    BrowseInfo: TBrowseInfo; 
    ItemIDList: PItemIDList; 
    ItemSelected: PItemIDList; 
    NameBuffer: array[0..MAX_PATH] of Char; 
begin 
    Result := ''; 

    // Should be doing some error handling here. Omitted for clarity, but 
    // obviously should raise some sort of exception if anything fails instead 
    // of just returning an empty string. 
    ItemIDList := nil; 
    FillChar(BrowseInfo, SizeOf(BrowseInfo), 0); 
    BrowseInfo.hwndOwner := 0; 
    BrowseInfo.pidlRoot := ItemIDList; 
    BrowseInfo.pszDisplayName := NameBuffer; 
    BrowseInfo.lpszTitle := 'Select a directory'; 
    BrowseInfo.ulFlags := BIF_RETURNONLYFSDIRS; 
    ItemSelected := SHBrowseForFolder(BrowseInfo); 
    if ItemSelected <> nil then 
    begin 
    SHGetPathFromIDList(ItemSelected, NameBuffer); 
    Result := NameBuffer; 
    end; 
    CoTaskMemFree(BrowseInfo.pidlRoot); 
end; 

function WinVistaSelectFolder: String; 
var 
    FileDialog: IFileDialog; 
    hr: HRESULT; 
    IResult: IShellItem; 
    FileName: PWideChar; 
    Settings: Cardinal; 
    Title: String; 
begin 
    hr := CoCreateInstance(CLSID_FileOpenDialog, 
         nil, 
         CLSCTX_INPROC_SERVER, 
         IFileDialog, 
         FileDialog); 
    if hr = S_OK then 
    begin 
    FileDialog.GetOptions(Settings); 
    Settings := Settings or FOS_PICKFOLDERS or FOS_FORCEFILESYSTEM; 
    FileDialog.SetOptions(Settings); 
    FileDialog.SetOkButtonLabel('Select'); 
    Title := 'Select a directory'; 
    FileDialog.SetTitle(PWideChar(Title)); 
    hr := FileDialog.Show(0); 
    if hr = S_OK then 
    begin 
     hr := FileDialog.GetResult(IResult); 
     if hr = S_OK then 
     begin 
     IResult.GetDisplayName(SIGDN_FILESYSPATH, FileName); 
     Result := FileName; 
     end; 
    end; 
    end; 
end; 

結束。