已经有一个关于TSaveDialog和[ofOverwritePromt]的简单问题在Delphi overwrite existing file on save dialog上。
所以我的问题/场景如下:
- 我有一个
TSaveDialog
- 我在
Options
中设置了[ofOverwritePromt]
- 我将过滤器设置为“PDF(*.pdf)|* .pdf”
- 过滤器索引设置为1
现在我执行程序并调用对话框。如果我使用鼠标或键盘(不输入)选择的文件已存在,则保存对话框会询问是否覆盖,并显示以下消息:
但是,如果我执行相同的操作,但是像“Test”这样键入文件名而没有指定扩展名,则保存对话框不会确认覆盖。 我知道它实际上返回另一个文件名“C:\ Users \ xxx \ Desktop \ Test”,而不是“C:\ Users \ xxx \ Desktop \ Test.pdf”。 如果对话框要求您保存文件,但需要键入扩展名,这种情况有点不好。 所以通常我会这样处理:
repeat
{ Ask for the file if not silent }
if not dlgSave.Execute then
Exit;
{ Read the filename from the save dialog }
LTempFile := dlgSave.FileName;
if not SameText(ExtractFileExt(LTempFile), '.pdf') then
begin
{ Add the extension }
LTempFile := LTempFile + '.pdf';
{ As we bypassed the overwrite check in dialog do it now }
if FileExists(LTempFile) then
if MsgWarn(Format('%s already exists. Replace?', [ExtractFileName(LTempFile)]), mbOKCancel) <> mrOk then
Continue;
end;
Break;
until False;
有没有更加优雅的方式来做到这一点,而不需要自定义标准对话框?