27

我想删除 Node.Type 为 5 或 6 的 CheckBoxes。我使用以下代码:

private void TvOne_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
    int type = (e.Node as Node).typ;
    if (type == 5 || type == 6)
    {
        Color backColor, foreColor;
        if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected)
        {
            backColor = SystemColors.Highlight;
            foreColor = SystemColors.HighlightText;
        }
        else if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot)
        {
            backColor = SystemColors.HotTrack;
            foreColor = SystemColors.HighlightText;
        }
        else
        {
            backColor = e.Node.BackColor;
            foreColor = e.Node.ForeColor;
        }
        using (SolidBrush brush = new SolidBrush(backColor))
        {
            e.Graphics.FillRectangle(brush, e.Node.Bounds);
        }
        TextRenderer.DrawText(e.Graphics, e.Node.Text, this.TvOne.Font,
            e.Node.Bounds, foreColor, backColor);

        if ((e.State & TreeNodeStates.Focused) == TreeNodeStates.Focused)
        {
            ControlPaint.DrawFocusRectangle(e.Graphics, e.Node.Bounds,
                foreColor, backColor);
        }
        e.DrawDefault = false;
    }
    else
    {
        e.DrawDefault = true;
    }
}

问题是图像和根节点的线不存在。如何删除复选框并让图像和线条出现?

这是错误的!

4

3 回答 3

74

在您显示的代码中,您自己处理类型为 5 或 6 的所有节点的绘图。对于其余类型,您只是允许系统以默认方式绘制节点。这就是为什么它们都具有预期的线条,但您绘制的所有者却没有:您忘记绘制线条了!你看,当你说它e.DrawDefault = false;假设你真的是认真的。没有完成任何常规绘图,包括标准线。

您要么需要自己绘制这些线条,要么弄清楚如何在没有所有者绘图的情况下过关。

从您现在拥有的代码来看,您似乎正在尝试在您的自绘代码中尽可能多地模拟系统的本机绘图样式,所以我不清楚您首先通过自绘完成了什么. 如果您只是想阻止显示类型 5 和 6 节点的复选框(就像线条一样,根本没有被绘制,因为您没有绘制它们!),有一种更简单的方法可以做到这一点而无需涉及所有者绘画。


那么,您问,隐藏单个节点复选框的更简单方法是什么?好吧,事实证明TreeView控件本身实际上支持这一点,但该功能并未在 .NET Framework 中公开。您需要 P/Invoke 并调用 Windows API 来获取它。将以下代码添加到您的表单类(确保您已添加using声明System.Runtime.InteropServices):

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

顶部所有乱七八糟的东西都是你的 P/Invoke 声明。您需要一些常量、描述树视图项属性的TVITEM结构和SendMessage函数。底部是您实际调用以执行操作的函数 ( HideCheckBox)。您只需传入TreeView控件和TreeNode要从中删除复选标记的特定项目。

因此,您可以删除每个子节点的复选标记,以获得如下所示的内容:

   对子节点隐藏复选标记的 TreeView

于 2011-01-28T09:42:44.303 回答
18

使用 TreeViewExtensions。

使用示例:

private void MyForm_Load(object sender, EventArgs e)
{
     this.treeview1.DrawMode = TreeViewDrawMode.OwnerDrawText;
     this.treeview1.DrawNode += new DrawTreeNodeEventHandler(arbolDependencias_DrawNode);
}

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

这是作为扩展方法的答案代码,您可以使用它:

public static class TreeViewExtensions
{
    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>
    public static void HideCheckBox(this TreeNode node)
    {
        TVITEM tvi = new TVITEM();
        tvi.hItem = node.Handle;
        tvi.mask = TVIF_STATE;
        tvi.stateMask = TVIS_STATEIMAGEMASK;
        tvi.state = 0;
        SendMessage(node.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
    }
}
于 2014-03-06T16:30:00.580 回答
6

这是非常好的!我要做的唯一修改是只将 theTreeNode而不是传递TreeView给该HideCheckBox方法。TreeView可以从TreeNode自身中检索:

TreeView tvw = node.TreeView;
于 2012-09-18T19:06:29.770 回答