档案总管右半边Delphi TListView

档案总管右半边Delphi TListView

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

完成画面:https://ithelp.ithome.com.tw/upload/images/20210822/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


<<:  Ruby幼幼班--Rotate String

>>:  以Postgresql为主,再聊聊资料库 PostgreSQL复制schema下table结构到另一schema的procedure及取样的研讨

012-忘记打文章

连续发文快两个礼拜了,今天很悠闲了看了场电影、煮饭、洗衣服、健身还有整理家里之後,回过神来,已经快1...

[Java Day07] 2.2. 初始化区块

教材网址 https://coding104.blogspot.com/2021/06/java-a...

零风险(zero risks)

-ISO 31000 在风险管理社区中,人们普遍认为无法消除风险,并且“没有风险”是不可能的,因为...

总结篇 — Nightwatch.js E2E 之旅

首先感谢队长 Ian 的邀请,也感谢队友们的鼓励,终於要结束 30 天的挑战了 其实大概写到一半就没...

连续和非连续内存分配之间的区别

在将主内存分配给操作系统中的进程时,有两个主要部分。 在连续内存分配中,进程被分配主内存的顺序块给整...