如何用API函数从某个EXE或Doc文件中提取出它的图标

fangheling 2001-12-04 12:31:44
如何用API函数从某个EXE文件中提取出它的图标
...全文
81 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
王集鹄 2001-12-04
  • 打赏
  • 举报
回复
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses
Registry, ShellApi;

function StrLeft(const mStr: string; mDelimiter: string): string;
begin
Result := Copy(mStr, 1, Pos(mDelimiter, mStr) - 1);
end; { StrLeft }

function StrRight(const mStr: string; mDelimiter: string): string;
begin
if Pos(mDelimiter, mStr) <= 0 then
Result := ''
else Result := Copy(mStr, Pos(mDelimiter, mStr) + Length(mDelimiter), MaxInt);
end; { StrRight }

function FileIcon(mFileName: TFileName): THandle; { 返回文件的图标 }
var
vRegistry: TRegistry;
vExtension, vIconFileName: TFileName;
vIconIndex: Integer;
vExtDescription: string;
var
vBuffer: array[0 .. MAX_PATH] of Char;
vSysPath: string;
begin
Result := 0;
if not FileExists(mFileName) then Exit;

GetSystemDirectory(vBuffer, MAX_PATH + 1);
vSysPath := string(vBuffer) + '\';

vRegistry := TRegistry.Create;
try
vExtension := UpperCase(ExtractFileExt(mFilename));
if (vExtension = '.EXE') or (vExtension = '.ICO') then begin
vIconFileName := mFilename;
vIconIndex := 0;
end else begin
vRegistry.RootKey := HKEY_CLASSES_ROOT;
if vRegistry.OpenKey(vExtension, False) then begin
vExtDescription := vRegistry.ReadString('');
vRegistry.OpenKey('\' + vExtDescription, False);
vRegistry.OpenKey('DefaultIcon', False);
vIconFileName := vRegistry.ReadString('');
vIconIndex := StrToIntDef(StrRight(vIconFileName, ','), 0);
vIconFileName := StrLeft(vIconFileName, ',');
end else begin
vIconFileName := vSysPath + 'Shell32.dll';
vIconIndex := 0;
end;
end;
Result := ExtractIcon(HInstance, PChar(vIconFileName), UINT(vIconIndex));
finally
vRegistry.Free;
end;
end; { FileIcon }

procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Image1.Picture.Icon.Handle := FileIcon(OpenDialog1.FileName);
end;

end.
taxi 2001-12-04
  • 打赏
  • 举报
回复
ExtractIcon可提取exe,dll,icl文件的图标
ExtractAssociatedIcon用来提取任何文件的图标,一般用来提取除exe,dll,icl以外的文件图标,具体怎么用看帮助,很简单的。
Sephil 2001-12-04
  • 打赏
  • 举报
回复
ExtractIcon
ExtractIconEx
没有记错的话

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧