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?