1. Computing & Technology

Discuss in my forum

How to Display "Bold" Items in the TTreeView component

By , About.com Guide

TreeView Items with Bold Captions

The TTreeView Delphi component displays a hierarchical list of items, such as the headings in a document.

By default (Windows drawing), all items (tree nodes) of a tree view share the same font styling. If you want to have each item of a tree view to look differently you would need to include some special owner drawing techniques.

If you only want to apply *bold* to some tree items (their Captions), you can use a "special" (defined in the CommCtrl.pas unit) TreeView API.

Here's a simple trick to display some of the tree items in bold:

 uses CommCtrl, ...;
 
 procedure BoldTreeNode(treeNode: TTreeNode; Value: Boolean) ;
 var
    treeItem: TTVItem;
 begin
    if not Assigned(treeNode) then Exit;
 
    with treeItem do
    begin
      hItem := treeNode.ItemId;
      stateMask := TVIS_BOLD;
      mask := TVIF_HANDLE or TVIF_STATE;
      if Value then
        state := TVIS_BOLD
      else
        state := 0;
    end;
 
    TreeView_SetItem(treeNode.Handle, treeItem) ;
 end;
 
 //Usage
    BoldTreeNode(TreeView1.Items[1], True) ;
    BoldTreeNode(TreeView1.Items[6], True) ; 

Delphi tips navigator:
» TListBox with Radio Buttons
« How to Access a TRadioButton from a TRadioGroup - Disable, Change Font, etc.

©2012 About.com. All rights reserved.

A part of The New York Times Company.