Get Computer Drives - Delphi Tutorial (Tip for 10 )


In this example you can see how we can get list of Computer Drives with icons and labels.

 First, check this video with details for this example .



 And of course following is the source code of this example :





(code style formatted by http://hilite.me/ )



unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellAPI, Buttons, ExtCtrls, StdCtrls;

type
  TfrmMain = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.Button1Click(Sender: TObject);
var
fDriveSize:Cardinal;
fDrives: array[0..128] of Char;
fDrive : PChar;
flag:Cardinal;
infoFile:SHFILEINFO;
S:string;
btn:TSpeedButton;
iconx:TIcon;
begin

 try
 fDriveSize:=GetLogicalDriveStrings(SizeOf(fDrives),fDrives);
 if fDriveSize=0 then Exit;
 fDrive:=fDrives;

 while fDrive^<>#0 do
 begin
   btn:=TSpeedButton.Create(Panel1);
   btn.Parent:=Panel1;
   btn.Align:=alLeft;

   flag:=SHGFI_ICON;
   flag:=flag or SHGFI_SMALLICON or SHGFI_DISPLAYNAME;
   ZeroMemory(Addr(infoFile),SizeOf(infoFile));
   SHGetFileInfo(PChar(fDrive),0,infoFile,SizeOf(infoFile),flag);
   S:=infoFile.szDisplayName;

   iconx:=Ticon.Create;
   iconx.Handle:=infoFile.hIcon;

   btn.Glyph.Assign(iconx);
   btn.Caption:=S;
   btn.Width:=120;
   Inc(fDrive,Sizeof(fDrive));
   DestroyIcon(infoFile.hIcon);
   iconx.Free;
 end;
 finally

 end;
end;

end.
Share on Google Plus

About Clear SoftX

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.