I'm need to draw some graphics as node image. Like it draws images from ImageList in OnGetImageIndex event, but from the single source like TIcon, TImage, TBitmap.
In my situation all nodes have it own icons and places in UserData record.
How I can draw theese icons to nodes?
I found this code here, and tried to adept it for my situation:
procedure TForm10.Button1Click(Sender: TObject);
var
Node: PVirtualNode;
begin
VirtualStringTree1.AddChild(nil);
Node := VirtualStringTree1.AddChild(nil);
VirtualStringTree1.AddChild(Node);
Node := VirtualStringTree1.AddChild(Node);
VirtualStringTree1.AddChild(Node);
VirtualStringTree1.AddChild(Node);
VirtualStringTree1.AddChild(Node);
end;
procedure TForm10.VirtualStringTree1AfterItemPaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; ItemRect: TRect);
var
rImage: TRect;
OffsetLeft: Integer;
Icon: TIcon;
begin
rImage := ItemRect;
Icon := TIcon.Create;
Icon.LoadFromFile('TestIcon_16.ico');
with TVirtualStringTree(Sender) do
begin
if (toShowRoot in TreeOptions.PaintOptions) then
OffsetLeft := Indent * (GetNodeLevel(Node) + 1)
else
OffsetLeft := Indent * GetNodeLevel(Node);
Inc(rImage.Left, Margin + OffsetLeft);
Inc(rImage.Top, (NodeHeight[Node] - Icon.Height) div 2);
rImage.Right := rImage.Left + Icon.Width;
rImage.Bottom := rImage.Top + Icon.Height;
end;
DrawIcon(TargetCanvas.Handle, rImage.Left, rImage.Top, Icon.Handle);
end;
After button click, I see that:

Why that's happens? Icon size 100% - 16 x 16 px.
Where I can solve problem with drawning of text?
What I do wrong?
Extentparameter of theOnMeasureTextWidthevent method. And then paint the text by yourself in theOnDrawTextevent method. But much better you'll go with default approach using image list. You can store those images into an image list and to each node store just index to that image list, don't you ? - TLamaDrawIconwhich stretches it. you could useDrawIconExor simplyTargetCanvas.Draw(x, y, Icon). in any case, you leaking memory of the TIcon, and I really don't see why you don't want to use an ImageList withOnGetImageIndexwhich is really the right way to go?... - kobik