TreeNode图像叠加

时间:2009-03-19 19:22:27

标签: c# treeview treenode

我有一个大约30张图像的图像列表,当TreeNode处于特定状态时,我希望能够覆盖30张图像。我知道C ++ TreeItem可以使用TVIS_OVERLAYMASK这样做:

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

是否有任何机制可以在.NET中实现类似的结果?

3 个答案:

答案 0 :(得分:7)

我看到这个问题仍在获得意见,所以我将发布大卫建议的实施。

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
    }
}

答案 1 :(得分:2)

为什么不直接生成带叠加层的图像,所以你不必浪费宝贵的CPU周期: 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); 
    } 
} 

答案 2 :(得分:0)

我不知道如何自动执行叠加,但您可以使用所有者绘制的树节点执行此操作。