TreeView的一些节点中删除复选框 [英] TreeView Remove CheckBox by some Nodes

查看:272
本文介绍了TreeView的一些节点中删除复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除的CheckBox其中Node.Type为5或6。我用这个code:

I want remove CheckBoxes where the Node.Type is 5 or 6. I use this code:

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

问题是,将图像和线根节点是不存在的。
如何删除的复选框,并让图像和行呢?

The Problem is that then the Image and the Line to the Root Node is not there. How can Remove the Checkbox and let the Image and the Line there?

这是不对的!

推荐答案

在code你已经证明,你是处理自己绘制的所有节点,其类型为5或6。对于剩下的的类型,你只是使系统绘制中的节点的默认方式。这就是为什么他们都如预期的线条,但你的那些老板拉不:你忘了在线条画!你看,当你说 e.DrawDefault = FALSE; 它假定你真的是认真的。正规图纸都没有做,包括标准线。

In the code you've shown, you are handling the drawing yourself for all of the nodes whose type is either 5 or 6. For the rest of the types, you're simply allowing the system to draw the nodes in the default way. That's why they all have the lines as expected, but the ones you're owner-drawing do not: You forgot to draw in the lines! You see, when you say e.DrawDefault = false; it's assumed that you really do mean it. None of the regular drawing is done, including the standard lines.

你要么需要在这些行自己画,或者找出如何没有业主绘制所有度日。

You'll either need to draw in those lines yourself, or figure out how to get by without owner-drawing at all.

从code你现在,它看起来像你试图模拟系统的原生的绘图风格,尽可能在你的所有者绘制code,所以它不是我清楚究竟你由业主拉摆在首位完成。如果你只是想保持复选框从显示出来的5型和6个节点(其中,像线条,只是没有得到绘制的,因为你没有把他们拉!),还有一个更简单的方法来做到这一点,而不涉及业主图。

From the code you have now, it looks like you're trying to simulate the system's native drawing style as much as possible in your owner-draw code, so it's not clear to me what exactly you accomplish by owner-drawing in the first place. If you're just trying to keep checkboxes from showing up for type 5 and 6 nodes (which, like the lines, are simply not getting drawn because you aren't drawing them!), there's a simpler way to do that without involving owner drawing.

所以,你问,那是什么简单的方法来隐藏各个节点的复选框?那么,事实证明,在 TreeView控件控制本身实际上支持此,但功能不是在.NET Framework中暴露出来。你需要的P / Invoke并调用Windows API来得到它。以下code添加到您的表单类(确保你已经添加了 System.Runtime.InteropServices 使用声明$ C>)

So, you ask, what is that simpler way to hide the checkboxes for individual nodes? Well, it turns out that the TreeView control itself actually supports this, but that functionality is not exposed in the .NET Framework. You need to P/Invoke and call the Windows API to get at it. Add the following code to your form class (make sure you've added a using declaration for 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 结构,描述了一个TreeView项的属性,而 SendMessage函数功能。在底部是你居然会打电话给做的契税功能( HideCheckBox )。您只需通过在 TreeView控件控制,并要取消勾选特定的树节点项目。

All of the messy stuff at the top are your P/Invoke declarations. You need a handful of constants, the TVITEM structure that describes the attributes of a treeview item, and the SendMessage function. At the bottom is the function you'll actually call to do the deed (HideCheckBox). You simply pass in the TreeView control and the particular TreeNode item from which you want to remove the checkmark.

所以,你可以从每个子节点取消复选标记得到的东西,看起来像这样:

So you can remove the checkmarks from each of the child nodes to get something that looks like this:

&NBSP;&NBSP;&NBSP;

   

这篇关于TreeView的一些节点中删除复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆