Make Individual TreeView Nodes Bold

The full framework TreeView control supports setting a Font on a per-node basis, the Compact Framework control doesn’t support this, however with a little interop magic you can mark individual nodes as Bold. Because the .NETCF v2.0 TreeView control exposes it’s own window handle, and each TreeNode also exposes it’s handle, we have all the raw data we need to format the node. First we need to define a structure and a couple of enumerations:-

internal struct TVITEM
public TVIF mask;
public IntPtr hItem;
public TVIS state;
public TVIS stateMask;
string pszText;
int cchTextMax;
int iImage;
int iSelectedImage;
int cChildren;
int lParam;

internal enum TVIF
TEXT =0x0001,
IMAGE =0x0002,
PARAM =0x0004,
STATE =0x0008,
HANDLE =0x0010,
CHILDREN =0x0040,

internal enum TVIS
SELECTED =0x0002,
CUT =0x0004,
BOLD =0x0010,
EXPANDED =0x0020,



Then I’ve created a single static method which can set the bold state of any node:-

public static void SetNodeEmphasis(TreeNode n, bool bold)
//get the control and node handles
IntPtr hTreeview = n.TreeView.Handle;
IntPtr hNode = n.Handle;

//create a TVITEM struct
TVITEM t = new TVITEM();
t.hItem = hNode;
//mark only the handle and state members as valid
//set the state to bold if bold param was true
t.state = bold ? TVIS.BOLD : 0;
//set statemask to show we want to set the bold state
t.stateMask = TVIS.BOLD;

//pin the struct in memory
GCHandle hTVITEM = GCHandle.Alloc(t, GCHandleType.Pinned);
//create a TVM_SETITEM message with the handle of our pinned struct
Microsoft.WindowsCE.Forms.Message m = Microsoft.WindowsCE.Forms.Message.Create(hTreeview, 0x113F, IntPtr.Zero, hTVITEM.AddrOfPinnedObject());
//send the message to the treeview
Microsoft.WindowsCE.Forms.MessageWindow.SendMessage(ref m);
//free the pinned structure

The method is easy to reuse as you only need to pass the TreeNode in, it will grab the handle of the parent control from the node itself. After applying the formatting to a couple of nodes you’ll get something like this:-


By Peter Freeman Foot

Microsoft Windows Development MVP

One reply on “Make Individual TreeView Nodes Bold”

Comments are closed.