简体   繁体   English

如何将TShellListView项目筛选到特定的文件扩展名?

[英]How to filter TShellListView items to a specific file extension?

I'm working on a project in Delphi 7 that needs ShellListView1 so show .PNG or .JPG files only. 我正在Delphi 7中开发一个需要ShellListView1的项目,因此仅显示.PNG或.JPG文件。

How can I view only folders and specific file types (example: ' .exe; .bat') ? 如何仅查看文件夹和特定文件类型(例如:“。 exe; .bat”)?

I was told is a ShellListView1 component with masking but websites I try are offline. 有人告诉我是一个带遮罩的ShellListView1组件,但我尝试访问的网站处于脱机状态。

You can write a handler for the OnAddFolder event, which fires whenever an item is going to be added to the list. 您可以为OnAddFolder事件编写一个处理程序,该事件将在将项目添加到列表时触发。 The following code allows to add only files with *.exe or *.bat extension to the list: 以下代码仅允许将扩展名为*.exe*.bat文件添加到列表中:

procedure TForm1.ShellListView1AddFolder(Sender: TObject;
  AFolder: TShellFolder; var CanAdd: Boolean);
var
  FileExt: string;
begin
  CanAdd := not AFolder.IsFolder;
  if CanAdd then
  begin
    FileExt := ExtractFileExt(AFolder.PathName);
    CanAdd := (FileExt = '.exe') or (FileExt = '.bat');
  end;
end;
uses Masks;
...
procedure TForm1.ShellListView1AddFolder(Sender: TObject;
  AFolder: TShellFolder; var CanAdd: Boolean);
begin
  CanAdd := AFolder.IsFolder or MatchesMask(AFolder.PathName, '*.exe');
end;

function MatchesMask() returns True is a string value matches a format specifed by a mask. MatchesMask()函数返回True是一个与掩码指定的格式匹配的字符串值。

Syntactically valid Mask consists of literal characters, sets, and wildcards. 语法上有效的Mask由文字字符,集合和通配符组成。 Wildcards are asterisks (*) or question marks (?). 通配符是星号(*)或问号(?)。 An asterisk matches any number of characters. 星号匹配任意数量的字符。 A question mark matches a single arbitrary character. 问号与单个任意字符匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM