extractIcon取得应用程序的图标

procedure GetExeIcon(FileName:string);
var
  icon:Ticon;
begin
  icon := TIcon.create;
  try
    icon.handle := extractIcon(hInstance,pchar(FileName),0);
  finally
    icon.free;
  end;
end;

附  ExtarctIcon 的联机帮助

the ExtractIcon function retrieves the handle of an icon from the specified executable file, dynamic-link library (DLL), or icon file.

这个函数可以从可执行文件,动态连接库,或 icon 文件中取得 icon 的 handle

HICON ExtractIcon(

    HINSTANCE hInst, // instance handle  实例句柄,传 hinstance就行
    LPCTSTR lpszExeFileName, // filename of file with icon   文件的路径
    UINT nIconIndex  // index of icon to extract             图标索引
   ); 
 

Parameters

hInst

Identifies the instance of the application calling the function.

lpszExeFileName

Points to a null-terminated string specifying the name of an executable file, DLL, or icon file.

nIconIndex

Specifies the index of the icon to retrieve. If this value is 0, the function returns the handle of the first icon in the specified file. If this value is -1, the function returns the total number of icons in the specified file.

值为 0 ,则返回第一个 icon的 handle ,为 -1  则返回 icon 的总数

 

Return Values

If the function succeeds, the return value is the handle to an icon. If the file specified was not an executable file, DLL, or icon file, the return is 1. If no icons were found in the file, the return value is NULL.


注意:要用 -1 来返回 icon 的个数时,由于 uInt 是无符号类型,所以提示为

Constant expression violates subrange bounds

解决办法为用 $FFFFFFFF 来代替 -1

这可得到的是 32 * 32 大小的图标

-------------------------------------------
这就是HubDog的《Delphi之未经证实的葵花宝典version 1.8》 中的有关提取图标的内容,
请自己试一试吧


   WINSHELLAPI DWORD WINAPI SHGetFileInfo(
    LPCSTR  pszPath,
    DWORD  dwFileAttributes,
    SHFILEINFO FAR  *psfi,
    UINT  cbFileInfo,
    UINT  uFlags
   );
它的作用是:取回文件系统中的一个对象的信息,对象可以是文件、文件夹、
目录或驱动器的根目录。经过三个多小时的调试,我终于完全弄明白怎样取
得并显示一个文件的图标了:包括大图标、小图标,象资源管理器上的那样。
操作过程大体如下:
var ShFileInfo: TSHFILEINFO;
    FileList:TListView;
begin
  ...
  Result := FileList.Items.Add;
  with Result do
  begin
    Caption:=filename;
    ShGetFileInfo(pchar(vartostr(filename)), 0, SHFileInfo, SizeOf(SHFileInfo),
        SHGFI_SMALLICON or SHGFI_SYSICONINDEX or SHGFI_TYPENAME)=0 then
        showmessage('error in shgetfileinfo');
    ImageIndex := SHFileInfo.iIcon;
  end;
  ...
end;
这是最关键的几个地方,中间省略了许多细节。
另:我在6月17日的笔记里提到ExtractAssociatedIcon()和ExtractIcon()函数,
也可以提取文件的图标,但是速度比这个方法要慢上许多,而且我不会用它们提
取小图标。
//
uses ShellApi;

procedure TForm1.Button1Click(Sender: TObject);
var
  Icon : hIcon;
  IconIndex : word;

begin
 Icon := ExtractAssociatedIcon(HInstance,
                               'C:/SomePath/SomeFile.ext',
                               IconIndex);
 DrawIcon(Form1.Canvas.Handle, 10, 10, Icon);
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值