unit1.~pas

来自「精彩编程百例26~50 其中有 控制任务栏 windows底层任务控制 屏保预览」· ~PAS 代码 · 共 101 行

~PAS
101
字号
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    DriveComboBox1: TDriveComboBox;
    DirectoryListBox1: TDirectoryListBox;
    BitBtn1: TButton;
    procedure MakeTree;
    procedure BitBtn1Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  TreeCount, FilesCount, DirsCount : integer;
  //保存文件的相关信息
  TreeSize, FilesSize, DirsSize : comp;
implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.MakeTree;
//使用递规法进行文件扫描
var  Sr : TSearchRec;
     Err : integer;
     TrSize, FilePath : string;
Begin
 Err:=FindFirst('*.*',$37,Sr) ;
 While (Err = 0) do
  begin
   if Sr.Name[1]<>'.' then
   begin
     FilePath:=ExpandFileName(Sr.Name);
     TreeSize:=TreeSize+Sr.Size;
     TrSize:=FloatToStr(TreeSize);
     Form1.Caption:=DirectoryListBox1.Directory+'  '+IntToStr(TreeCount)
                   +'  files and folders    Size: '+TrSize;
     if (Sr.Attr and faDirectory)=0 then
        begin
          FilesSize:=FilesSize+Sr.Size;
          inc(FilesCount);
        end;
     inc(TreeCount);
   end;

   //开始扫描
   If ((Sr.Attr and faDirectory)<>0)AND(Sr.Name[1] <> '.') then
   begin
     DirsSize:=DirsSize+Sr.Size;
     inc(DirsCount);
     ChDir(Sr.Name) ;
     MakeTree ;
     ChDir('..') ;
   end ;
   //结束扫描
   Err:=FindNext(Sr) ;
  end ;
End;

procedure TForm1.BitBtn1Click(Sender: TObject);
//初始化计数器,对指定的文件夹进行扫描

var
s:string;

begin
 TreeCount:=1;
 FilesCount:=0;
 DirsCount:=0;
 TreeSize:=0;
 FilesSize:=0;
 DirsSize:=0;
 ChDir(DirectoryListBox1.Directory);
 MakeTree;
 Form2.showmodal;

//显示扫描结果
end;


procedure TForm1.FormResize(Sender: TObject);
//设置控件的位置
begin
    BitBtn1.Left:=round((Form1.Width-BitBtn1.Width)/2)-5;
    DriveComboBox1.Left:=round((Form1.Width-DriveComboBox1.Width)/2-5);
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?