最近本人做的项目中,需要将一系列图片列表显示到一个ListBox中,本人使用的raize套件中的RzListBox,这里讲的方法同样适用于这两种组件,首先设置ListBox的style为:lbOwnerDrawVariable,然后处理ListBox的MeasureItem与DrawItem事件。
代码如下:
运行结果如下:
代码如下:
procedure TFrameAuction.RzListBoxMeasureItem(Control: TWinControl;
Index: Integer; var Height: Integer);
begin
Height:=82;//设置item的高度
end;
Index: Integer; var Height: Integer);
begin
Height:=82;//设置item的高度
end;
procedure TFrameAuction.RzListBoxDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
jpeg:TJPEGImage;
bmp:TBitMap;
root,cnode: IXMLNode;
icon: TIcon;
fname:string;
begin
root:=self.FXMLDoc.DocumentElement;
cnode:=root.ChildNodes[Index];
with (Control as TRzListBox).Canvas do begin
bmp:=TBitmap.Create;
jpeg:=TJPEGImage.Create;
if cnode.GetAttribute('photo')='' then begin
fname:=ExtractFilePath(Application.ExeName)+'images\none.jpg';
end else begin
fname:=ExtractFilePath(Application.ExeName)+'images\'+root.GetAttribute('id')+'-'+cnode.GetAttribute('photoid')+'-'+cnode.GetAttribute('photo');
if not FileExists(fname) then
fname:=ExtractFilePath(Application.ExeName)+'images\none.jpg';
end;
jpeg.LoadFromFile(fname);
with bmp do
begin
PixelFormat:=pf24bit;
Height:=60;
Width:=self.RzListBox.Width;
Canvas.Brush.Color:=$00F0EDE6;
Canvas.FillRect(Canvas.ClipRect);
Canvas.StretchDraw(Bounds(0,0,80,60), jpeg);
end;
FillRect(Rect);
Draw(Rect.Left+1, Rect.Top + 1,bmp);
icon:=TIcon.Create;
if cnode.GetAttribute('status')='1' then
self.ImageListStatus.GetIcon(8,icon)
else if cnode.GetAttribute('status')='3' then
self.ImageListStatus.GetIcon(2,icon)
else if cnode.GetAttribute('status')='2' then begin
if cnode.GetAttribute('bargainflag')='0' then
self.ImageListStatus.GetIcon(6,icon)
else
self.ImageListStatus.GetIcon(4,icon)
end else if cnode.GetAttribute('status')='0' then begin
if root.GetAttribute('activeitem')=cnode.GetAttribute('itemid') then
self.ImageListStatus.GetIcon(0,icon);
end;
Draw(Rect.Left+1+80+5,Rect.Top+22,icon);
TextOut(1,Rect.Top+64,cnode.GetAttribute('no')+'号:'+cnode.GetAttribute('itemtitle'));
end;
icon.Free;
bmp.Free;
jpeg.Free;
end;
Rect: TRect; State: TOwnerDrawState);
var
jpeg:TJPEGImage;
bmp:TBitMap;
root,cnode: IXMLNode;
icon: TIcon;
fname:string;
begin
root:=self.FXMLDoc.DocumentElement;
cnode:=root.ChildNodes[Index];
with (Control as TRzListBox).Canvas do begin
bmp:=TBitmap.Create;
jpeg:=TJPEGImage.Create;
if cnode.GetAttribute('photo')='' then begin
fname:=ExtractFilePath(Application.ExeName)+'images\none.jpg';
end else begin
fname:=ExtractFilePath(Application.ExeName)+'images\'+root.GetAttribute('id')+'-'+cnode.GetAttribute('photoid')+'-'+cnode.GetAttribute('photo');
if not FileExists(fname) then
fname:=ExtractFilePath(Application.ExeName)+'images\none.jpg';
end;
jpeg.LoadFromFile(fname);
with bmp do
begin
PixelFormat:=pf24bit;
Height:=60;
Width:=self.RzListBox.Width;
Canvas.Brush.Color:=$00F0EDE6;
Canvas.FillRect(Canvas.ClipRect);
Canvas.StretchDraw(Bounds(0,0,80,60), jpeg);
end;
FillRect(Rect);
Draw(Rect.Left+1, Rect.Top + 1,bmp);
icon:=TIcon.Create;
if cnode.GetAttribute('status')='1' then
self.ImageListStatus.GetIcon(8,icon)
else if cnode.GetAttribute('status')='3' then
self.ImageListStatus.GetIcon(2,icon)
else if cnode.GetAttribute('status')='2' then begin
if cnode.GetAttribute('bargainflag')='0' then
self.ImageListStatus.GetIcon(6,icon)
else
self.ImageListStatus.GetIcon(4,icon)
end else if cnode.GetAttribute('status')='0' then begin
if root.GetAttribute('activeitem')=cnode.GetAttribute('itemid') then
self.ImageListStatus.GetIcon(0,icon);
end;
Draw(Rect.Left+1+80+5,Rect.Top+22,icon);
TextOut(1,Rect.Top+64,cnode.GetAttribute('no')+'号:'+cnode.GetAttribute('itemtitle'));
end;
icon.Free;
bmp.Free;
jpeg.Free;
end;
运行结果如下: