VirtualStringTree aligned text and gridlines - delphi

I have a virtualstringtree (Gridlines enabled) with X headers and x roots.
I would like to add children (at least 1) to the roots that have only 1 text which is in the middle and goes through all the headers (independent). So no matter if I resize the headers, etc. the text is always aligned to the actual client size of the VirtualStringTree.
Is this possible? If so, how?

This sounds like cell merging. This is discussed here, for example, where the solution is to include toAutoSpanColumns in TreeOptions.AutoOptions.
If you want center-aligned text, override OnDrawText accordingly. Something like:
procedure TForm1.VirtualStringTree1DrawText(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
const Text: WideString; const CellRect: TRect; var DefaultDraw: Boolean);
var
r: TRect;
begin
r := CellRect;
Windows.DrawTextW(TargetCanvas.Handle, PWideChar(Text), Length(Text), r, DT_CENTER or DT_VCENTER);
DefaultDraw := False;
end;

Related

DBAdvGrid RowHeights - alter all rows except 1

I'm using the following code in the CustomCellDraw event of DBAdvGrid(TMS) to increase row height.
procedure TForm1.DBAdvGrid1CustomCellDraw(Sender: TObject; Canvas: TCanvas;
ACol, ARow: Integer; AState: TGridDrawState; ARect: TRect; Printing: Boolean);
begin
DBAdvGrid1.RowHeights[ARow]:=120;
end;
How do I make it avoid increasing row 0, which is the 1st row in the Grid, containing column names/headers? - I'd like that row to remain untouched while all the rest should get resized via the above code. Basically it should ignore row index 0 and start from row index 1
It would be like this:
procedure TForm1.DBAdvGrid1CustomCellDraw(Sender: TObject; Canvas: TCanvas;
ACol, ARow: Integer; AState: TGridDrawState; ARect: TRect; Printing: Boolean);
begin
if ARow > 0 then
DBAdvGrid1.RowHeights[ARow] := 120;
end;
But do not modify row heights from a drawing event. Such event is triggered frequently, and is used exclusively for content painting, not for adjusting content size. What's worse, if you e.g. allowed row sizing and the user would try to setup row height, it would in turn trigger that event where you would change the height back, so you'd be fighting with the user.
Content sizing should be done earlier, as this example shows in the OnCustomCellSize event.
But for your aim I think it's enough to set DefaultRowHeight and FixedRowHeight properties with no additional code.

How to make a proper alternate row color on a filtered TVirtualStringTree

Previously I used this VirtualStringTree for showing all of the nodes, and I used the Node.Index to check the odd and even rows inside the OnBeforeCellPaint event.
But when I filtered the nodes, I realized that the Node.Index is irrelevant to be used as alternate rows as shown in the screenshot below:
Any idea/solution to solve this?
procedure TMainForm.IpTreeBeforeCellPaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
begin
if Node.Index mod 2 = 0
then TargetCanvas.Brush.Color := $00F7E6D5
else TargetCanvas.Brush.Color := $00FBF2EA;
if Sender = ipTree then
if IpAddresses[ PVirtualNode( Node ).Index ].Highlighted then
TargetCanvas.Brush.Color := clYellow;
TargetCanvas.FillRect( CellRect );
end;
Heres my code, this doesnt take into account child nodes. It alternates color for each row. However, if you did have children, you can always use
iLevel := Sender.GetNodeLevel( Node );
then if its an even number, paint all the child nodes the same as parent.

How to underline or highlight a part of node caption

I want to implement a search function in my virtualtreeview. And I want to highlight or underline the searched word in the nodes.
How can I do this?
Thank you
I would write a handler for the OnDrawText event because it's the only event (at this time) where you'll get passed the node text, the rectangle where that text is about to be rendered as well as the canvas prepared for such rendering. There are more proper events for both tasks (like OnBeforeCellPaint, or OnAfterItemErase for text background highlighting, and OnAfterCellPaint or OnAfterItemPaint for text underlining), just none of them provide text rendering specific parameters as the OnDrawText one.
If your nodes won't be multiline and you don't care about text alignment, reading orientation, nor string shortening, then your task might be as easy as one of the following examples.
1. Matching text background color
procedure TForm1.VirtualTreeDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
Node: PVirtualNode; Column: TColumnIndex; const Text: string; const CellRect: TRect;
var DefaultDraw: Boolean);
var
BackMode: Integer;
begin
// if the just rendered node's Text starts with the text written in a TEdit control
// called Edit, then...
if StartsText(Edit.Text, Text) then
begin
// store the current background mode; we need to use Windows API here because the
// VT internally uses it (so the TCanvas object gets out of sync with the DC)
BackMode := GetBkMode(TargetCanvas.Handle);
// setup the color and draw the rectangle in a width of the matching text
TargetCanvas.Brush.Color := clYellow;
TargetCanvas.FillRect(Rect(
CellRect.Left,
CellRect.Top + 1,
CellRect.Left + TargetCanvas.TextWidth(Copy(Text, 1, Length(Edit.Text))),
CellRect.Bottom - 1)
);
// restore the original background mode (as it likely was modified by setting the
// brush color)
SetBkMode(TargetCanvas.Handle, BackMode);
end;
end;
An example visual output:
2. Matching text underline
procedure TForm1.VirtualTreeDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
Node: PVirtualNode; Column: TColumnIndex; const Text: string; const CellRect: TRect;
var DefaultDraw: Boolean);
begin
// if the just rendered node's Text starts with the text written in a TEdit control
// called Edit, then...
if StartsText(Edit.Text, Text) then
begin
TargetCanvas.Pen.Color := clRed;
TargetCanvas.MoveTo(CellRect.Left, CellRect.Bottom - 2);
TargetCanvas.LineTo(
CellRect.Left + TargetCanvas.TextWidth(Copy(Text, 1, Length(Edit.Text))),
CellRect.Bottom - 2
);
end;
end;
And an example visual output:
In real code I'd suggest pre-calculating those highlight shapes and in the OnDrawText event only draw, but optimization I would leave on you; the main point is the event itself, I think.
Little modification. Pay attention to if.
var
BackMode: integer;
begin
inherited;
// if the just rendered node's Text starts with the text written in a TEdit control
// called Edit, then...
if StartsText(Sender.SearchBuffer, Text) and (Node = Sender.FocusedNode) then
begin
TargetCanvas.Pen.Color := clRed;
TargetCanvas.MoveTo(CellRect.Left, CellRect.Bottom - 2);
TargetCanvas.LineTo(
CellRect.Left + TargetCanvas.TextWidth(Copy(Text, 1, Length(Sender.SearchBuffer))),
CellRect.Bottom - 2
);
end;
end;

Change Starting point of data in Column in delphi

I am using Developer Express components - TdxDBgrid as Grid and TdxMemData as dataset.
There are around 10 columns displayed in grid.
For the second column, I am trying to change starting point where column value displayed. I am trying to do it using ACanvas.TextRect. But the changes are not getting affected. Anybody having idea how to change starting position of data in Column for any grid.
thanks
I'm not sure if what you're asking is how to shift the x-position where the text starts, but if it is, try something like this:
procedure TForm1.dxDBGrid1Column2CustomDrawCell(Sender: TObject;
ACanvas: TCanvas; ARect: TRect; ANode: TdxTreeListNode;
AColumn: TdxTreeListColumn; ASelected, AFocused, ANewItemRow: Boolean;
var AText: String; var AColor: TColor; AFont: TFont;
var AAlignment: TAlignment; var ADone: Boolean);
var
XOffset : Integer;
begin
XOffset := 20;
ACanvas.FillRect(ARect);
ACanvas.TextOut(ARect.Left + XOffset, ARect.Top, AText);
ADone := True;
end;
Obviously that doesn't deal with details like how to draw selected and focused columns, etc, but you should get the idea and you can look at the DevEx source for those.

VirtualStringTree - Multiline Nodes and centre text vertically

If a node in a VirtualStringTree is multiline (vsMultiline in Node.States) then how can i centre the text vertically for all columns (except the multiline column) in that node?
I have tried using the OnBeforeCellPaint (using TargetCanvas.TextOut()) but this does not paint the text at all. By default, the text for a multiline node is always painted at the top of the node.
(For non-multiline nodes the text is painted vertically centred).
Try it using DrawText(..)
you can add text alignment on it such as left, right, top, middle etc.
use the Cellrect for the Rect.
in your case i think it workable on OnDrawtext, set the DefaultText := False;
Thanks to XBasic3000, i was able to come up with this solution, which covers almost every possible combination:
procedure TForm1.TreeDrawText(
Sender: TBaseVirtualTree; TargetCanvas: TCanvas; Node: PVirtualNode;
Column: TColumnIndex; const Text: WideString; const CellRect: TRect;
var DefaultDraw: Boolean);
var DrawFormat : Cardinal;
R : TRect;
s : WideString;
NodeWidth,EllipsisWidth : Integer;
Size: TSize;
begin
if not (Column in [yourmultilinecolumns]) then
begin
DefaultDraw := False;
R := CellRect;
GetTextExtentPoint32W(TargetCanvas.Handle, PWideChar(Text), Length(Text), Size);
NodeWidth := Size.cx + 2 * Tree.TextMargin;
GetTextExtentPoint32W(TargetCanvas.Handle, '...', 3, Size);
EllipsisWidth := Size.cx;
if ((NodeWidth - 2 * Tree.TextMargin) > R.Right - R.Left) then
s := EllipseString(TargetCanvas.Handle, Text, R.Right - R.Left, EllipsisWidth)
else s := Text;
DrawFormat := DT_NOPREFIX or DT_VCENTER or DT_SINGLELINE;
Windows.DrawTextW(TargetCanvas.Handle, PWideChar(s), Length(s), R, DrawFormat);
end;
end;
The EllipseString() method is very similar to VirtualTrees.ShortenString() in VirtualTrees.pas.
The only isue is the inability to draw multiline text on other columns. You must specify the multilinecolumns set, so there is no capability to draw multiline and vertically centred.

Resources