简体   繁体   English

TreeView复选框无法正确显示

[英]TreeView checkboxes not displaying correctly

I need some help with a TreeView with checkboxes because after hiding the checkbox for the parent nodes and leaving them only for the child nodes they appear cut through. 我需要一些带有复选框的TreeView帮助,因为在隐藏父节点的复选框并将其仅保留给子节点的复选框之后,它们会切穿。 The treeview looks like this: 树形视图如下所示: 在此处输入图片说明

The code I am using to hide the checkbox for the parent nodes is this: 我用来隐藏父节点复选框的代码是这样的:

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

void sharedFolders_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
    if (e.Node.Level == 1 || e.Node.Level == 0)
        HideCheckBox(sharedFolders, e.Node);
    e.DrawDefault = true;
}

Can anyone help me display the full checkbox? 谁能帮我显示完整的复选框?

尝试将TreeView DrawMode更改为OwnerDrawAll

sharedFolders.DrawMode = TreeViewDrawMode.OwnerDrawAll;

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

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