this slowpoke moves

Get Shortcut Icons

uses ExtCtrls, StrUtils, ShellAPI,
  CommCtrl, ImgList, ComCtrls
  
//

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then Edit1.Text := OpenDialog1.FileName;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  Icon : TIcon;
  ListItem : TListItem;
  shInfo : TSHFileInfo;
  sFileType : string;
begin
  { initialise ListView and Icon }
  ListView1.SmallImages := imageList1;
  Icon := TIcon.Create;

  try
    ListView1.Items.BeginUpdate;
    ListItem := ListView1.items.add;{ Initialise ListView.Item.Add }

    { get details about file type from SHGetFileInfo }
    SHGetFileInfo(PChar(Edit1.Text), 0, shInfo,
      SizeOf(shInfo), SHGFI_TYPENAME);
    sFileType := shInfo.szTypeName;

    { is this a shortcut file ? }
    if shInfo.szTypeName = 'Shortcut' then
      SHGetFileInfo(PChar(Edit1.Text), 0, shInfo, SizeOf(shInfo),
        SHGFI_LINKOVERLAY or SHGFI_ICON or
        SHGFI_SMALLICON or SHGFI_SYSICONINDEX)
    else
      { ...otherwise treat it as a normal file}
      SHGetFileInfo(PChar(Edit1.Text), 0, shInfo, SizeOf(shInfo),
        SHGFI_ICON or SHGFI_SMALLICON or
        SHGFI_SYSICONINDEX);

    { assign icon }
    Icon.Handle := shInfo.hIcon;

    { List File name, Icon and FileType in ListView}
    ListItem.Caption := ExtractFileName(Edit1.Text);    //...add filename
    ListItem.SubItems.Add(sFileType); //...and filetype..
    ListItem.ImageIndex := ImageList1.AddIcon(Icon); //...and icon.
  finally
    ListView1.Items.EndUpdate; //..free memory on icon and clean up.
    sFileType := '';
    Icon.Free;
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate