简体   繁体   English

更改树视图中子节点的背景色

[英]Changing the backcolor of a childnode in the treeview

As per TreeView Remove CheckBox by some Nodes根据TreeView 删除某些节点的复选框

After doing so I have my tree-view of check-box without parent node check-box .这样做之后,我的check-box tree-view没有parent node check-box But I am facing a problem, I am not able to change the color of a particular child node.但是我面临一个问题,我无法更改特定子节点的颜色。

ie. IE。 if i try to change like如果我尝试改变

treeview1.Nodes[0].Nodes[2].BackColor=Color.Gray;

is still having the same old color.仍然具有相同的旧颜色。 Can anyone help me on this.谁可以帮我这个事。 Thanks.谢谢。

Edited已编辑

private const int TVIF_STATE = 0x8;
    private const int TVIS_STATEIMAGEMASK = 0xF000;
    private const int TV_FIRST = 0x1100;
    private const int TVM_SETITEM = TV_FIRST + 63;

    [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)]
    private struct TVITEM
    {
        public int mask;
        public IntPtr hItem;
        public int state;
        public int stateMask;
        [MarshalAs(UnmanagedType.LPTStr)]
        public string lpszText;
        public int cchTextMax;
        public int iImage;
        public int iSelectedImage;
        public int cChildren;
        public IntPtr lParam;
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam,
                                             ref TVITEM lParam);

    /// <summary> 
    /// Hides the checkbox for the specified node on a TreeView control. 
    /// </summary> 
    private void HideCheckBox(TreeView tvw, TreeNode node)
    {
        TVITEM tvi = new TVITEM();
        tvi.hItem = node.Handle;
        tvi.mask = TVIF_STATE;
        tvi.stateMask = TVIS_STATEIMAGEMASK;
        tvi.state = 0;
        SendMessage(tvw.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
    }

    /// <summary>
    /// Handles the DrawNode event of the treeView1 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.Forms.DrawTreeNodeEventArgs"/> instance containing the event data.</param>
    /// <remarks></remarks>
    private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
    {
        if (e.Node.Level == 0)
            HideCheckBox(e.Node.TreeView, e.Node);
        e.DrawDefault = true;           
    }




    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {          

        treeView1.Nodes[0].Nodes[1].BackColor = Color.Red;
    }

I've tried it the way you did (specially the DrawNode event handler) and I'm pretty sure that you set TreeView.DrawMode = TreeViewDrawMode.OwnerDrawText;我已经按照你的方式尝试过(特别是DrawNode事件处理程序),我很确定你设置了TreeView.DrawMode = TreeViewDrawMode.OwnerDrawText; . . That won't draw the Background (just Text only) so that's why the BackColor is not updated.这不会绘制Background (仅Text ),这就是不更新BackColor的原因。 You have to set it to TreeViewDrawMode.OwnerDrawAll instead:您必须将其设置为TreeViewDrawMode.OwnerDrawAll

I would use another approach to Hide all the Child node checkboxes without using DrawNode event handler.我会使用另一种方法来Hide所有Child node checkboxes而不使用DrawNode事件处理程序。 I would add code to the BeforeExpand like this:我会像这样向BeforeExpand添加代码:

 //BeforeExpand event handler for your TreeView
 private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e){
   foreach (TreeNode node in e.Node.Nodes) 
       HideCheckBox(e.Node.TreeView, e.Node);
 }

You can also loop through all the nodes with level>0 to hide the checkbox once .您还可以遍历level>0所有节点以隐藏复选框一次 Then whenever you add more nodes to your TreeView , if it's not level 0 node , just HideCheckBox right after adding it.然后,每当您向TreeView添加更多节点时,如果它不是level 0 node ,只需在添加后HideCheckBox

NOTE : Of course the 2 approaches I mentioned above don't require you to set DrawMode to anything other than Normal .:当然,2接近我上面提到的要求你设置DrawMode比其他任何Normal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM