Monday, October 30, 2017

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.

0 comments:

Post a Comment

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Search This Blog

Powered by Blogger.

Contributors

Text Widget