档案总管右半边Delphi TListView

档案总管右半边Delphi TListView

延续上一篇 想要写一个”档案总管”的想法,今天先来完成”右半边”。
TListView元件就具备”右半边”的这些功能:显示模式(大小图示、清单、详细资料)、点击标头可排序、Double click可执行或开启(依系统设定之联结)。

完成画面:http://img2.58codes.com/2024/20111373gTtl87FbNS.jpg
环境: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


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章