Custom-drawing of the treeview has been quite buggy; this was fixed recently, hot-tracking is done by the theme-services now, but underlining is still possible when the theme-draw options is removed. Since this required some breaking changes it did not make it into v4.0 (
https://wiki.freepascal.org/Lazarus_5.0_release_notes#TTreeView).
Working with a non-trunk version of Lazarus you can use the following workaround: Turn hot-tracking off and do it yourself. For this purpose you must redraw the tree with every mouse move when the mouse is over a different node. And in the OnCustomDrawItem handler you check whether the currently painted node is the same is the node under the mouse, and in this case you turn on underlining:
TForm1 = class(TForm)
TreeView1: TTreeView;
procedure TreeView1CustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode;
State: TCustomDrawState; var DefaultDraw: Boolean);
procedure TreeView1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
FNodeUnderCursor: TTreeNode;
FPrevNodeUnderCursor: TTreeNode;
public
end;
procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView;
Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
If Node.Count > 0
then Sender.Canvas.Font.Style := [fsBold]
else Sender.Canvas.Font.Style := [];
if Node = FNodeUnderCursor then
Sender.Canvas.Font.Style := Sender.Canvas.Font.Style + [fsUnderline];
end;
procedure TForm1.TreeView1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
FNodeUnderCursor := (Sender as TTreeView).GetNodeAt(X, Y);
if FNodeUnderCursor <> FPrevNodeUnderCursor then
begin
TreeView1.Invalidate;
FPrevNodeUnderCursor := FNodeUnderCursor;
end;
end;
In the trunk version your original code, however, would not work inspite of the customdrawing repair. This is because you set the Canvas.Font.style to [fsBold] or [] - and this removes the underlining state. If you change it as follows it would work in trunk, too:
procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView; Node:
TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
If Node.Count > 0
then Sender.Canvas.Font.Style := Sender.Canvas.Font.Style + [fsBold]
else Sender.Canvas.Font.Style := Sender.Canvas.Font.Style - [fsBold];
end;