Вечер добрый дамы и господа :]
Собственно, впервые в жизни столкнулся с использованием WinApi, посидел почитал немного о функциях, решил для примера разобрать листинг готового "приложения для просмотра файлов и папок в указанной пользователем директории".
Собственно, код компилируется без проблем, но само запущенное приложение не получает никаких данных ни о дисках, ни о файлах.
Как я понял, проблема заключается в переменной "SearchRec: TSearchRec;", но не могу понять что именно не так. Был бы очень благодарен за разъяснение данной ситуации
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
ListBox1: TListBox;
ComboBox1: TComboBox;
procedure ListBox1DblClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Dir : String;
implementation
{$R *.dfm}
function DelLastDir(str : String):String;
var i: byte;
str1 : string;
begin
str1:=str;
Delete(str1,Length(str1),1);
i:=Length(str1);
while Copy(str1,i,1)<>'\' do
begin Delete(str1,i,1); i:=i-1; end;
Result:=str1;
end;
function GetDrive(Drive: String): String;
var
DriveType : uInt;
begin
DriveType := GetDriveType(PChar(Drive));
case DriveType of
0: Result := '?';
1: Result := 'Path does not exists';
Drive_Removable: Result := 'Removable';
Drive_Fixed: Result := 'Fixed';
Drive_Remote: Result := 'Remote';
Drive_CDROM: Result := 'CD-ROM';
Drive_RamDisk: Result := 'RAMDisk'
else Result := 'Unknown';
end;
end;
procedure ShowFiles;
var
SearchRec: TSearchRec;
begin
Form1.ListBox1.Clear;
if FindFirst(Dir + '*.*', faAnyFile, SearchRec) = 0 then
repeat
if (SearchRec.Attr and faDirectory) <> 0 then begin
if (SearchRec.name <> '.') and (SearchRec.name <> '..') then
Form1.ListBox1.Items.Insert(0,SearchRec.name)
end
else
Form1.ListBox1.Items.Add(SearchRec.name);
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
Dir:=ComboBox1.Items[ComboBox1.ItemIndex];
ShowFiles;
Label3.Caption:=Dir;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
SearchRec: TSearchRec;
str : Char;
i : integer;
begin
ComboBox1.Clear;
str := 'A';
for i := 1 to 10 do
begin
if GetDrive(str+':')<>'Unknown' then ComboBox1.Items.Add(str+':\');
str := chr(ord(str)+1);
end;
ComboBox1.ItemIndex:=2;
Dir:='C:\';
ShowFiles;
Label2.Caption:='';
Label3.Caption:='C:\';
end;
procedure TForm1.ListBox1Click(Sender: TObject);
var
FileAttributeData: TWin32FileAttributeData;
FileApiSize : Cardinal;
FileName : string;
begin
FileName:=Label3.Caption + ListBox1.Items[ListBox1.ItemIndex];
if FileExists(FileName) then
begin
GetFileAttributesEx(PChar(FileName), GetFileExInfoStandard, @FileAttributeData);
FileApiSize := FileAttributeData.nFileSizeLow;
Label2.Caption:='Размер: ' + IntToStr(FileApiSize) + ' Кбайт';
end
else Label2.Caption:='';
end;
procedure TForm1.ListBox1DblClick(Sender: TObject);
var
SearchRec: TSearchRec;
begin
if ListBox1.ItemIndex>0 then
begin
Dir:=Label3.Caption + ListBox1.Items[ListBox1.ItemIndex]+'\';
ShowFiles;
ListBox1.Items.Insert(0,'<-...');
Label3.Caption:=Dir;
end
else //åñëè íàæàòî íà '<-...'
if Length(Label3.Caption)>3 then
begin
Dir:=DelLastDir(Label3.Caption);
ShowFiles;
ListBox1.Items.Insert(0,'<-...');
Label3.Caption:=Dir;
end;
end;
end.