请问如何获得某程序或者某文件的图标,然后显示到 TImage 组件之上? ( 积分: 50 )

  • 主题发起人 主题发起人 snail_chn
  • 开始时间 开始时间
S

snail_chn

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,需要分开获得?主要是如何讲起显示到 TImage 组件上。thx!
 
如题,需要分开获得?主要是如何讲起显示到 TImage 组件上。thx!
 
首先用API函数LoadLibrary(lpLibFileName: PChar): HMODULE; stdcall; 把指定的程序模块映射到调用程序的地址空间去。该函数返回模块的句柄,假定为hExe;<br>然后用EnumResourceNames(hExe,RT_ICON,@EnumResName,myflag)找到图标的资源名,其中参数EnumResName是回调函数,图标的name值将由回调函数的一个变参得到。<br>然后再向回调函数体内加入代码提取图标就可以了。EnumResourceNames函数将会一直查找每一个图标直到查找失败或EnumResName返回false值为止。这样我们就能提取程序所包含的每一个图标,榨尽最后一滴油,够狠吧!<br>详细代码如下:<br>unit main;<br>interface<br>uses<br>Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>ComCtrls, Menus, ImgList;<br>type<br>TForm1 = class(TForm)<br>MainMenu1: TMainMenu;<br>File1: TMenuItem;<br>Open1: TMenuItem;<br>Save1: TMenuItem;<br>Exit1: TMenuItem;<br>OpenDialog1: TOpenDialog;<br>ListView1: TListView;<br>StatusBar1: TStatusBar;<br>ImageList1: TImageList;<br>procedure Open1Click(Sender: TObject);<br>procedure FormCreate(Sender: TObject);<br>procedure FormDestroy(Sender: TObject);<br>procedure Exit1Click(Sender: TObject);<br>private<br>{ Private declarations }<br>FileName: TFileName;<br>FIcon: TIcon;<br>public<br>{ Public declarations }<br>end;<br>Function EnumResName(hModule: HMODULE; lpType, lpName: PChar;lParam: Longint):BOOL;stdcall; <br>var<br>Form1: TForm1;<br>hExe: HINST;<br>implementation<br>{$R *.DFM}<br>Function EnumResName(hModule: HMODULE; lpType, lpName: PChar;lParam: Longint):BOOL; //回调函数不能是对象方法<br>//lpType是要提取的资源类型,这里为RT_ICON<br>//lpName为图标资源名<br>var<br>hIcon1: HICON;<br>hResource: HRSRC;<br>hMem: HRSRC;<br>lpResource: PByte;<br>ListItem: TListItem;<br>Begin<br>//返回在hExe模块中lpName指定的图标资源的句柄;<br>hResource:=FindResource(hExe,lpName,RT_ICON); <br>//Load and Lock the icon<br>hMem:=LoadResource(hExe,hResource);<br>lpResource:=LockResource(hMem);<br>//从资源文件中创建图标并返回其句柄<br>hIcon1:=Create Iconfrom Resource Ex(lp Resource,Sizeof Resource(hExe,h Resource),True,$00030000,32,32,LR-DEFAULTCOLOR);<br>with Form1 do<br>begin<br>//用控件ImageList保存提取的图标,并在ListView中显示出来<br>FIcon.Handle:=hIcon1;<br>ImageList1.AddIcon(fIcon);<br>ListItem:=ListView1.Items.Add;<br>ListItem.ImageIndex:=ImageList1.Count-1;<br>end;<br>result:=true;//注意:只有返回为真值时,才能继续提取下一个图标<br>end;<br>procedure TForm1.Open1Click(Sender: TObject);<br>var<br>myflag: integer;<br>begin<br>if Opendialog1.Execute then<br>begin<br>FileName:=Opendialog1.FileName;<br>StatusBar1.Panels.Items[0].Text:=FileName;<br>Opendialog1.HistoryList.Add(FileName);<br>hExe:=LoadLibrary((PCharFileName));<br>myflag:=1;<br>ImageList1.Clear;<br>ListView1.Items.Clear;<br>EnumResourceNames(hExe,RT_ICON,@EnumResName,myflag);<br>end;<br>end;<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>FIcon:=TIcon.Create;<br>Opendialog1.Filter:='可执行文件(*.exe)|*.exe';<br>end;<br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br>FIcon.Free;<br>end;<br>procedure TForm1.Exit1Click(Sender: TObject);<br>begin<br>Close;<br>end;<br>end.<br>PS:若还想从EXE,DLL中获得更多的资源请查看DELPHI的WIN32帮助。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部