pas文件
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
cxDataStorage, cxEdit, DB, cxDBData, cxCurrencyEdit,
cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel,
cxClasses, cxControls, cxGridCustomView, cxGrid, DBClient, StdCtrls,
Grids, DBGrids, ComCtrls, ShlObj, cxShellCommon, cxTextEdit, cxContainer,
cxMaskEdit, cxDropDownEdit, cxShellComboBox, ImgList, Menus;
type
TForm1 = class(TForm)
Button1: TButton;
ListView1: TListView;
Memo1: TMemo;
cb_Path: TcxShellComboBox;
edt_FileName: TcxTextEdit;
ImageList1: TImageList;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
procedure Button1Click(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure ProcessSearchRec(SearchRec:TSearchRec);
end;
var
Form1: TForm1;
implementation
uses DateUtils,shellApi;
{$R *.dfm}
procedure TForm1.ProcessSearchRec(SearchRec:TSearchRec);
var
ListItem:TListItem;
Icon:TIcon;
iImageIndex:Integer;
fd:TDateTime;
begin
Icon := TIcon.Create;
Icon.Handle := ExtractIcon(HInstance,pchar(cb_Path.Text+'/'+SearchRec.Name),0);
iImageIndex := ImageList1.AddIcon(Icon);
ListItem := ListView1.Items.Add;
ListItem.Caption := SearchRec.Name;
ListItem.SubItems.Add(IntToStr(SearchRec.Size));
fd := FileDateToDateTime(SearchRec.Time);
ListItem.SubItems.Add(FormatDateTime('yyyy-mm-dd',fd));
ListItem.ImageIndex := iImageIndex;
Memo1.Lines.Add(SearchRec.Name)
end;
procedure TForm1.Button1Click(Sender: TObject);
var
found:integer;
SearchRec:TSearchRec;
begin
// 可以使用FindFirst和FindNext函数查找文件,如:
ListView1.Items.Clear;
Memo1.Lines.Clear;
ImageList1.Clear;
begin
Found := FindFirst(cb_Path.Text+'/'+edt_FileName.Text, faAnyFile,SearchRec);
while Found = 0 do
begin
ProcessSearchRec(SearchRec);
Found := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
end;
procedure TForm1.N1Click(Sender: TObject);
begin
ListView1.ViewStyle := vsReport;
end;
procedure TForm1.N2Click(Sender: TObject);
begin
ListView1.ViewStyle := vsIcon;
end;
procedure TForm1.N3Click(Sender: TObject);
begin
ListView1.ViewStyle := vsSmallIcon;
end;
end.
dfm文件
object Form1: TForm1
Left = 155
Top = 211
Width = 719
Height = 491
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -10
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 247
Top = 13
Width = 61
Height = 20
Caption = 'Find'
TabOrder = 0
OnClick = Button1Click
end
object ListView1: TListView
Left = 13
Top = 52
Width = 475
Height = 170
Columns = <
item
Caption = '名称'
Width = 163
end
item
Caption = '大小'
Width = 122
end
item
Caption = '日期'
end>
LargeImages = ImageList1
PopupMenu = PopupMenu1
TabOrder = 1
ViewStyle = vsReport
end
object Memo1: TMemo
Left = 13
Top = 228
Width = 469
Height = 98
Lines.Strings = (
'Memo1')
TabOrder = 2
end
object cb_Path: TcxShellComboBox
Left = 13
Top = 13
Width = 124
Height = 21
Properties.Root.BrowseFolder = bfDrives
Properties.ShowFullPath = sfpAlways
TabOrder = 3
end
object edt_FileName: TcxTextEdit
Left = 143
Top = 13
Width = 85
Height = 21
TabOrder = 4
Text = '*.*'
end
object ImageList1: TImageList
Height = 32
Width = 32
Left = 440
Top = 144
end
object PopupMenu1: TPopupMenu
Left = 376
Top = 128
object N1: TMenuItem
Caption = '列表'
OnClick = N1Click
end
object N2: TMenuItem
Caption = '大图标'
OnClick = N2Click
end
object N3: TMenuItem
Caption = '小图标'
OnClick = N3Click
end
end
end