一
方法一
使用API函數ImageList_Write和ImageList_Read
IStream是一個OLE對象
方法二
從TImageList繼承一個子類TImageListEx
二
自定義的TImageListEx控件在Public部分一並實現了對上述兩種方法的封裝
TImageListEx類源代碼如下
unit ImageListEx;
interface
uses Windows
type
TImageListEx = class(TImageList)
public
procedure LoadFromFile(const FileName: string);//實現API方式保存
procedure LoadFromStream(Stream: TStream);
procedure SaveToFile(const FileName: string);
procedure SaveToStream(Stream: TStream);
procedure LoadFromFileEx(const FileName: string);//實現自定義方式保存
procedure LoadFromStreamEx(Stream: TStream);
procedure SaveToFileEx(const FileName: string);
procedure SaveToStreamEx(Stream: TStream);
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents(
end;
{ TImageListEx }
procedure TImageListEx
var
Stream: TStream;
begin
Stream := TFileStream
try
LoadFromStream(Stream);
finally
Stream
end;
end;
procedure TImageListEx
var
Stream: TStream;
begin
Stream := TFileStream
try
LoadFromStreamEx(Stream);
finally
Stream
end;
end;
procedure TImageListEx
var
SA: TStreamAdapter;
begin
SA := TStreamAdapter
try
Handle := ImageList_Read(SA);//將當前圖像列表的句柄指向從二進制流中得到的句柄
if Handle =
raise EReadError
finally
SA
end;
end;
procedure TImageListEx
var
Width
Bitmap
BinStream: TMemoryStream;
procedure LoadImageFromStream(Image: TBitmap);
var
Count: DWORD;
begin
Image
Stream
BinStream
BinStream
BinStream
Image
end;
begin
Stream
Stream
Self
Self
Bitmap := TBitmap
Mask := TBitmap
BinStream := TMemoryStream
try
while Stream
begin
LoadImageFromStream(Bitmap);//從二進制流中讀出位圖
LoadImageFromStream(Mask);//從二進制流中讀出掩碼位圖
Add(Bitmap
end;
finally
Bitmap
Mask
BinStream
end;
end;
procedure TImageListEx
var
Stream: TStream;
begin
Stream := TFileStream
try
SaveToStream(Stream);
finally
Stream
end;
end;
procedure TImageListEx
var
Stream: TStream;
begin
Stream := TFileStream
try
SaveToStreamEx(Stream);
finally
Stream
end;
end;
procedure TImageListEx
var
SA: TStreamAdapter;
begin
SA := TStreamAdapter
try
if not ImageList_Write(Handle
raise EWriteError
finally
SA
end;
end;
procedure TImageListEx
var
I: Integer;
Width
Bitmap
BinStream: TMemoryStream;
procedure SetImage(Image: TBitmap; IsMask: Boolean);
begin
Image
with Image do
begin
if IsMask then Monochrome := True;//掩碼位圖必須使用單色
Height := Self
Width := Self
end;
end;
procedure SaveImageToStream(Image: TBitmap);
var
Count: DWORD;
begin
BinStream
Image
Count := BinStream
Stream
Stream
end;
begin
Height := Self
Width := Self
Stream
Stream
Bitmap := TBitmap
Mask := TBitmap
BinStream := TMemoryStream
try
for I :=
begin
SetImage(Bitmap
SetImage(Mask
GetImages(I
SaveImageToStream(Bitmap);//保存位圖到二進制流中
SaveImageToStream(Mask);//保存掩碼位圖到二進制流中
end;
finally
Bitmap
Mask
BinStream
end;
end;
end;
下面示范在Delphi中的使用方法
首先在Delphi中新建一個項目
ImageListEx
ImageListEx
在Button
在Button
在Button
運行程序
三
本文介紹的內容已用於解決本人在實際項目中遇到的情況
From:http://tw.wingwit.com/Article/program/Delphi/201311/8481.html