treeView without checkbox

参考文档:

http://dotnetfollower.com/wordpress/2011/05/winforms-treeview-hide-checkbox-of-treenode/

实现效果:

1. 子节点不带checkbox 2. 在子节点前面绘图

实现过程:

1. 继承treeNode

 public class HiddenCheckBoxTreeNode : TreeNode
    {
        private Color _NodeColor;
        public Color NodeColor
        {
            get { return _NodeColor; }
            set { _NodeColor = value; }
            
        }

        private int _Opacity;
        public int Opacity
        {
            get { return _Opacity; }
            set { 
                if (value>=0 && value <= 255)
                {
                    _Opacity = value;
                }
            }

        }
        public HiddenCheckBoxTreeNode() { }
        public HiddenCheckBoxTreeNode(string text) : base(text) { }
        public HiddenCheckBoxTreeNode(string text, Color color, int opacity) : base(text) { this._NodeColor = color; this._Opacity = opacity; }
        public HiddenCheckBoxTreeNode(string text, TreeNode[] children) : base(text, children) { }
        public HiddenCheckBoxTreeNode(string text, int imageIndex, int selectedImageIndex) : base(text, imageIndex, selectedImageIndex) { }
        public HiddenCheckBoxTreeNode(string text, int imageIndex, int selectedImageIndex, TreeNode[] children) : base(text, imageIndex, selectedImageIndex, children) { }
        protected HiddenCheckBoxTreeNode(SerializationInfo serializationInfo, StreamingContext context) : base(serializationInfo, context) { }
    }

2. 

 1 public class MixedCheckBoxesTreeView : TreeView
 2 {
 3     /// <summary>
 4     /// Specifies the attributes of a node
 5     /// </summary>
 6     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
 7     public struct TV_ITEM
 8     {
 9         public int    Mask;
10         public IntPtr ItemHandle;
11         public int    State;
12         public int    StateMask;
13         public IntPtr TextPtr;
14         public int    TextMax;
15         public int    Image;
16         public int    SelectedImage;
17         public int    Children;
18         public IntPtr LParam;
19     }
20  
21     public const int TVIF_STATE          = 0x8;
22     public const int TVIS_STATEIMAGEMASK = 0xF000;
23  
24     public const int TVM_SETITEMA = 0x110d;
25     public const int TVM_SETITEM  = 0x110d;
26     public const int TVM_SETITEMW = 0x113f;
27  
28     public const int TVM_GETITEM  = 0x110C;
29  
30     [DllImport("user32.dll", CharSet = CharSet.Auto)]
31     public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, ref TV_ITEM lParam);
32  
33     protected override void WndProc(ref Message m)
34     {
35         base.WndProc(ref m);
36  
37         // trap TVM_SETITEM message
38         if (m.Msg == TVM_SETITEM || m.Msg == TVM_SETITEMA || m.Msg == TVM_SETITEMW)
39             // check if CheckBoxes are turned on
40             if (CheckBoxes)
41             {
42                 // get information about the node
43                 TV_ITEM tv_item = (TV_ITEM)m.GetLParam(typeof(TV_ITEM));
44                 HideCheckBox(tv_item);
45             }
46     }   
47  
48     protected void HideCheckBox(TV_ITEM tv_item)
49     {
50         if (tv_item.ItemHandle != IntPtr.Zero)
51         {
52             // get TreeNode-object, that corresponds to TV_ITEM-object
53             TreeNode currentTN = TreeNode.FromHandle(this, tv_item.ItemHandle);
54  
55             HiddenCheckBoxTreeNode hiddenCheckBoxTreeNode = currentTN as HiddenCheckBoxTreeNode;
56             // check if it's HiddenCheckBoxTreeNode and
57             // if its checkbox already has been hidden
58  
59             if(hiddenCheckBoxTreeNode != null)
60             {
61                 HandleRef treeHandleRef = new HandleRef(this, Handle);
62  
63                 // check if checkbox already has been hidden
64                 TV_ITEM currentTvItem    = new TV_ITEM();
65                 currentTvItem.ItemHandle = tv_item.ItemHandle;
66                 currentTvItem.StateMask  = TVIS_STATEIMAGEMASK;
67                 currentTvItem.State      = 0;
68  
69                 IntPtr res = SendMessage(treeHandleRef, TVM_GETITEM, 0, ref currentTvItem);
70                 bool needToHide = res.ToInt32() > 0 && currentTvItem.State != 0;
71  
72                 if (needToHide)
73                 {
74                     // specify attributes to update
75                     TV_ITEM updatedTvItem    = new TV_ITEM();
76                     updatedTvItem.ItemHandle = tv_item.ItemHandle;
77                     updatedTvItem.Mask       = TVIF_STATE;
78                     updatedTvItem.StateMask  = TVIS_STATEIMAGEMASK;
79                     updatedTvItem.State      = 0;
80  
81                     // send TVM_SETITEM message
82                     SendMessage(treeHandleRef, TVM_SETITEM, 0, ref updatedTvItem);
83                 }
84             }
85         }
86     }
87  
88     protected override void OnBeforeCheck(TreeViewCancelEventArgs e)
89     {
90         base.OnBeforeCheck(e);
91  
92         // prevent checking/unchecking of HiddenCheckBoxTreeNode,
93         // otherwise, we will have to repeat checkbox hiding
94         if (e.Node is HiddenCheckBoxTreeNode)
95             e.Cancel = true;
96     }
97 }

3. 添加子节点

1  HiddenCheckBoxTreeNode node2Add = new HiddenCheckBoxTreeNode(renderItemTxt, color, opacity);
2  seletedNode.Nodes.Add(node2Add);

4. 对treeview添加绘图事件

 1  toc.DrawMode = TreeViewDrawMode.OwnerDrawText;
 2  toc.DrawNode +=new DrawTreeNodeEventHandler(toc_DrawNode);
 3 
 4 void toc_DrawNode(object sender, DrawTreeNodeEventArgs e)
 5         {
 6             e.DrawDefault = true;
 7             //画颜色
 8             Rectangle rect = e.Bounds;
 9             Rectangle colorRect = new Rectangle(rect.Left - 15, rect.Top, 12, 12);
10             Main.UserCtr.HiddenCheckBoxTreeNode hiddenNode = e.Node as Main.UserCtr.HiddenCheckBoxTreeNode;
11             
12             if (hiddenNode != null && e.Node.Level == 2)
13             {
14                 //Color nodeColor = Common.ColorHelper.SetColorOpacity(hiddenNode.NodeColor, hiddenNode.Opacity);
15                 using (SolidBrush brush = new SolidBrush(hiddenNode.NodeColor))
16                 {
17                     e.Graphics.FillRectangle(brush, colorRect);
18                 }
19             }
20         }

 

posted @ 2014-09-16 09:12  吹小号的程序猿  阅读(251)  评论(0编辑  收藏  举报