How To Make the Selected TreeNode Bold without Owner Drawing

Basic code demonstrating how to make the selected TreeNode Bold without owner drawing

The following code uses the TVIS_BOLD constant to make the selected node bold.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
    procedure TreeView1Changing(Sender: TObject; Node: TTreeNode;
      var AllowChange: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Commctrl;

{$R *.DFM}

procedure SetNodeBold(Node: TTreeNode; MakeBold: Boolean);


var
  Item: TTVItem;
  Template: Integer;
begin
  if Assigned(Node) then
  begin
    if MakeBold then
      Template := -1
    else
      Template := 0;
    Item.Mask := TVIF_STATE;
    Item.hItem := Node.ItemId;
    Item.StateMask := TVIS_BOLD;
    Item.State := Item.StateMask and Template;
    TreeView_SetItem(Node.TreeView.Handle, Item);
  end;
end;

procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
  SetNodeBold(Node, True);
end;

procedure TForm1.TreeView1Changing(Sender: TObject; Node: TTreeNode;
  var AllowChange: Boolean);
begin
  SetNodeBold(TreeView1.Selected, False)
end;

end.

 

Share this article!

Follow us!

Find more helpful articles: