DELPHI - How to use opendialog1 for choosing a folder? TOpenDialog, TFileOpenDialog
DELPHI - How to use opendialog1 for choosing a folder?
On Vista and up you can show a more modern looking dialog using TFileOpenDialog
.
var OpenDialog: TFileOpenDialog; SelectedFolder: string; ..... OpenDialog := TFileOpenDialog.Create(MainForm); try OpenDialog.Options := OpenDialog.Options + [fdoPickFolders]; if not OpenDialog.Execute then Abort; SelectedFolder := OpenDialog.FileName; finally OpenDialog.Free; end; if SelectedFolder[ Length( SelectedFolder ) ] <> '\' then SelectedFolder := SelectedFolder + '\';
uses FileCtrl; const SELDIRHELP = 1000; procedure TForm1.Button1Click(Sender: TObject); var Dir: string; begin Dir := 'C:\Windows'; if FileCtrl.SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt],SELDIRHELP) then Label1.Caption := Dir; end;
Selecting a directory with TOpenDialog
ust found the code below that seems to work fine in XP and Vista, Win7.
It provides a UI for a user to select a directory.
It uses TOpenDialog, but sends it a few messages to clean up the appearance for the purposes of selecting a directory.
After suffering from the limited capabilities provided by Windows itself,
it's a pleasure to be able to give my users a familiar UI where they can browse and select a folder comfortably.
I'd been looking for something like this for a long time so thought I'd post it here so others can benefit from it.
Here's what it looks like in Win 7:
function GimmeDir(var Dir: string): boolean; var OpenDialog: TOpenDialog; OpenDir: TOpenDir; begin //The standard dialog... OpenDialog:= TOpenDialog.Create(nil); //Objetc that holds the OnShow code to hide controls OpenDir:= TOpenDir.create; try //Conect both components... OpenDir.Dialog:= OpenDialog; OpenDialog.OnShow:= OpenDir.HideControls; //Configure it so only folders are shown (and file without extension!)... OpenDialog.FileName:= '*.'; OpenDialog.Filter:= '*.'; OpenDialog.Title:= 'Chose a folder'; //No need to check file existis! OpenDialog.Options:= OpenDialog.Options + [ofNoValidate]; //Initial folder... OpenDialog.InitialDir:= Dir; //Ask user... if OpenDialog.Execute then begin Dir:= ExtractFilePath(OpenDialog.FileName); result:= true; end else begin result:= false; end; finally //Clean up... OpenDir.Free; OpenDialog.Free; end; end;
分类:
DELPHI
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
2013-09-08 TPS5410/TPS5430 开关电源稳压器(DC-DC)
2013-09-08 TF卡翻盖式卡座
2012-09-08 git 创建 .gitignore 文件
2012-09-08 git commit 时,会打开默认的文本编辑器,要求你输入提交信息
2012-09-08 git 初始配置
2012-09-08 Git忽略文件
2012-09-08 git 创建 .gitignore 文件 建立项目过滤规则