3

I have an imagelist of about 30 images, and 3 images I'd like to be able to overlay on top of the 30 when a TreeNode is in a particular state. I know that a C++ TreeItem can do this with the TVIS_OVERLAYMASK as such:

SetItemState(hItem,INDEXTOOVERLAYMASK(nOverlayIndex), TVIS_OVERLAYMASK);

Is there any mechanism to achieve similar results in .NET?

Sam Trost
  • 2,173
  • 20
  • 26

3 Answers3

7

I see this question is still getting views, so I'll post the implementation of what David suggested.

internal class MyTree : TreeView
{
    internal MyTree() :
        base()
    {
        // let the tree know that we're going to be doing some owner drawing
        this.DrawMode = TreeViewDrawMode.OwnerDrawText;
        this.DrawNode += new DrawTreeNodeEventHandler(MyTree_DrawNode);
    }

    void MyTree_DrawNode(object sender, DrawTreeNodeEventArgs e)
    {
        // Do your own logic to determine what overlay image you want to use
        Image overlayImage = GetOverlayImage();

        // you have to move the X value left a bit, 
        // otherwise it will draw over your node text
        // I'm also adjusting to move the overlay down a bit
        e.Graphics.DrawImage(overlayImage,
            e.Node.Bounds.X - 15, e.Node.Bounds.Y + 4);

        // We're done! Draw the rest of the node normally
        e.DefaultDraw = true
    }
}
Sam Trost
  • 2,173
  • 20
  • 26
  • In my .NET 2.0 app the overlays are on top of the treeview icon. Are you using a newer version of the framework? – Sam Trost May 01 '12 at 00:57
  • my bad: I had DrawMode to be OwnerDrawAll not OwnerDrawText seems to work well now - thanks – Adam Butler May 01 '12 at 01:35
  • It's a shame, after a little more testing I find I experience weird black highlighting like described here: http://stackoverflow.com/q/1003459/417721 - we are .net 3.5 – Adam Butler May 01 '12 at 01:59
2

Why don't you just generate the image with the overlay on demand even, so you don't have to waste precious CPU cycles like this: http://madprops.org/blog/highlighting-treenodes-with-an-overlay-image/ :

private void InitializeLinkedTreeImages() 
{ 
    foreach (string key in treeImages.Images.Keys) 
    { 
        Bitmap bmp = new Bitmap(treeImages.Images[key]); 
        Graphics g = Graphics.FromImage(bmp); 
        g.DrawImageUnscaled(Properties.Resources.Linked16, 0, 0); 
        treeImages.Images.Add(key + "Linked", bmp); 
    } 
} 
McX
  • 1,296
  • 2
  • 12
  • 16
DaFlame
  • 131
  • 1
  • 2
0

I don't know of a way to do the overlay automatically, but you could do this with an owner drawn tree node.

David
  • 34,223
  • 3
  • 62
  • 80