如何得到某种文件类型对应的图标路径(100分)

  • 主题发起人 主题发起人 bootdisk
  • 开始时间 开始时间
B

bootdisk

Unregistered / Unconfirmed
GUEST, unregistred user!
WINSHELLAPI DWORD WINAPI SHGetFileInfo(

LPCTSTR pszPath,
DWORD dwFileAttributes,
SHFILEINFO FAR *psfi,
UINT cbFileInfo,
UINT uFlags
);


想得到某文件类型的图标文件路径
ShGetFileInfo(PChar(Filename), 0, SHFileInfo, SizeOf(SHFileInfo), SHGFI_ICONLOCATION);
不成功,求教用过该函数的大侠

帮助里写的
SHGFI_ICONLOCATION Retrieves the name of the file that contains the icon representing the file. The name is copied to the szDisplayName member of the structure specified by psfi.
 
procedure TForm1.Button1Click(Sender: TObject);

var

ShFileInfo : TSHFileInfo;

SysListHandle : THandle;

begin

// Retrieve small icon, type name, and icon location

// in the system image list.

SysListHandle := SHGetFileInfo(PChar(Edit1.Text), 0,

ShFileInfo, SizeOf(TSHFileInfo), SHGFI_DISPLAYNAME or

SHGFI_SMALLICON or SHGFI_ICON or SHGFI_TYPENAME or

SHGFI_ICONLOCATION);

// Assign retrieved icon to the small icon Image.

Image1.Picture.Icon.Handle := ShFileInfo.hIcon;

// If SysListHandle is <> 0, it's a valid file.

if SysListHandle <> 0 then

begin

// Assign display name label.

Label3.Caption := string(ShFileInfo.szDisplayName);

// Assign icon index label.

Label5.Caption := IntToStr(ShFileInfo.iIcon);

// Assign type name label.

Label7.Caption := string(ShFileInfo.szTypeName);

// 2nd call to SHGetFileInfo to retrieve normal icon.

SHGetFileInfo(PChar(Edit1.Text), 0, ShFileInfo,

SizeOf(TSHFileInfo), SHGFI_ICON);

// Assign retrieved icon to large icon TImage.

Image2.Picture.Icon.Handle := ShFileInfo.hIcon;

end

else

begin

// Handle invalid filename.

Label3.Caption := 'Not a valid filename';

Label5.Caption := '';

Label7.Caption := '';

end;

end;
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ShellApi, ExtCtrls, FileCtrl;

type
TForm1 = class(TForm)
FileListBox1: TFileListBox;
DirectoryListBox1: TDirectoryListBox;
DriveComboBox1: TDriveComboBox;
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Image1: TImage;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
FileInfo: TShFileInfo;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
FileName: String;
begin
if FileListBox1.ItemIndex<>-1 then
FileName:= FileListBox1.FileName
else
FileName:= DirectoryListBox1.Directory;
Edit1.Text:= FileName;
SHGetFileInfo(pChar(FileName),0,FileInfo,SizeOf(FileInfo),SHGFI_DISPLAYNAME);
Edit2.Text:= FileInfo.szDisplayName;
SHGetFileInfo(pChar(FileName),0,FileInfo,SizeOf(FileInfo),SHGFI_TYPENAME);
Edit3.Text:= FileInfo.szTypeName;
SHGetFileInfo(pChar(FileName),0,FileInfo,SizeOf(FileInfo),SHGFI_ICON);
Image1.Picture.Icon.Handle:= FileInfo.hIcon;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
Edit2.Text := '';
Edit3.Text := '';
DriveComboBox1.Drive := 'C';

end;

end.
 
我是想得到某文件类型对应的图标位置,比如对于txt文件,读注册表
HKEY_CLASSES_ROOT/txtfile/DefaultIcon可以得到
%SystemRoot%/system32/shell32.dll,-152
即图标在%SystemRoot%/system32/shell32.dll中,index为152
想得到这个路径
 
那你直接读注册表不就可以了。
 
有的文件这样读不出来。。。
 
后退
顶部