implementation
{$R *.dfm}
uses
Winapi.ShlObj;
procedure TForm3.Button1Click(Sender: TObject);
begin
OpenDialog1.Execute(Self.Handle);
end;
type
TFileDialogWrapperHack =
class(TObject)
protected
FFileDialog: TCustomFileDialog;
end;
TCustomFileDialogHack =
class(TComponent)
private
FClientGuid:
string;
FDefaultExtension:
string;
FDefaultFolder:
string;
FDialog: IFileDialog;
end;
TOpenDialogHack =
class(TCommonDialog)
private
FHistoryList: TStrings;
FOptions: TOpenOptions;
FFilter:
string;
FFilterIndex: Integer;
FCurrentFilterIndex: Integer;
FInitialDir:
string;
FTitle:
string;
FDefaultExt:
string;
FFileName: TFileName;
FFiles: TStrings;
FFileEditStyle: TFileEditStyle;
FOnSelectionChange: TNotifyEvent;
FOnFolderChange: TNotifyEvent;
FOnTypeChange: TNotifyEvent;
FOnCanClose: TCloseQueryEvent;
FOnIncludeItem: TIncludeItemEvent;
FOptionsEx: TOpenOptionsEx;
FInternalWrapper: TObject;
end;
TOpenDialogHelper =
class helper
for TOpenDialog
procedure SetDefaultFolder(
const ADefaultFolder:
string);
end;
procedure TOpenDialogHelper.SetDefaultFolder(
const ADefaultFolder:
string);
var
FileDialogWrapper: TFileDialogWrapperHack;
CustomFileDialog: TCustomFileDialog;
FileDialog: IFileDialog;
ShellItem: IShellItem;
HR: HRESULT;
begin
FileDialogWrapper := TFileDialogWrapperHack(TOpenDialogHack(Self).FInternalWrapper);
if Assigned(FileDialogWrapper)
then
begin
CustomFileDialog := FileDialogWrapper.FFileDialog;
if Assigned(CustomFileDialog)
then
begin
FileDialog := TCustomFileDialogHack(CustomFileDialog).FDialog;
if Assigned(FileDialog)
then
begin
HR := SHCreateItemFromParsingName(PWideChar(ADefaultFolder),
nil, IShellItem, ShellItem);
if Succeeded(HR)
then
FileDialog.SetFolder(ShellItem);
end;
end;
end;
end;
procedure TForm3.OpenDialog1TypeChange(Sender: TObject);
begin
if Sender
is TOpenDialog
then
begin
case TOpenDialog(Sender).FilterIndex
of
1:
begin
TOpenDialog(Sender).SetDefaultFolder('
C:\Program Files');
end;
2:
begin
TOpenDialog(Sender).SetDefaultFolder('
C:\Program Files (x86)');
end;
end;
end;
end;