列出 TListView 目录中的文件
我正在构建一个程序,需要在 Form_Create
上填充一个名为 FileList
的 TListView
,我想要填充的目录是编译后的目录程序是+ \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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题有多个部分。我将在这里提供概述。如果您在任何特定步骤上需要帮助,请发布更具体的后续问题。
确定“编译后的程序在哪里”指的是什么。
要获取 EXE 文件的完整路径,请调用
ParamStr(0)
。要从该字符串中删除 EXE 文件名,这样您就只有目录部分,请调用提取文件路径
。确保它以反斜杠结尾(IncludeTrailingPathDelimiter
)然后附加您的“文件”目录。获取文件列表。
使用
FindFirst
和FindNext
创建一个循环来查看所有文件。名称将包含“.”。和“..”相对目录名称,因此您可能希望排除它们。请注意,文件不按任何特定顺序枚举。例如,如果您需要按字母顺序对它们进行排序,则需要您自己执行此操作。 (在您的测试中它们可能看起来按字母顺序排列,但这并不总是如此。)<前><代码>变量
记录: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);
结尾;
结尾;
向控件添加节点以表示文件。
您可能希望在我上面提到的假设的
DoSomethingWithFile
函数中执行此操作。使用列表视图的Items
属性执行以下操作与项目有关的事情,例如添加新项目。<前><代码>变量
项目:TListItem;
开始
项目 := ListView.Items.Add;
Item.Caption := 文件名;
结尾;
TListItem
具有其他属性;查看文档了解详细信息。SubItems
属性在以下情况下很有用:正在以“报告”模式显示列表视图,其中单个节点可以有多个列。将图像与项目相关联。
列表视图中的节点图像来自关联的图像列表,
LargeImages
和SmallImages
。它们引用您的一个或多个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.
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, callExtractFilePath
. Make sure it ends with a backslash (IncludeTrailingPathDelimiter
) and then append your "Files" directory.Get a list of files.
Use
FindFirst
andFindNext
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.)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'sItems
property to do things with the items, such as add new ones.TListItem
has additional properties; check the documentation for details. TheSubItems
property is useful if you're showing the list view in "report" mode, where there can be multiple columns for a single node.Associate images with the items.
Nodes' images in a list view come from the associated image lists,
LargeImages
andSmallImages
. They refer to one or moreTImageList
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.如果将 TImagelist 放在包含两个图像的窗体上(一个是前文件,一个是目录),然后将 TImagelist 分配给 TListviews LargeImages 属性,则可以使用以下代码。
然后,您可以在此基础上为不同的文件类型添加不同的图像到 TImageList,并使用 ExtractFileExt(SR.Name) 获取文件扩展名
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.
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
绘制行时您需要显示图像。
这应该会给你一个想法:
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.