dynamic icons with the virtual tree component for delphi
I need to know how to store and load two different icons for each node in the virtual tree component , also the two icons are different in size
Thanks
Unless you're going to owner-draw the whole thing, no control I'm aware of supports heterogeneous icon sizes, Virtual Treeview include. All icons for a given view are taken from a single TImageList
control, and TImageList
only supports one image size at a time.
You can make the icons appear to have different sizes by making the image size be that of the larger icon, and then painting the smaller icons onto larger icons that happen to be padded with transparent borders.
If you only need to support one icon size at a time, then you can maintain two separate TImageList
controls. When you want to switch sizes, reassign the tree control's ImageList
property. You may also need to adjust the DefaultNodeHeight
property, along with the heights of all the nodes that already exist.
This works for me using TPngImageList. I assume this implementation should work with standard TImageList, but using png images is a better choice.
In this example the form class name is "TfPrj", the TvirtualStringTree instance name is "vPrj", and TMyDataRecord is the record structure where you get your data.
In you code manage a custom drawing like this.
var
p: TMyDataRecord;
const
riskImagesSize=64;
riskImagesPadding=2;
procedure FindP(Node: PVirtualNode);
begin
// This procedure fill in the "p" record with your tree data for the node.
end;
procedure TfPrj.vPrjMeasureItem(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
Node: PVirtualNode; var NodeHeight: Integer);
begin
FindP(Node);
if p<>nil then
if p.LargeImages then
NodeHeight := riskImagesSize+riskImagesPadding*2;
end;
procedure TfPrj.vPrjBeforeCellPaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
begin
if Column=0 then begin
FindP(Node);
if p<>nil then begin
if p.LargeImages then begin
Inc(ContentRect.Left,riskImagesSize);
end;
end;
end;
procedure TfPrj.vPrjDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
Node: PVirtualNode; Column: TColumnIndex; const Text: string;
const CellRect: TRect; var DefaultDraw: Boolean);
var
i: integer;
s: string;
begin
FindP(Node);
if Column=0 then
if p<>nil then
if p.LargeImages then begin
iLarge.Draw(TargetCanvas,CellRect.Left-riskImagesSize-riskImagesPadding*2,CellRect.Top+riskImagesPadding,p.LargeImagesIndex);
end;
end;
procedure TfPrj.vPrjGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode;
Kind: TVTImageKind; Column: TColumnIndex; var Ghosted: Boolean;
var ImageIndex: Integer);
begin
if Column<>0 then
Exit;
FindP(Node);
if p.LargeImages then
ImageIndex=-1
else
// Assign the image index for standard TPngImageList
end;
上一篇: 重建树,恢复状态(扩展节点)
下一篇: 带有delphi虚拟树组件的动态图标