Delphi 弹出Windows风格的选择文件夹对话框, 还可以新建文件夹

 
 

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, FileCtrl, Buttons, shlobj,ActiveX;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
  Path: string;//起始路径

implementation

{$R *.dfm}

function  BrowseProc(hWin:THandle; uMsg: Cardinal; lParam:LPARAM;lpData:LPARAM):LRESULT;stdcall;
begin
  if uMsg   = BFFM_INITIALIZED   then
  SendMessage(hWin,BFFM_SETSELECTION,1,lpData); //   用传过来的参数作默认路径
  Result   :=   0;
end;

function  aa: string;
var
  bi:TBrowseInfo;  //uses ShlObj
  IdList,RootItemIDList:PItemIDList;
  IDesktopFolder:IShellFolder;
  Eaten,Flags:LongWord;
begin
    result:='';
    FillChar(bi,SizeOf(bi),0);
    bi.hwndOwner:=0;
    bi.lpszTitle:='';
    bi.ulFlags:= BIF_RETURNONLYFSDIRS+64;   //加了64,显示"新建文件夹"按钮
    bi.lpfn := @BrowseProc;
    bi.lParam:=0;
    IdList :=SHBrowseForFolder(bi);
    if IdList<>nil then
    begin
        SetLength(result,255);
        SHGetPathFromIDList(IdList,PChar(result));
        result:=string(pchar(result));
        if result<>'' then
          if result[Length(result)]<>'\' then
                result:=result+'\';
    end;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
    ShowMessage(aa);
end;
//***************************第一种
//**************************第二种

function BrowseCallbackProc(hwnd: HWND;uMsg: UINT;lParam: Cardinal;lpData: Cardinal): integer; stdcall; 
begin
  if uMsg=BFFM_INITIALIZED then
    result :=SendMessage(Hwnd,BFFM_SETSELECTION,Ord(TRUE),Longint(PChar(Path)))
  else
  result :=1
end;

function SelDir(const Caption: string; const Root: WideString; out Directory: string): Boolean; 
var 
WindowList: Pointer;
BrowseInfo: TBrowseInfo;
Buffer: PChar;
RootItemIDList, ItemIDList: PItemIDList;
ShellMalloc: IMalloc;
IDesktopFolder: IShellFolder;
Eaten, Flags: LongWord;
begin 
Result := False;
Directory := '';
FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then
begin
Buffer := ShellMalloc.Alloc(MAX_PATH);
try
RootItemIDList := nil;
if Root <> '' then begin
SHGetDesktopFolder(IDesktopFolder);
IDesktopFolder.ParseDisplayName(Application.Handle, nil, POleStr(Root), Eaten, RootItemIDList, Flags);
end;
with BrowseInfo do begin
hwndOwner := Application.Handle;
pidlRoot := RootItemIDList;
pszDisplayName := Buffer;
lpszTitle := PChar(Caption);
ulFlags := BIF_RETURNONLYFSDIRS;
lpfn :=@BrowseCallbackProc;
lParam :=BFFM_INITIALIZED;
end; 
WindowList := DisableTaskWindows(0);
try
ItemIDList := ShBrowseForFolder(BrowseInfo);
finally
EnableTaskWindows(WindowList);
end;
Result := ItemIDList <> nil;
if Result then begin
ShGetPathFromIDList(ItemIDList, Buffer); 
ShellMalloc.Free(ItemIDList); 
Directory := Buffer;
end; 
finally
ShellMalloc.Free(Buffer);
end;
end;
end;

procedure TForm2.Button2Click(Sender: TObject);
var
  Path1: string;
begin
Path :=Edit1.Text;
SelDir('SelectDirectory Sample','',Path1);
Edit1.Text :=Path1
end;

end.

Delphi 弹出Windows风格的选择文件夹对话框, 还可以新建文件夹的更多相关文章

  1. (转)android底部弹出iOS7风格对话选项框(QQ对话框)--第三方开源--IOS_Dialog_Library

    本文转载于:http://blog.csdn.net/zhangphil/article/details/44940339 完成这个效果的是使用了 IOS_Dialog_Library 下载地址:ht ...

  2. 解决win10 cmd运行python弹出windows应用商店下python应用程序

    方法一: 1.我一开始下载完python后,忘记下载到哪个位置,在win10底下输入框搜索python,点击打开文件所在位置,所在位置是python快捷键的位置,直接复制进行环境配置 配置完环境变量后 ...

  3. 原生js日期时间插件鼠标点击文本框弹出日期时间表格选择日期时间

    原文出处 (这是我从互联网上搜来的,感觉能满足各方面的需求.个人感觉挺不错的,所以后期修改了一下向大家推荐!) 效果图: html代码: <!DOCTYPE html PUBLIC " ...

  4. SQL Server 2008 R2中,变表的右键弹出菜单中的“选择前1000行”为“选择所有行”

    原文:SQL Server 2008 R2中,变表的右键弹出菜单中的"选择前1000行"为"选择所有行" 从SQL Server 2008开始,微软为了提高查询 ...

  5. 自定义HttpModule,用于未登录用户,不弹出Windows认证窗口,而是跳转回SSO站点

    2012年的一篇随笔记录,可以学习到如何自定义HttpModule,而具体里面针对需求开发的代码,可能未必能让大伙了解到什么,可快速扫描而过. using System; using System.W ...

  6. 电脑预装Office2016打开Word时点击保存弹出“word无法启动转换器RECOVR32.CNV”对话框问题的修复方法

    感谢大佬:https://blog.csdn.net/qq_41969790/article/details/85161701 1.问题描述:电脑预装的Office2016,家庭和学生版正版.每次打开 ...

  7. android 弹出框(输入框和选择框)

    1.输入框: final EditText inputServer = new EditText(this); inputServer.setFilters(new InputFilter[]{new ...

  8. 关于winform窗体关闭时弹出提示框,选择否时窗体也关闭的问题

    在窗体中有FormClosing这个事件,这个事件是在窗体关闭时候运行的.如果要取消某个事件的操作,那么就在该事件中写上e.Cancel=true就能取消该事件,也就是不执行该事件.所以,你要在窗体关 ...

  9. loadrunner回放时弹出windows安全警告

    在录制 https://www.baidu.com,回放时总是弹出安全警告. 处理方案:打开IE的internet选项-->隐私,设置成“接受所有Cookie”,如下图所示即可解决

随机推荐

  1. Log4j(1.2.17) - hello world

    1. Maven 依赖 <dependencies> <dependency> <groupId>log4j</groupId> <artifac ...

  2. SQL server 如何修改登录名和密码

    No :1 启动SQL Server Management Studio,用windows登录进入: No :2 在左侧对象资源处理器中找到根节点,也就是你安装sqlserver时注册的服务器名称.然 ...

  3. eclipse-自动注释

    在eclipse中自动添加'注释'的快捷键是'Alt+Shift+J',可以在 MyEclipse中的 Java->Code Style->Code Template->Commen ...

  4. c#中使用数据读取器读取查询结果

    今天有时间了. 在看<c#数据库入门经典> ,总结数据读取器查询结果. 针对单个结果集使用读取器,有3中方法: String connString =..; String sql =@&q ...

  5. express不是内部命令解决办法

    安装nodejs后,安装express,cmd下命令:npm install express -g;之后敲入express -V会提示不是内部命令,是因为, 4.0版本中将命令工具分家出来了,所以我们 ...

  6. Nhibernate 智能提示 以及其他类库智能提示

    Nhibernate 的智能提示 Nhibernate.dll 放到以下路径 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framewo ...

  7. js 代码记录

    window.screen.availWidth 返回当前屏幕宽度(空白空间) window.screen.availHeight 返回当前屏幕高度(空白空间) window.screen.width ...

  8. 实战EntityFramework

    删除对象一定要在同一个context 我尝试这在两个方法中使用两个context(Container)实例来进行一个获得一个删除,结果我获得的”The object cannot be deleted ...

  9. 物联网操作系统 - Zephyr

    What is Zephyr? Zephyr Project is a small, scalable real-time operating system for use on resource-c ...

  10. UNIX Filesystems - Evolution Design and Implementation.pdf

    UNIX Filesystems - Evolution Design and Implementation.pdf