欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# TreeView树节点实现部分节点左侧显示CheckBox

程序员文章站 2022-06-08 09:15:58
...

#region 隐藏CheckBoxs

    public void treeViewGroupStatements_DrawNode(object sender, DrawTreeNodeEventArgs e)
    {
        HideLevelOfTreeView(e.Node);
        e.DrawDefault = true;
    }
    public void HideLevelOfTreeView(TreeNode tn)
    {
        //TreeNode parNode = tn.Parent;
        //if (parNode != null && parNode.Text == "全部项目")
        //{
        //    HideCheckBox(tn.TreeView, tn);
        //}
        ProjectFolderDTO prjFolderDTO = tn.Tag as ProjectFolderDTO;
        if (prjFolderDTO != null)
        {
            if (!GlobalData.NewPaperSets.Contains(prjFolderDTO.NodeType.ToString()))
            {
                HideCheckBox(tn.TreeView, tn);
            }
        }
        ProjectInfoDTO prjInfoDTO = tn.Tag as ProjectInfoDTO;
        if (prjInfoDTO != null)
        {
            HideCheckBox(tn.TreeView, tn);
        }
    }

    //#endregion
    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;
    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);

    }

    [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);
    #endregion