Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.64 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ToolWin, Winapi.ShlObj, Winapi.ActiveX,
  8.   System.IOUtils;
  9.  
  10. type
  11.  TMyStrings = array of string;
  12.   TForm1 = class(TForm)
  13.     ToolBar1: TToolBar;
  14.     SelectpathButton: TButton;
  15.  
  16.     procedure Button1Click(Sender: TObject);
  17.     procedure SelectpathButtonClick(Sender: TObject);
  18.     function  GetTextFiles(path:String):TStringList;
  19.   private
  20.     { Private declarations }
  21.   public
  22.  
  23.     { Public declarations }
  24.   end;
  25.  
  26. var
  27.  
  28.   Form1: TForm1;
  29.  
  30.  
  31. implementation
  32.  
  33. {$R *.dfm}
  34.  
  35. procedure TForm1.Button1Click(Sender: TObject);
  36. var
  37.   I: Integer;
  38. begin
  39.  
  40.  
  41. end;
  42. function TForm1.GetTextFiles(path: string):TStringList;
  43.   var
  44.     I:Integer;
  45.  
  46.     Temp_array:TArray<System.string>;
  47.  
  48.   begin
  49.  
  50.    Result:=TStringList.Create;
  51.    Temp_array:= TDirectory.GetFiles(path);
  52.    for i:=1 to Length( Temp_array) do
  53.     begin
  54.           if ExtractFileExt(Temp_array[i])='txt' then
  55.              Result.Add(Temp_array[i]);
  56.     end;
  57.   end;
  58.  
  59.  
  60.   function AdvSelectDirectory(const Caption: string;
  61.     const Root: WideString; var Directory: string; EditBox: Boolean = False;
  62.     ShowFiles: Boolean = False; AllowCreateDirs: Boolean = True): Boolean;
  63.  
  64.   function SelectDirCB(Wnd: HWND; uMsg: UINT; lParam, lpData: lParam)
  65.     : Integer; stdcall;
  66.   begin
  67.     case uMsg of
  68.       BFFM_INITIALIZED:
  69.         SendMessage(Wnd, BFFM_SETSELECTION, Ord(True), Integer(lpData));
  70.     end;
  71.     Result := 0;
  72.   end;
  73.  
  74. var
  75.   WindowList: Pointer;
  76.   BrowseInfo: TBrowseInfo;
  77.   Buffer: PChar;
  78.   RootItemIDList, ItemIDList: PItemIDList;
  79.   ShellMalloc: IMalloc;
  80.   IDesktopFolder: IShellFolder;
  81.   Eaten, Flags: LongWord;
  82. const
  83.   BIF_USENEWUI = $0040;
  84.   BIF_NOCREATEDIRS = $0200;
  85. begin
  86.   Result := False;
  87.   if not DirectoryExists(Directory) then
  88.     Directory := '';
  89.   FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
  90.   if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then
  91.   begin
  92.     Buffer := ShellMalloc.Alloc(MAX_PATH);
  93.     try
  94.       RootItemIDList := nil;
  95.       if Root <> '' then
  96.       begin
  97.         SHGetDesktopFolder(IDesktopFolder);
  98.         IDesktopFolder.ParseDisplayName(Application.Handle, nil, POleStr(Root),
  99.           Eaten, RootItemIDList, Flags);
  100.       end;
  101.       OleInitialize(nil);
  102.       with BrowseInfo do
  103.       begin
  104.         hwndOwner := Application.Handle;
  105.         pidlRoot := RootItemIDList;
  106.         pszDisplayName := Buffer;
  107.         lpszTitle := PChar(Caption);
  108.         ulFlags := BIF_RETURNONLYFSDIRS or BIF_USENEWUI or BIF_EDITBOX *
  109.           Ord(EditBox) or BIF_BROWSEINCLUDEFILES * Ord(ShowFiles) or
  110.           BIF_NOCREATEDIRS * Ord(not AllowCreateDirs);
  111.         lpfn := @SelectDirCB;
  112.         if Directory <> '' then
  113.           lParam := Integer(PChar(Directory));
  114.       end;
  115.       WindowList := DisableTaskWindows(0);
  116.       try
  117.         ItemIDList := ShBrowseForFolder(BrowseInfo);
  118.       finally
  119.         EnableTaskWindows(WindowList);
  120.       end;
  121.       Result := ItemIDList <> nil;
  122.       if Result then
  123.       begin
  124.         ShGetPathFromIDList(ItemIDList, Buffer);
  125.         ShellMalloc.Free(ItemIDList);
  126.         Directory := Buffer;
  127.       end;
  128.     finally
  129.       ShellMalloc.Free(Buffer);
  130.     end;
  131.   end;
  132. end;
  133.  
  134.  
  135.  
  136. procedure TForm1.SelectpathButtonClick(Sender: TObject);
  137.  var
  138.   path,Root:String;
  139.   fileList:TStringList;
  140. begin
  141.   fileList:=TStringList.Create;
  142.   path:='c:\\';
  143.  if AdvSelectDirectory('Выбор папки','',path)=true then
  144.   begin
  145.     fileList:= GetTextFiles(path);
  146.     fileList.Free;
  147.   end;
  148. end;
  149.  
  150. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement