fan*_*cco 10
Vcl.Dialogs.TOpenDialog可用于此目的.
另请参见UsingDialogs.
procedure TForm1.Button1Click(Sender: TObject);
var
selectedFile: string;
dlg: TOpenDialog;
begin
selectedFile := '';
dlg := TOpenDialog.Create(nil);
try
dlg.InitialDir := 'C:\';
dlg.Filter := 'All files (*.*)|*.*';
if dlg.Execute(Handle) then
selectedFile := dlg.FileName;
finally
dlg.Free;
end;
if selectedFile <> '' then
<your code here to handle the selected file>
end;
Run Code Online (Sandbox Code Playgroud)
请注意,此处的示例假定将一个TButtonnamed Button1删除到表单,并将该 TForm1.Button1Click(Sender: TObject)过程分配给按钮OnClick事件.
TOpenDialog.Filter通过使用|(管道)字符将它们连接在一起,可以在属性中使用多个文件扩展名,如下所示:
'AutoCAD drawing|*.dwg|Drawing Exchange Format|*.dxf'
Run Code Online (Sandbox Code Playgroud)