档案总管右半边Delphi TListView
延续上一篇 想要写一个”档案总管”的想法,今天先来完成”右半边”。
TListView元件就具备”右半边”的这些功能:显示模式(大小图示、清单、详细资料)、点击标头可排序、Double click可执行或开启(依系统设定之联结)。
完成画面:
环境:Delphi RAD 10.4 Win10 64x
Source+Exe 下载
注:本程式码,主要参考N多年前的一本Delphi 5 的书。
使用TSearchRec搜寻档案:
//find the first one flag := FindFirst(sPath, faAnyFile,sRec); //if found if flag=0 then begin // add into ListView newItem := ListView1.Items.Add; newItem.Caption := sRec.Name; // file name newItem.SubItems.Add(intToStr(sRec.Size)); // size newItem.SubItems.Add(DateTimeToStr(FileDateToDateTime(sRec.Time))); // time // is directory if sRec.Attr = faDirectory then newItem.ImageIndex := 1; // find next while (FindNext(sRec)=0) do begin // add into ListView newItem := ListView1.Items.Add; newItem.Caption := sRec.Name; // file name newItem.SubItems.Add(intToStr(sRec.Size)); // size newItem.SubItems.Add(DateTimeToStr(FileDateToDateTime(sRec.Time))); // time // is directory if sRec.Attr = faDirectory then newItem.ImageIndex := 1; // Sort ListView1.AlphaSort; end;
FindFirst 找到返回0
SearchRec.Attr faDirectory= 16 faAnyFile=71 faArchive=32
SearchRec 属性 .Name .Size .Time .TimeStamp .Attr 可取用
元件初始设定:
//--- 设定各元件初始值procedure TForm1.componentSetUp;begin with ListView1 do begin ColumnClick := True; Columns[0].Caption := 'NAME'; Columns[1].Caption := 'SIZE'; Columns[2].Caption := 'DATE'; SortType := stBoth; ViewStyle := vsReport; PopupMenu := PopupMenu1; largeImages := ImageList1; smallImages := ImageList2; end; with cmbFilter do begin FileList := FileListBox1; Filter := 'All files (*.*)|*.*|Text files (*.txt)|*.txt'; Filter := Filter+ '|BMP (*.bmp)|*.bmp|JPG (*.jpg)|*.jpg'; end;end;
ListView 功能:
ListView.ColumnClick 内容重新排序
procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);begin ListView1.SortType := stNone; if Column.Index <> SortedColumn then begin SortedColumn := Column.Index; Descending := False; end else Descending := not Descending; ListView1.SortType := stData;end;
SortType
If SortType is not stNone, the list items in the Items property are automatically sorted.
ListView1DblClick 执行 ShellExec API
procedure TForm1.ListView1DblClick(Sender: TObject);var fTmp,fullPath, fDir,YesNo : string; flag : Boolean;begin fDir := DirectoryListBox1.Directory; fTmp := ListView1.items[ListView1.ItemIndex].Caption; fullPath := fDir+'\'+fTmp; doExec(fullPath);end;
按右键 popupmenu 选择不同显示方式
ListView1.ViewStyle vsIcon vsSmallIcon vsList vsReport