如题。

解决方案 »

  1.   

    TOpenDialog
    TSaveDialog不可以满足要求吗
      

  2.   

    TOpenDialog
    TSaveDialog动态的创建不就可以了吗
      

  3.   

    GetOpenFileName函数,OPENFILENAME结构体。
      

  4.   

    顶一下~~~~~~~~~~API函数吗?为什么非要用API?
      

  5.   

    推荐一本书 DELPHI WIN32API 函数大全
      

  6.   

    直接 API 在某些特殊场合是需要的,比如在 ActiveForm 里调用,
    TOpenDialog,TSaveDialog 会在任务栏上有个空白的安钮,
    而且对话框不是模式的,用户不小心点了后面的IE后,对话框
    会跑到后面去,而IE此时又不能操作,让人十分迷惑。下面的两个函数就是已经包装好的API:function OpenFileDlg(H: THandle; InitFileName: String; FileExt: String='所有文件(*.*)|*.*'; Title: String='打开文件' ): String;  function AllocFilterStr(const S: string): string;
      var
        P: PChar;
      begin
        Result := '';
        if S <> '' then
        begin
          Result := S + #0;
          P := AnsiStrScan(PChar(Result), '|');
          while P <> nil do
          begin
            P^ := #0;
            Inc(P);
            P := AnsiStrScan(P, '|');
          end;
        end;
      end;var
      ofn : TOpenFileName;
      tlt , cfn , flt : String;
    begin
      Result:='';
      FillChar(ofn, SizeOf(TOpenFileName), 0);
      if (Win32MajorVersion >= 5) and (Win32Platform = VER_PLATFORM_WIN32_NT) or // Win2k
      ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and (Win32MajorVersion >= 4) and (Win32MinorVersion >= 90)) then //WinME
        ofn.lStructSize := SizeOf(TOpenFilename)
      else
        ofn.lStructSize := SizeOf(TOpenFilename) - (SizeOf(DWORD) shl 1) - SizeOf(Pointer); //subtract size of added fields
      tlt:=Title;         SetLength(tlt,MAX_PATH+1); tlt[Length(Title)+1]:=#0;
      cfn:=InitFileName;  SetLength(cfn,MAX_PATH+1); cfn[Length(InitFileName)+1]:=#0;
      flt:=FileExt;
      ofn.hwndOwner:=H;
      ofn.hInstance:=HInstance;
      ofn.lpstrFilter:=PChar(AllocFilterStr(flt));
      ofn.lpstrFile:=PChar(cfn);
      ofn.nMaxFile:=MAX_PATH;
      ofn.lpstrTitle:=PChar(tlt);
      ofn.nMaxFileTitle:=MAX_PATH;
      ofn.Flags:=OFN_OVERWRITEPROMPT + OFN_PATHMUSTEXIST;
      if GetOpenFileName(ofn) then
        Result:=String(ofn.lpstrFile);
    end;function SaveFileDlg(H: THandle; InitFileName: String; Title: String='保存文件'): String;
    var
      ofn : TOpenFileName;
      tlt , cfn : String;
    begin
      Result:='';
      FillChar(ofn, SizeOf(TOpenFileName), 0);
      if (Win32MajorVersion >= 5) and (Win32Platform = VER_PLATFORM_WIN32_NT) or // Win2k
      ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and (Win32MajorVersion >= 4) and (Win32MinorVersion >= 90)) then // WinME
        ofn.lStructSize := SizeOf(TOpenFilename)
      else
        ofn.lStructSize := SizeOf(TOpenFilename) - (SizeOf(DWORD) shl 1) - SizeOf(Pointer); // subtract size of added fields
      tlt:=Title;         SetLength(tlt,MAX_PATH+1); tlt[Length(Title)+1]:=#0;
      cfn:=InitFileName;  SetLength(cfn,MAX_PATH+1); cfn[Length(InitFileName)+1]:=#0;
      ofn.hwndOwner:=H;
      ofn.hInstance:=HInstance;
      ofn.lpstrFile:=PChar(cfn);
      ofn.nMaxFile:=MAX_PATH;
      ofn.lpstrTitle:=PChar(tlt);
      ofn.nMaxFileTitle:=MAX_PATH;
      ofn.Flags:=OFN_OVERWRITEPROMPT + OFN_PATHMUSTEXIST;
      if GetSaveFileName(ofn) then
        Result:=String(ofn.lpstrFile);
    end;
      

  7.   

    我找到了一个更好的方法,也是使用VCL来实现的,也不会在任务栏上出现空白按钮,代码如下:
    unit OSDialogLib;interfaceuses
      Windows,SysUtils,Dialogs,Forms;function ShowOpenDialog():PChar;stdcall;
    function ShowSaveDialog():PChar;stdcall;implementation//显示打开文件对话框
    function ShowOpenDialog():PChar;stdcall;
    var
      OpenDialog: TOpenDialog;
      FileName: String;
    begin
      Result := PChar('');
      Application.Handle := GetActiveWindow();
      OpenDialog := TOpenDialog.Create(Application);
      OpenDialog.Filter := 'NOTES数据库(*.nsf)|*.nsf';
      //设置路径和文件必须存在
      OpenDialog.Options := OpenDialog.Options + [ofPathMustExist, ofFileMustExist];  if OpenDialog.Execute() then
      begin
        FileName := OpenDialog.FileName;
        if Pos('nsf',FileName) <= 0 then FileName := FileName + '.nsf';
        Result := PChar(FileName);
        OpenDialog.Free;
      end;
    end;//保存打开文件对话框
    function ShowSaveDialog():PChar;stdcall;
    var
      SaveDialog: TSaveDialog;
      FileName: String;
    begin
      Result := PChar('');
      Application.Handle := GetActiveWindow();
      SaveDialog := TSaveDialog.Create(Application);
      SaveDialog.Filter := 'NOTES数据库(*.nsf)|*.nsf';  if SaveDialog.Execute() then
      begin
        FileName := SaveDialog.FileName;
        if Pos('nsf',FileName) <= 0 then FileName := FileName + '.nsf';
        Result := PChar(FileName);
        SaveDialog.Free;
      end;
    end;end.
      

  8.   

    再次修改后的代码:
    unit OSDialogLib;interfaceuses
      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.
      

  9.   

    楼上的代码为什么要用 StrPas 函数呢? 从定义上看,它没什么用啊。