TOpenDialog,TSaveDialog可以,不过有时候喜欢跑后面,看不到 unit OSDialogLib;
interface
uses Windows,SysUtils,Dialogs,Forms;
function ShowOpenFileDialog(const DialogPrompt:PChar; const DefaultFileName:PChar; const FileFilter:PChar):PChar; stdcall; function ShowSaveFileDialog(const DialogPrompt:PChar; const DefaultFileName:PChar; const FileFilter:PChar):PChar; stdcall;
implementation
//显示打开文件对话框 function ShowOpenFileDialog(const DialogPrompt:PChar; const DefaultFileName:PChar; const FileFilter:PChar):PChar; stdcall; var OpenFileDialog: TOpenDialog; FileNameA: String; FileName: String; begin Result := PChar(''); Application.Handle := GetActiveWindow(); OpenFileDialog := TOpenDialog.Create(Application); OpenFileDialog.Filter := StrPas(FileFilter); OpenFileDialog.Title := StrPas(DialogPrompt); if OpenFileDialog.Title = '' then OpenFileDialog.Title := '打开'; //设置路径和文件必须存在 OpenFileDialog.Options := OpenFileDialog.Options + [ofPathMustExist, ofFileMustExist]; FileNameA := StrPas(DefaultFileName); if FileNameA <> '' then OpenFileDialog.FileName := FileNameA;
if OpenFileDialog.Execute() then begin FileName := OpenFileDialog.FileName; if Pos('nsf',FileName) <= 0 then FileName := FileName + '.nsf'; Result := PChar(FileName); OpenFileDialog.Free; end; end;
//保存打开文件对话框 function ShowSaveFileDialog(const DialogPrompt:PChar; const DefaultFileName:PChar; const FileFilter:PChar):PChar; stdcall; var SaveFileDialog: TSaveDialog; FileNameA: String; FileName: String; begin Result := PChar(''); Application.Handle := GetActiveWindow(); SaveFileDialog := TSaveDialog.Create(Application); SaveFileDialog.Filter := StrPas(FileFilter); SaveFileDialog.Title := StrPas(DialogPrompt); if SaveFileDialog.Title = '' then SaveFileDialog.Title := '另存为'; //设置提示用户文件已经存在 SaveFileDialog.Options := SaveFileDialog.Options + [ofOverwritePrompt]; FileNameA := StrPas(DefaultFileName); if FileNameA <> '' then SaveFileDialog.FileName := FileNameA;
if SaveFileDialog.Execute() then begin FileName := SaveFileDialog.FileName; if Pos('nsf',FileName) <= 0 then FileName := FileName + '.nsf'; Result := PChar(FileName); SaveFileDialog.Free; end; end;
unit OSDialogLib;
interface
uses
Windows,SysUtils,Dialogs,Forms;
function ShowOpenFileDialog(const DialogPrompt:PChar;
const DefaultFileName:PChar;
const FileFilter:PChar):PChar; stdcall;
function ShowSaveFileDialog(const DialogPrompt:PChar;
const DefaultFileName:PChar;
const FileFilter:PChar):PChar; stdcall;
implementation
//显示打开文件对话框
function ShowOpenFileDialog(const DialogPrompt:PChar;
const DefaultFileName:PChar;
const FileFilter:PChar):PChar; stdcall;
var
OpenFileDialog: TOpenDialog;
FileNameA: String;
FileName: String;
begin
Result := PChar('');
Application.Handle := GetActiveWindow();
OpenFileDialog := TOpenDialog.Create(Application);
OpenFileDialog.Filter := StrPas(FileFilter);
OpenFileDialog.Title := StrPas(DialogPrompt);
if OpenFileDialog.Title = '' then OpenFileDialog.Title := '打开';
//设置路径和文件必须存在
OpenFileDialog.Options := OpenFileDialog.Options + [ofPathMustExist, ofFileMustExist];
FileNameA := StrPas(DefaultFileName);
if FileNameA <> '' then OpenFileDialog.FileName := FileNameA;
if OpenFileDialog.Execute() then
begin
FileName := OpenFileDialog.FileName;
if Pos('nsf',FileName) <= 0 then FileName := FileName + '.nsf';
Result := PChar(FileName);
OpenFileDialog.Free;
end;
end;
//保存打开文件对话框
function ShowSaveFileDialog(const DialogPrompt:PChar;
const DefaultFileName:PChar;
const FileFilter:PChar):PChar; stdcall;
var
SaveFileDialog: TSaveDialog;
FileNameA: String;
FileName: String;
begin
Result := PChar('');
Application.Handle := GetActiveWindow();
SaveFileDialog := TSaveDialog.Create(Application);
SaveFileDialog.Filter := StrPas(FileFilter);
SaveFileDialog.Title := StrPas(DialogPrompt);
if SaveFileDialog.Title = '' then SaveFileDialog.Title := '另存为';
//设置提示用户文件已经存在
SaveFileDialog.Options := SaveFileDialog.Options + [ofOverwritePrompt];
FileNameA := StrPas(DefaultFileName);
if FileNameA <> '' then SaveFileDialog.FileName := FileNameA;
if SaveFileDialog.Execute() then
begin
FileName := SaveFileDialog.FileName;
if Pos('nsf',FileName) <= 0 then FileName := FileName + '.nsf';
Result := PChar(FileName);
SaveFileDialog.Free;
end;
end;
end.