經常看見一些軟件有著美麗,光艷奪目,非常個性化的圖標,曾幾何時也想擁有,可苦於缺少一點點藝術細胞,只好打起妙手空空的念頭。經過一天的捉摸,也找到一些門道,效果還佳,再用Delphi自帶的Image Editor加以修改,徹底私有化,為我所用。灑家用此法做了一些漂亮的圖標,令老友羨慕不已。好了,好東西當然不敢獨享,且容我為各位慢慢道來:
首先用API函數LoadLibrary(lpLibFileName: PChar): HMODULE; stdcall; 把指定的程序模塊映射到調用程序的地址空間去。該函數返回模塊的句柄,假定為hExe;
然後用EnumResourceNames(hExe,RT_ICON,@EnumResName,myflag)找到圖標的資源名,其中參數EnumResName是回調函數,圖標的name值將由回調函數的一個變參得到。
然後再向回調函數體內加入代碼提取圖標就可以了。EnumResourceNames函數將會一直查找每一個圖標直到查找失敗或EnumResName返回false值為止。這樣我們就能提取程序所包含的每一個圖標,搾盡最後一滴油,夠狠吧!
詳細代碼如下:
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, Menus, ImgList;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
Open1: TMenuItem;
Save1: TMenuItem;
Exit1: TMenuItem;
OpenDialog1: TOpenDialog;
ListView1: TListVIEw;
StatusBar1: TStatusBar;
ImageList1: TImageList;
procedure Open1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Exit1Click(Sender: TObject);
private
{ Private declarations }
FileName: TFileName;
FIcon: TIcon;
public
{ Public declarations }
end;
Function EnumResName(hModule: HMODULE; lpType, lpName: PChar;lParam: Longint):BOOL;stdcall;
var
Form1: TForm1;
hExe: HINST;
implementation
{$R *.DFM}
Function EnumResName(hModule: HMODULE; lpType, lpName: PChar;lParam: Longint):BOOL; //回調函數不能是對象方法
//lpType是要提取的資源類型,這裡為RT_ICON
//lpName為圖標資源名
var
hIcon1: HICON;
hResource: HRSRC;
hMem: HRSRC;
lpResource: PByte;
ListItem: TListItem;
Begin
//返回在hExe模塊中lpName指定的圖標資源的句柄;
hResource:=FindResource(hExe,lpName,RT_ICON);
//Load and Lock the icon
hMem:=LoadResource(hExe,hResource);
lpResource:=LockResource(hMem);
//從資源文件中創建圖標並返回其句柄
hIcon1:=Create Iconfrom Resource Ex(lp Resource,Sizeof Resource(hExe,h Resource),True,$00030000,32,32,LR-DEFAULTCOLOR);
with Form1 do
begin
//用控件ImageList保存提取的圖標,並在ListVIEw中顯示出來
FIcon.Handle:=hIcon1;
ImageList1.AddIcon(fIcon);
ListItem:=ListVIEw1.Items.Add;
ListItem.ImageIndex:=ImageList1.Count-1;
end;
result:=true;//注意:只有返回為真值時,才能繼續提取下一個圖標
end;
procedure TForm1.Open1Click(Sender: TObject);
var
myflag: integer;
begin
if Opendialog1.Execute then
begin
FileName:=Opendialog1.FileName;
StatusBar1.Panels.Items[0].Text:=FileName;
Opendialog1.HistoryList.Add(FileName);
hExe:=LoadLibrary((PCharFileName));
myflag:=1;
ImageList1.Clear;
ListVIEw1.Items.Clear;
EnumResourceNames(hExe,RT_ICON,@EnumResName,myflag);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FIcon:=TIcon.Create;
Opendialog1.Filter:='可執行文件(*.exe)|*.exe';
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FIcon.Free;
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;
end.
篇幅所限,存盤和查錯的代碼沒有列出