我需要能够禁用WinForms应用程序的TreeView
控件中的一些复选框,但是标准TreeView
控件中没有内置这样的功能。
我已经在使用TreeView.BeforeCheck
事件了,如果节点被禁用,我会取消它,这是非常好的。
我还将禁用节点的ForeColor
更改为GrayText
。
有谁有一个简单而健壮的解决方案吗?
发布于 2009-04-14 16:08:14
因为在C++中有支持,所以我们可以使用p/invoke来解决它。
下面是p/invoke部分的设置,只需将其提供给调用类即可。
// constants used to hide a checkbox
public const int TVIF_STATE = 0x8;
public const int TVIS_STATEIMAGEMASK = 0xF000;
public const int TV_FIRST = 0x1100;
public const int TVM_SETITEM = TV_FIRST + 63;
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
IntPtr lParam);
// struct used to set node properties
public 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;
}
我们想要一个节点一个节点地确定。执行此操作的最简单方法是在绘制节点事件上。为了这个事件,我们必须将我们的树设置为owner drawn,所以一定要将它设置为默认设置之外的其他设置。
this.tree.DrawMode = TreeViewDrawMode.OwnerDrawText;
this.tree.DrawNode += new DrawTreeNodeEventHandler(tree_DrawNode);
在你的tree_DrawNode函数中,确定正在绘制的节点是否应该有一个复选框,并在适当的时候隐藏它。然后将默认的绘制属性设置为true,因为我们不想担心绘制所有其他细节。
void tree_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
if (e.Node.Level == 1)
{
HideCheckBox(e.Node);
e.DrawDefault = true;
}
else
{
e.Graphics.DrawString(e.Node.Text, e.Node.TreeView.Font,
Brushes.Black, e.Node.Bounds.X, e.Node.Bounds.Y);
}
}
最后,对我们定义的函数的实际调用:
private void HideCheckBox(TreeNode node)
{
TVITEM tvi = new TVITEM();
tvi.hItem = node.Handle;
tvi.mask = TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0;
IntPtr lparam = Marshal.AllocHGlobal(Marshal.SizeOf(tvi));
Marshal.StructureToPtr(tvi, lparam, false);
SendMessage(node.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, lparam);
}
发布于 2020-03-30 23:19:57
这是PowerShell版本,非常感谢@sam-trost的救命稻草代码!
P/invoke:
$TypeDefinition = @'
using System;
using System.Runtime.InteropServices;
namespace Win32Functions {
public class Win32TreeView {
// constants used to hide a checkbox
public const int TVIF_STATE = 0x8;
public const int TVIS_STATEIMAGEMASK = 0xF000;
public const int TV_FIRST = 0x1100;
public const int TVM_SETITEM = TV_FIRST + 63;
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
// struct used to set node properties
public 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;
}
}
}
'@
Add-Type -TypeDefinition $TypeDefinition -PassThru
事件处理程序:
$TreeView1_DrawNode = [System.Windows.Forms.DrawTreeNodeEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DrawTreeNodeEventArgs]
if ($null -ne $_.Node) {
# P/invoke hack to hide Node CheckBox
if ($_.Node.Level -eq 0) {
Hide-NodeCheckBox($_.Node)
}
$_.DrawDefault = $true
}
}
TreeView:
$TreeView1.DrawMode = [TreeViewDrawMode]::OwnerDrawText
$TreeView1.add_DrawNode($TreeView1_DrawNode)
功能:
function Hide-NodeCheckBox([TreeNode]$node) {
# P/invoke hack to hide Node CheckBox
if ($node.TreeView.CheckBoxes) {
$tvi = [Win32Functions.Win32TreeView+TVITEM]::new()
$tvi.hItem = $node.Handle
$tvi.mask = [Win32Functions.Win32TreeView]::TVIF_STATE
$tvi.stateMask = [Win32Functions.Win32TreeView]::TVIS_STATEIMAGEMASK
$tvi.state = 0
[IntPtr]$lparam = [Marshal]::AllocHGlobal([Marshal]::SizeOf($tvi))
[Marshal]::StructureToPtr($tvi, $lparam, $false)
[Win32Functions.Win32TreeView]::SendMessage($node.TreeView.Handle, [Win32Functions.Win32TreeView]::TVM_SETITEM, [IntPtr]::Zero, $lparam)
}
}
发布于 2009-03-30 18:15:24
TreeView.BeforeCheck --注册此事件,检查该节点是否是允许选中复选框的节点,如果无法选中,则可以通过在TreeViewCancelEventArgs上设置cancel属性来取消该事件。这应该会阻止用户选中这些框,但不会带来最佳的用户体验。
要删除不可复选项目的复选框,您可以使用owner-draw在复选框上绘制一个实心矩形以将其删除。
https://stackoverflow.com/questions/698369
复制相似问题