delphidelphi-xe8virtualtreeviewtvirtualstringtree

TVirtuailStringTree text and image Alignment


i am drawing text and image in tvirtuailstringtree as following in onbeforecellpaint event

begin
Textrectplace := NewRect;
Textrectplace.Left := Textrectplace.Left + 2;
Textrectplace.Width := 24;
Textrectplace.Height := Data.image.height;
Textrectplace.Top := Textrectplace.Top;
Textrectplace.Bottom := Textrectplace.Bottom;
xOfftext := Textrectplace.Left + Textrectplace.Width + 4;

yOfftext := Textrectplace.Top - 3 + ((Data.image.height - TargetCanvas.TextHeight('H')) div 2);
TargetCanvas.font.color := clgray;
TargetCanvas.font.Size := 10;
TargetCanvas.TextOut(xOfftext, yOfftext, Data.text);
end;

end;


begin
imgrect:= Textrectplace;
imgrect.Left := imgrect.Left + 150;
imgrect.Width := 24;
imgrect.Height := 36;
imgrect.Top := imgrect.Top - 6 + ((Data.image.height - TargetCanvas.TextHeight('H')) div 2);
imgrect.Bottom := imgrect.Bottom;

TargetCanvas.Draw(imgrect.Left, imgrect.Top, Data.image);
end;

I have one problem in text and image alignment I wanted the text to be aligned to left and that part is handled . the image has align problem I wanted to make it aligned to the Right with the text without textoverflow currently if the node has short text its all good and the image showing correctly with the text . but if the text is too long its overflow the image .

here is example image

enter image description here

in the image example It shows how the long texted node looks like and how it should be if the text is too long and the list width is small for the alignment of the image with text it should show I am long nod... until the list become bigger then show the full text which is I am long node text how can I achieve that

Updated Code

procedure TForm1.virtuailtreeBeforeCellPaint(Sender: TBaseVirtualTree;
      TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
      CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
var
Data: ^PnodeData;
NewRect: TRect;
Textrectplace: TRect;
imgrect : TRect;

begin



if not Assigned(Node) then
begin
exit;
end;

Data := virtuailtree.GetNodeData(Node);

NewRect := CellRect;




//text
begin
Textrectplace := NewRect;
Textrectplace.Left := Textrectplace.Left + 2;
Textrectplace.Width := 70;
Textrectplace.Height := 30;
Textrectplace.Top := Textrectplace.Top;
Textrectplace.Bottom := Textrectplace.Bottom;
TargetCanvas.font.color := clgray;
TargetCanvas.font.Size := 10;
DrawText(TargetCanvas.Handle, pChar(Data.text), Length(Data.text)
, Textrectplace, DT_End_Ellipsis );
end;

end;

//right image that should be stay at the right position 


begin
imgrect := Textrectplace;
imgrect.left := imgrect.left + 150;
imgrect.Width := 24;
imgrect.Height := 36;
imgrect.Top := imgrect.Top - 6 + ((30 - TargetCanvas.TextHeight('H')) div 2);
imgrect.Bottom := imgrect.Bottom;


TargetCanvas.Draw(imgrect.left, imgrect.Top, Data.image);
end;




end;

Solution

  • To shorten the text to fit within a TRect you can use the WinApi DrawText() function, with DT_END_ELLIPSIS format specifier.

    To adjust the space for text when the TVirtualStringTree is resized (e.g. with a TSplitter) simply use:

    TextRectPlace.Right := CellRect - imgRect.width;
    imgRect.Left := TextRectPlace.Right;