简体   繁体   中英

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.

How can I view only folders and specific file types (example: ' .exe; .bat') ?

I was told is a ShellListView1 component with masking but websites I try are offline.

You can write a handler for the OnAddFolder event, which fires whenever an item is going to be added to the list. The following code allows to add only files with *.exe or *.bat extension to the list:

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.

Syntactically valid Mask consists of literal characters, sets, and wildcards. Wildcards are asterisks (*) or question marks (?). An asterisk matches any number of characters. A question mark matches a single arbitrary character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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