Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: [Solved] VirtualDrawTree question-Child displayed in wrong location  (Read 2077 times)

bobonwhidbey

• Hero Member
• Posts: 502
[Solved] VirtualDrawTree question-Child displayed in wrong location
« on: June 16, 2021, 08:52:50 pm »
I haven't been able to find a good example using the TVirtualDrawTree so I'm stumbling in the dark. This simple example I've put together has a record with only the one Caption:string. Here's how I'm adding root and child records...seems to be working:
Code: Pascal  [Select][+][-]
2. var
3.   Data: PMyRec;
4.   Node: PVirtualNode;
5. begin
6.   with VDT do
7.     case (Sender as TButton).Tag of
8.       0:
9.       begin // add to root
11.         Data := VDT.GetNodeData(Node);
12.         Data^.Caption := 'Root ' + IntToStr(RootNodeCount);
13.       end;
14.       1: if Assigned(FocusedNode) then  // add as child
15.         begin
16.           ChildCount[FocusedNode] := ChildCount[FocusedNode] + 1;
17.           Expanded[FocusedNode] := True;
18.           InvalidateToBottom(FocusedNode);
20.           Data := VDT.GetNodeData(Node);
21.           Data^.Caption := 'Child ' + IntToStr(RootNodeCount);
22. //          Data^.Caption := 'Child ' + IntToStr(Node.Index);
23.         end;
24.     end;
25. end;
26.

When I display the tree with this, you can see, in the attachment, that the tree indentions seem to be correct but the text is showing up at the bottom, like a main root record rather than a child record.

Code: Pascal  [Select][+][-]
1. procedure TForm.VDTDrawNode(Sender: TBaseVirtualTree; const PaintInfo: TVTPaintInfo);
2. var
3.   Data: PMyRec;
4.   R: TRect;
5.   I, w: integer;
6.   s, s1: string;
7. begin
8.   with Sender as TVirtualDrawTree, PaintInfo do
9.   begin
10.     Data := VDT.GetNodeData(Node);
11.     SetBKMode(Canvas.Handle, TRANSPARENT);
12.     R := ContentRect;
13.     if vsSelected in Node^.States then
14.       Canvas.Font.Color := clWhite
15.     else
16.       Canvas.Font.Color := clBlack;
17.     s := Data^.Caption;
18.     DrawText(Canvas.Handle, PChar(s), Length(s), R, DT_LEFT or
19.       DT_BOTTOM or DT_SINGLELINE);
20.   end;
21. end;
22.

Can someone provide help or a good VDT example?
« Last Edit: June 18, 2021, 07:53:15 pm by bobonwhidbey »
Win10 64-bit / Lazarus 32-bit 2.0.12 / FPC 3.2.0

jamie

• Hero Member
• Posts: 4740
Re: VirtualDrawTree question-Child displayed in wrong location
« Reply #1 on: June 17, 2021, 02:30:45 am »
Try the CELLRECT instead for the text out...
The only true wisdom is knowing you know nothing

bobonwhidbey

• Hero Member
• Posts: 502
Re: VirtualDrawTree question-Child displayed in wrong location
« Reply #2 on: June 17, 2021, 02:36:32 am »
Same problem   Attached shows the results after adding 3 at the root, then selecting the middle line and adding a child.
Code: Pascal  [Select][+][-]
1.   with Sender as TVirtualDrawTree, PaintInfo do
2.   begin
3.     Data := VDT.GetNodeData(Node);
4.     s := Data^.Caption;
5.     SetBKMode(Canvas.Handle, TRANSPARENT);
6.     if vsSelected in Node^.States then
7.       Canvas.Font.Color := clWhite
8.     else
9.       Canvas.Font.Color := clBlack;
10.     R := CellRect;
11.     DrawText(Canvas.Handle, PChar(s), Length(s), R, DT_LEFT or
12.       DT_BOTTOM or DT_SINGLELINE);
13.   end;
14.
« Last Edit: June 17, 2021, 02:38:07 am by bobonwhidbey »
Win10 64-bit / Lazarus 32-bit 2.0.12 / FPC 3.2.0

jamie

• Hero Member
• Posts: 4740
Re: VirtualDrawTree question-Child displayed in wrong location
« Reply #3 on: June 17, 2021, 02:54:13 am »
you add a child node but you don't give it a parent node so its just going to keep showing in the root path..

on the second case step, use the focused node for example as the parent node instead of nil
The only true wisdom is knowing you know nothing