WinForms TreeView:检查Checkbox是否隐藏 [英] WinForms TreeView: check if Checkbox is hidden

查看:253
本文介绍了WinForms TreeView:检查Checkbox是否隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下解决方案在TreeView中显示或隐藏复选框:

I am using the following solution to show or hide checkboxes in a TreeView:

https://stackoverflow.com/a/22230299/1583649

但是,现在我想知道如何检查该节点的复选框是否隐藏.

However, now i would like to know how to check if the checkbox is hidden or not for the node.

例如,我希望能够仅在顶级节点的子级上将其选中时才显示复选框的情况下,将checkbox.checked设置为true(或false).有办法吗?

For example, i want to be able to set checkbox.checked to true (or false) on only the children of a top-level node that have a checkbox visible when the top-level node is checked. Is there a way to do this?

推荐答案

TVM_GETITEMSTATE或TVM_GETITEMSTATE可用于获取状态,然后可用于推断节点上的哪个复选框映像(无,选中或未选中).这是一个扩展类,提供了您需要的两种方法.

TVM_GETITEM or TVM_GETITEMSTATE can be used to get the state, which can then be used to deduce which checkbox image (none, checked or unchecked) is on the node. Here's an extension class that provides the two methods you need.

用法:

// something like this
var treeNode = ...;
if (treeNode.IsCheckBoxVisible())
    treeNode.SetIsCheckBoxVisible(false);
else
    treeNode.SetIsCheckBoxVisible(true);

扩展:

public static class TreeViewExtensions
{
    /// <summary>
    /// Gets a value indicating if the checkbox is visible on the tree node.
    /// </summary>
    /// <param name="node">The tree node.</param>
    /// <returns><value>true</value> if the checkbox is visible on the tree node; otherwise <value>false</value>.</returns>
    public static bool IsCheckBoxVisible(this TreeNode node)
    {
        if (node == null)
            throw new ArgumentNullException("node");
        if (node.TreeView == null)
            throw new InvalidOperationException("The node does not belong to a tree.");
        var tvi = new TVITEM
            {
                hItem = node.Handle,
                mask = TVIF_STATE
            };
        var result = SendMessage(node.TreeView.Handle, TVM_GETITEM, node.Handle, ref tvi);
        if (result == IntPtr.Zero)
            throw new ApplicationException("Error getting TreeNode state.");
        var imageIndex = (tvi.state & TVIS_STATEIMAGEMASK) >> 12;
        return (imageIndex != 0);
    }

    /// <summary>
    /// Sets a value indicating if the checkbox is visible on the tree node.
    /// </summary>
    /// <param name="node">The tree node.</param>
    /// <param name="value"><value>true</value> to make the checkbox visible on the tree node; otherwise <value>false</value>.</param>
    public static void SetIsCheckBoxVisible(this TreeNode node, bool value)
    {
        if (node == null)
            throw new ArgumentNullException("node");
        if (node.TreeView == null)
            throw new InvalidOperationException("The node does not belong to a tree.");
        var tvi = new TVITEM
            {
                hItem = node.Handle,
                mask = TVIF_STATE,
                stateMask = TVIS_STATEIMAGEMASK,
                state = (value ? node.Checked ? 2 : 1 : 0) << 12
            };
        var result = SendMessage(node.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
        if (result == IntPtr.Zero)
            throw new ApplicationException("Error setting TreeNode state.");
    }

    private const int TVIF_STATE = 0x8;
    private const int TVIS_STATEIMAGEMASK = 0xF000;
    private const int TV_FIRST = 0x1100;
    private const int TVM_GETITEM = TV_FIRST + 62;
    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);
}

这篇关于WinForms TreeView:检查Checkbox是否隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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