TShellTreeView: All Drives inside the tree lazarus / 31-LCL ---------------------------------------------------------------------- RAW · 2016-10-27 06:25 Who knows how to get all the drives on a computer into the TShellTreeView? Something like RootFolder: rfDrives or Computer... Is there an easy way that I don't see ... ? I needed a special UI for some old computer-newbies for some programs and so I decided to do my own "Choose Folder"-Dialog without "Bling Bling"... Don't know how this is called... maybe epic minimalism...  EDIT: This is easier... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Unit uSelectFolder; {$MODE OBJFPC}{$H+} Interface USES  Classes,  SysUtils, Forms,   Controls,  StdCtrls, Graphics, LCLType, ShellCtrls;  TYPE  SelectFolder = Class    Class Procedure OnKeyDownDIR (Sender: TObject; Var Key: Word;                                 Shift: TShiftState);  PRIVATE     Class Var strResult: String;     Class Var stvDIR   : TShellTreeView;     PUBLIC     Class Function Show: String;  End; Implementation  Class Function SelectFolder.Show: String;  Var   wndDIR: TForm; Begin  Result:= '';   wndDIR:= TForm.Create(Nil);   Try    wndDIR.BorderStyle          := bsSizeable;    wndDIR.BorderIcons          := [biSystemMenu, biMaximize];    wndDIR.Caption              := Application.Title;    wndDIR.Constraints.MinHeight:= 300;    wndDIR.Constraints.MinWidth := 400;    wndDIR.Height               := 500;    wndDIR.Width              := 700;    wndDIR.AutoScroll           := False;    wndDIR.Position             := poDesktopCenter;    wndDIR.FormStyle          := fsNormal;    wndDIR.KeyPreview           := True;    wndDIR.DoubleBuffered       := True;     wndDIR.OnKeyDown           := @OnKeyDownDIR;     stvDIR                 := TShellTreeView.Create(wndDIR);    stvDIR.DoubleBuffered  := True;    stvDIR.BorderStyle     := bsSingle;    stvDIR.Font.Size       := 20;  stvDIR.Font.Quality    := fqAntialiased;    stvDIR.Font.Style      := [fsBold];    stvDIR.Align           := alClient;    stvDIR.AutoExpand    := True;    stvDIR.BackgroundColor := clBtnFace;    stvDIR.Root          := ExtractFileDrive(ParamStr(0));    stvDIR.Root            := '';    stvDIR.ObjectTypes     := [otFolders];    stvDIR.ReadOnly      := True;    stvDIR.ShowButtons     := True;    stvDIR.ShowRoot        := True;    stvDIR.HideSelection   := True;    stvDIR.ScrollBars      := ssAutoBoth;    stvDIR.TreeLinePenStyle:= psSolid;    stvDIR.TreeLineColor   := clLime;    stvDIR.ExpandSignColor := clLime;    stvDIR.Parent          := wndDIR;     wndDIR.ShowModal;   Result:= strResult;   Finally    wndDIR.Release;    wndDIR:= Nil;   End; End;  Class Procedure SelectFolder.OnKeyDownDIR(Sender: TObject; Var Key: Word;                                          Shift: TShiftState); Begin  If Key = VK_RETURN  Then   Begin    strResult:= stvDIR.Path;    TForm(Sender).Close;   End;   If Key = VK_ESCAPE  Then TForm(Sender).Close; End; End. // ==================// Can be used like this: Implementation USES uSelectFolder; {$R *.LFM} Procedure TForm1.Button1Click(Sender: TObject); Begin  Edit1.Text:= SelectFolder.Show; End;   It's very easy to use: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i USES uSelectFolder; orImplementation USES uSelectFolder; Procedure TForm1.Button1Click(Sender: TObject); Begin  Edit1.Text:= SelectFolder.Show; End; Procedure TForm1.FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState); Begin  If Key = VK_F5 Then Edit1.Text:= SelectFolder.Show; End;   In the moment I don't need any network drives, so this workaround should do the job... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Unit uSelectFolder; {$MODE OBJFPC}{$H+} Interface USES  Classes,  SysUtils, Forms,   Controls,  StdCtrls, LCLType,  Buttons, Graphics, ShellCtrls;  TYPE  SelectFolder = Class    Class Procedure wndDIRShow    (Sender: TObject);   Class Procedure wndDIRResize    (Sender: TObject);   Class Procedure wndDIRKeyDown   (Sender: TObject;                                  Var Key: Word; Shift: TShiftState);    Class Procedure wndDIRWheelDown (Sender: TObject; Shift: TShiftState;                                  MousePos: TPoint; Var Handled: Boolean);   Class Procedure wndDIRWheelUp   (Sender: TObject; Shift: TShiftState;                                    MousePos: TPoint; Var Handled: Boolean);    Class Procedure btnOKClick      (Sender: TObject);   Class Procedure btnDriveClick   (Sender: TObject);    Class Procedure DrivesScrollLeft;   Class Procedure DrivesScrollRight;     PRIVATE     Class Var wndDIR          : TForm;     Class Var slvList         : TShellListView;     Class Var stvDIR        : TShellTreeView;     Class Var btnOK           : TSpeedButton;     Class Var btnSelDrive     : TButton;     Class Var lBtnDrives      : TList;     Class Var strResult       : String;     Class Var iLengthBtnDrives: Integer;     PUBLIC     Class Function Show: String;  End; Implementation  Class Function SelectFolder.Show: String;  Var   iWidth, iHeight: Integer;   strDrive, strAZ: String;   btnDrive       : TButton;   I, iSpace      : Integer;   slDrives     : TStringlist; Begin  Result:= '';   If Screen.Width < 1024  Then iWidth:= Screen.WorkAreaWidth  Else iWidth:= 1024;   If Screen.Height < 768  Then iHeight:= Screen.WorkAreaHeight  Else iHeight:= 768;   wndDIR:= TForm.Create(Nil);   Try    wndDIR.DoubleBuffered    := True;    wndDIR.KeyPreview        := True;    wndDIR.Position          := poDesktopCenter;    wndDIR.Width             := iWidth;    wndDIR.Height            := iHeight;    wndDIR.BorderStyle       := bsSizeable;    wndDIR.BorderIcons       := [biMaximize, biSystemMenu];    wndDIR.FormStyle         := fsStayOnTop;      wndDIR.OnKeyDown       := @wndDIRKeyDown;      wndDIR.OnShow          := @wndDIRShow;      wndDIR.OnResize        := @wndDIRResize;      wndDIR.OnMouseWheelDown:= @wndDIRWheelDown;      wndDIR.OnMouseWheelUp  := @wndDIRWheelUp;     btnOK             := TSpeedButton.Create(wndDIR);    btnOK.Align       := alBottom;    btnOK.Height      := 50;    btnOK.Font.Size   := 20;    btnOK.Font.Quality:= fqAntialiased;    btnOK.Font.Style  := [fsBold];    btnOK.Parent      := wndDIR;    btnOK.Caption     := 'OK';      btnOK.OnClick   := @btnOKClick;     strAZ   := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';    slDrives:= TStringlist.Create;     Try      For I:= 1 To Length(strAZ)      Do       Begin        If DirectoryExists(strAZ[I]+':')        Then slDrives.Add (strAZ[I]+':');       End;       If Not slDrives.Count > 0      Then strDrive:= ''      Else       Begin        iSpace:= -100;        lBtnDrives         := TList.Create;        lBtnDrives.Capacity:= slDrives.Count;         For I:= 0 To slDrives.Count-1        Do       Begin          iSpace:= iSpace +100;           btnDrive                 := TButton.Create(wndDIR);          btnDrive.Parent            := wndDIR;          btnDrive.Caption           := slDrives[I];          btnDrive.Font.Size         := 20;          btnDrive.Font.Quality      := fqAntialiased;          btnDrive.Name              := 'btnDrive'+InTtoStr(I);          btnDrive.SetBounds            (iSpace, 0, 100, 50);            btnDrive.OnClick         := @btnDriveClick;            btnDrive.OnMouseWheelDown:= @wndDIRWheelDown;            btnDrive.OnMouseWheelUp  := @wndDIRWheelUp;           lBtnDrives.Add(btnDrive);         End;       strDrive:= slDrives[0]+'\';        TButton(lBtnDrives[0]).Font.Style:= [fsItalic, fsUnderline, fsBold];         btnSelDrive:= TButton(lBtnDrives[0]);        iLengthBtnDrives:= 100 *lBtnDrives.Count;       End;     Finally      slDrives.Free;     End;     slvList       := TShellListView.Create(wndDIR);    slvList.Parent:= wndDIR;    slvList.Root  := strDrive;    slvList.Hide;     stvDIR                := TShellTreeView.Create(wndDIR);    stvDIR.AutoExpand     := True;    stvDIR.DoubleBuffered := True;    stvDIR.Parent         := wndDIR;    stvDIR.Font.Color     := clBlack;    stvDIR.BackgroundColor:= clBtnFace;    stvDIR.Font.Size      := 15;    stvDIR.Font.Quality   := fqAntialiased;    stvDIR.Root           := strDrive;    stvDIR.ShellListView  := slvList;    stvDIR.ScrollBars     := ssAutoBoth;    stvDIR.SetBounds         (0, 50, wndDIR.ClientWidth, (wndDIR.ClientHeight-100));    stvDIR.Hint           := '"T": Tree'+sLineBreak+'"D": Drives';    stvDIR.ShowHint       := True;     If iLengthBtnDrives > wndDIR.ClientWidth    Then     Begin      wndDIR.Caption:= 'CHOOSE A FOLDER     Ctrl+ArrowKeys: Scroll Drives';      stvDIR.Hint   := '"T": Tree'+sLineBreak+'"D": Drives'+sLineBreak+'MouseWheel Up/Down: Scroll Drives';     End    Else     Begin      wndDIR.Caption:= 'CHOOSE A FOLDER';      stvDIR.Hint   := '"T": Tree'+sLineBreak+'"D": Drives';     End;     wndDIR.ShowModal;    Result:= strResult;   Finally    lBtnDrives.Free;    wndDIR.Release;    wndDIR:= Nil;   End; End;  Class Procedure SelectFolder.wndDIRShow(Sender: TObject); Begin  stvDIR.SetFocus;  stvDIR.Select(stvDIR.Items[0]); End;  Class Procedure SelectFolder.wndDIRResize(Sender: TObject);  Var   I, iSpace: Integer; Begin  If iLengthBtnDrives > wndDIR.ClientWidth  Then   Begin    wndDIR.Caption:= 'CHOOSE A FOLDER     Ctrl+ArrowKeys: Scroll Drives';    stvDIR.Hint   := '"T": Tree'+sLineBreak+'"D": Drives'+sLineBreak+'MouseWheel Up/Down: Scroll Drives';   End  Else   Begin    wndDIR.Caption:= 'CHOOSE A FOLDER';    stvDIR.Hint   := '"T": Tree'+sLineBreak+'"D": Drives';   End;   iSpace:= -100;   For I:= 0 To lBtnDrives.Count-1  Do   Begin    iSpace:= iSpace +100;    TButton(lBtnDrives[I]).Left:= iSpace;   End;   stvDIR.SetBounds(0, 50, wndDIR.ClientWidth, (wndDIR.ClientHeight-100)); End;  Class Procedure SelectFolder.btnOKClick(Sender: TObject); Begin  strResult:= stvDIR.Path;  wndDIR.Close; End;  Class Procedure SelectFolder.wndDIRKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);  Var   I: Integer; Begin  If Key = VK_ESCAPE Then wndDIR.Close;  If Key = Ord('T')  Then stvDIR.SetFocus;   If Key = Ord('D')  Then   Begin    For I:= 0 To lBtnDrives.Count-1    Do     Begin      If TButton(lBtnDrives[I]).Left = 0      Then       Begin      TButton(lBtnDrives[I]).SetFocus;        Break;       End;     End;   End;   If Key = VK_TAB  Then   If stvDIR.Focused   Then    Begin     For I:= 0 To lBtnDrives.Count-1     Do      Begin       If TButton(lBtnDrives[I]).Left = 0       Then        Begin         TButton(lBtnDrives[I]).SetFocus;         Break;        End;      End;    End;   If Key = VK_RETURN  Then   If stvDIR.Focused   Then    Begin     strResult:= stvDIR.Path;     wndDIR.Close;    End;   If (Key = VK_RIGHT) And (ssCtrl In Shift)  Then DrivesScrollRight;   If (Key = VK_LEFT) And (ssCtrl In Shift)  Then DrivesScrollLeft; End;  Class Procedure SelectFolder.btnDriveClick(Sender: TObject); Begin  If Sender Is TButton  Then   Begin    If Assigned(btnSelDrive)    Then btnSelDrive.Font.Style:= [];     TButton(Sender).Font.Style:= [fsItalic, fsUnderline, fsBold];    btnSelDrive:= TButton(Sender);   stvDIR.Root:= TButton(Sender).Caption+'\';    stvDIR.SetFocus;    stvDIR.Select(stvDIR.Items[0]);   End; End;  Class Procedure SelectFolder.DrivesScrollLeft;  Var   I: Integer; Begin  If (TButton(lBtnDrives[0]).Left < 0)  Then   Begin    TButton(lBtnDrives[0]).SetFocus;     For I:= 0 To lBtnDrives.Count-1  Do TButton(lBtnDrives[I]).Left:= TButton(lBtnDrives[I]).Left +100;   End; End;  Class Procedure SelectFolder.DrivesScrollRight;  Var   I: Integer; Begin  If (TButton(lBtnDrives[lBtnDrives.Count-1]).Left > wndDIR.ClientWidth -100)  Then   Begin    TButton(lBtnDrives[0]).SetFocus;     For I:= 0 To lBtnDrives.Count-1  Do TButton(lBtnDrives[I]).Left:= TButton(lBtnDrives[I]).Left -100;   End; End;  Class Procedure SelectFolder.wndDIRWheelDown(Sender: TObject; Shift: TShiftState; MousePos: TPoint; Var Handled: Boolean); Begin  DrivesScrollRight; End;  Class Procedure SelectFolder.wndDIRWheelUp(Sender: TObject; Shift: TShiftState; MousePos: TPoint; Var Handled: Boolean); Begin  DrivesScrollLeft; End;  End. ---------------------------------------------------------------------- totya · 2016-10-27 07:12 Quote from: SoE on October 27, 2016, 08:25:40 amWho knows how to get all the drives on a computer into the TShellTreeView? Hi! I see all drive on my system with this component with default settings. Win x64. ---------------------------------------------------------------------- RAW · 2016-10-27 08:20 Hi, thanks... yes me too... but only at DesignTime and there is no ObjectInspector-"Root"-Entry... At first I thought I only need "GetBasePath"... but then there is no tree...nothing in it... Right now I don't know how to use it in a unit without a DesignTime- Form. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ShellTreeView1.Root:= ShellTreeView1.GetBasePath;  // shows nothing... ???  I thought a TShellListView Comp is also necessary, but it works without it very fine... ---------------------------------------------------------------------- totya · 2016-10-27 09:53 I'm sorry, but I don't know what are you talking about... if I drop a simple TShellTreeView component to the form, I see all drives at my system at runtime. See attached example. Please try it! TShellTreeView component has Root property, this works both design and runtime for me. If you need a special path, see for example: http://forum.lazarus.free pascal.org/index.php/topic,33739.msg218994.html If Root dir is empty, you see all drives in your system. ---------------------------------------------------------------------- RAW · 2016-10-27 10:16 Quoteif I drop a simple TShellTreeView component to the form, I see all drives at my system at runtime. Me too... that's working fine... But this is not working: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Unit Unit1; {$mode objfpc}{$H+} Interface USES  Classes, SysUtils, Forms, Controls, Graphics, ShellCtrls;  TYPE  TForm1 = Class(TForm)    Procedure FormCreate (Sender: TObject);      PRIVATE      stvDIR: TShellTreeView;  End;  VAR  Form1: TForm1; Implementation {$R *.lfm} Procedure TForm1.FormCreate(Sender: TObject); Begin  stvDIR              := TShellTreeView.Create(Self);  stvDIR.AutoExpand     := True;  stvDIR.DoubleBuffered := True;  stvDIR.Parent         := Self;  stvDIR.BackgroundColor:= clBtnFace;  stvDIR.Font.Size      := 15;  stvDIR.Font.Quality   := fqAntialiased;  stvDIR.Root           := '';  stvDIR.Align          := alClient;  stvDIR.ObjectTypes    := [otFolders];  stvDIR.ReadOnly       := True;  stvDIR.ShowButtons    := True;  stvDIR.ShowRoot       := True;  stvDIR.HideSelection  := True;  stvDIR.ShowHint       := True; End; End.       Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i stvDIR.Root:= ''; // not working, no treestvDIR.Root:= stvDIR.GetBasePath; // not working, no treestvDIR.Root:= 'C:\'; // works fine, but I only see DIR C:\ of coursestvDIR.Root:= '/'; // works fine, but I only see DIR I:\ stvDIR.Root:= '\'; // works fine, but I only see DIR I:\ ... the same result... ---------------------------------------------------------------------- totya · 2016-10-27 10:59 Hi! If you can't tell what is your problem exactly, nobody can't help for you. Now I see what is your problem. if you create TShellTreeView component at runtime, you don't see the drives... It seems to me it's a small bug, simple workaround when you create the component at runtime: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);begin  stvDIR:= nil;  stvDIR:= TShellTreeView.Create(Self);   if Assigned(stvDIR) then    with stvDIR do      begin        Parent:= Panel1; // Parent component...        Align:= alClient;        Visible:= true;         // Workaround...      Root:= ExtractFileDrive(ParamStr(0));        Root:= '';      end;end; And then everything is works. ---------------------------------------------------------------------- Bart · 2016-10-27 14:15 I fixed that in trunk already. You must call PopulateWithbaseFiles() after you created it at runtime (in trunk this method is public). Bart ---------------------------------------------------------------------- RAW · 2016-10-27 20:19 QuoteRoot:= ExtractFileDrive(ParamStr(0)); Root:= ''; Thank you very much... that is working nice. QuoteI fixed that in trunk already. You must call PopulateWithbaseFiles() after you created it at runtime (in trunk this method is public). Good to know... thank you very much.