列出 TListView 目录中的文件

发布于 2024-08-17 19:14:54 字数 184 浏览 7 评论 0原文

我正在构建一个程序,需要在 Form_Create 上填充一个名为 FileListTListView,我想要填充的目录是编译后的目录程序是+ \Files,因为我从未使用过TListView 我想知道如何做到这一点?

I'm building a program that needs to on Form_Create, populate a TListView called FileList, the directory that I want to populate is where the compiled program is + \Files, as I never used a TListView I want to know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗫嚅 2024-08-24 19:14:54

您的问题有多个部分。我将在这里提供概述。如果您在任何特定步骤上需要帮助,请发布更具体的后续问题。

  1. 确定“编译后的程序在哪里”指的是什么。

    要获取 EXE 文件的完整路径,请调用 ParamStr(0)。要从该字符串中删除 EXE 文件名,这样您就只有目录部分,请调用 提取文件路径。确保它以反斜杠结尾(IncludeTrailingPathDelimiter)然后附加您的“文件”目录。

  2. 获取文件列表。

    使用FindFirstFindNext 创建一个循环来查看所有文件。名称将包含“.”。和“..”相对目录名称,因此您可能希望排除它们。请注意,文件不按任何特定顺序枚举。例如,如果您需要按字母顺序对它们进行排序,则需要您自己执行此操作。 (在您的测试中它们可能看起来按字母顺序排列,但这并不总是如此。)

    <前><代码>变量
    记录:TSearchRec;
    开始
    如果 FindFirst(path + '\*', faAnyFile, Rec) = 0 则尝试
    重复
    if (Rec.Name = '.') 或 (Rec.Name = '..') then
    继续;
    如果(Rec.Attr 和 faVolumeID)= faVolumeID 那么
    继续; // 与卷 ID 无关
    if (Rec.Attr 和 faHidden) = faHidden 那么
    继续; // 遵守操作系统“隐藏”设置
    if (Rec.Attr 和 faDirectory) = faDirectory 那么
    ; // 这是一个目录。可能想做一些特别的事情。
    DoSomethingWithFile(Rec.Name);
    直到 FindNext(Rec) <> 0;
    最后
    SysUtils.FindClose(Rec);
    结尾;
    结尾;

  3. 向控件添加节点以表示文件。

    您可能希望在我上面提到的假设的 DoSomethingWithFile 函数中执行此操作。使用列表视图的 Items 属性执行以下操作与项目有关的事情,例如添加新项目。

    <前><代码>变量
    项目:TListItem;
    开始
    项目 := ListView.Items.Add;
    Item.Caption := 文件名;
    结尾;

    TListItem 具有其他属性;查看文档了解详细信息。 SubItems 属性在以下情况下很有用:正在以“报告”模式显示列表视图,其中单个节点可以有多个列。

  4. 将图像与项目相关联。

    列表视图中的节点图像来自关联的图像列表,LargeImagesSmallImages 。它们引用您的一个或多个 TImageList 组件形式。将图标图像放在那里,然后指定项目的 ImageIndex 属性到相应的数字。

根据您希望程序的复杂程度,您可能希望使用 Delphi 的 TShellListView 控件,而不是自己完成上述所有工作。

There are multiple parts to your question. I'll provide an overview here. If you need help on any particular step, please post a more specific follow-up question.

  1. Determine what "where the compiled program is" refers to.

    To get the full path of the EXE file, call ParamStr(0). To remove the EXE file name from that string, so you have just the directory portion, call ExtractFilePath. Make sure it ends with a backslash (IncludeTrailingPathDelimiter) and then append your "Files" directory.

  2. Get a list of files.

    Use FindFirst and FindNext to make a loop that looks at all the files. The names will include the "." and ".." relative directory names, so you may wish to exclude them. Beware that the files are not enumerated in any particular order. If you need them sorted alphabetically, for instance, you'll need to do that yourself. (They may appear to be in alphabetical order in your tests, but that won't always be true.)

    var
      Rec: TSearchRec;
    begin
      if FindFirst(path + '\*', faAnyFile, Rec) = 0 then try
        repeat
          if (Rec.Name = '.') or (Rec.Name = '..') then
            continue;
          if (Rec.Attr and faVolumeID) = faVolumeID then
            continue; // nothing useful to do with volume IDs
          if (Rec.Attr and faHidden) = faHidden then
            continue; // honor the OS "hidden" setting
          if (Rec.Attr and faDirectory) = faDirectory then
            ; // This is a directory. Might want to do something special.
          DoSomethingWithFile(Rec.Name);
        until FindNext(Rec) <> 0;
      finally
        SysUtils.FindClose(Rec);
      end;
    end;
    
  3. Add nodes to the control to represent the files.

    You might wish to do this in the hypothetical DoSomethingWithFile function I mentioned above. Use the list view's Items property to do things with the items, such as add new ones.

    var
      Item: TListItem;
    begin
      Item := ListView.Items.Add;
      Item.Caption := FileName;
    end;
    

    TListItem has additional properties; check the documentation for details. The SubItems property is useful if you're showing the list view in "report" mode, where there can be multiple columns for a single node.

  4. Associate images with the items.

    Nodes' images in a list view come from the associated image lists, LargeImages and SmallImages. They refer to one or more TImageList components on your form. Put your icon images there, and then assign the items' ImageIndex properties to the corresponding numbers.

Depending on how elaborate you want your program to be, you may wish to use Delphi's TShellListView control instead of doing all the above work yourself.

庆幸我还是我 2024-08-24 19:14:54

如果将 TImagelist 放在包含两个图像的窗体上(一个是前文件,一个是目录),然后将 TImagelist 分配给 TListviews LargeImages 属性,则可以使用以下代码。

procedure TForm2.Button1Click(Sender: TObject);
    var li:TListItem;
    SR: TSearchRec;
begin
    FileList.Items.BeginUpdate;
    try
        FileList.Items.Clear;

        FindFirst(ExtractFilePath(Application.ExeName) +'*.*', faAnyFile, SR);
        try
            repeat
                li :=  FileList.Items.Add;
                li.Caption := SR.Name;

                if ((SR.Attr and faDirectory) <> 0)  then li.ImageIndex := 1
                else li.ImageIndex := 0;

            until (FindNext(SR) <> 0);
        finally
            FindClose(SR);
        end;
    finally
        FileList.Items.EndUpdate;
    end;
end;

然后,您可以在此基础上为不同的文件类型添加不同的图像到 TImageList,并使用 ExtractFileExt(SR.Name) 获取文件扩展名

if ((SR.Attr and faDirectory) <> 0)  then li.ImageIndex := 1
else if lowercase(ExtractFileExt(SR.Name)) = '.png' then li.ImageIndex := 2
else if lowercase(ExtractFileExt(SR.Name)) = '.pdf' then li.ImageIndex := 3
else li.ImageIndex := 0;

If you drop a TImagelist on the form with two images in (one fore files and on for directories), then assign the TImagelist to the TListviews LargeImages property, you can use the below code.

procedure TForm2.Button1Click(Sender: TObject);
    var li:TListItem;
    SR: TSearchRec;
begin
    FileList.Items.BeginUpdate;
    try
        FileList.Items.Clear;

        FindFirst(ExtractFilePath(Application.ExeName) +'*.*', faAnyFile, SR);
        try
            repeat
                li :=  FileList.Items.Add;
                li.Caption := SR.Name;

                if ((SR.Attr and faDirectory) <> 0)  then li.ImageIndex := 1
                else li.ImageIndex := 0;

            until (FindNext(SR) <> 0);
        finally
            FindClose(SR);
        end;
    finally
        FileList.Items.EndUpdate;
    end;
end;

You can then build on this by adding different images to the TImageList for different file types, and using ExtractFileExt(SR.Name) to get the files extension

if ((SR.Attr and faDirectory) <> 0)  then li.ImageIndex := 1
else if lowercase(ExtractFileExt(SR.Name)) = '.png' then li.ImageIndex := 2
else if lowercase(ExtractFileExt(SR.Name)) = '.pdf' then li.ImageIndex := 3
else li.ImageIndex := 0;
成熟稳重的好男人 2024-08-24 19:14:54

绘制行时您需要显示图像。

这应该会给你一个想法:
http://www.delphidabbler.com/articles?article=16
http://delphi.about.com/od/delphitips2008/qt/lv_checkbox_bmp。 htm

唯一的区别是您将绘制一个图标/图像。
您可以在此处了解如何在网格中执行此操作: http://delphi.about.com /library/weekly/aa032205a.htm
所以从两者你就可以得到这个想法。

You'll need to show the images when drawing the rows.

This should give you an idea:
http://www.delphidabbler.com/articles?article=16
http://delphi.about.com/od/delphitips2008/qt/lv_checkbox_bmp.htm

The only difference is that you'll draw an icon/image.
Here you learn how to do it in a grid: http://delphi.about.com/library/weekly/aa032205a.htm
So from both you can get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文