5
我使用了一個三級節點的樹狀圖,第二級我沒有設置複選框使用this question中的代碼。TreeNode.Handle沒有返回使用Treeview
它非常好,除了它從來沒有遇到非常第一個複選框去掉了複選框:
我打電話這段代碼是所有的數據都具有後HideCheckBox()方法被加載到TreeView控件:
foreach (TreeNode appNode in trvPermissions.Nodes)
{
foreach (TreeNode secNode in appNode.Nodes)
{
HideCheckBox(trvPermissions, secNode);
}
}
當我通過代碼似乎node.Handle它遇到的第一個節點返回0,這是不是調用代碼時取出使用有效的句柄查框。奇怪的是,如果我調用方法刪除複選框兩次,然後句柄正確返回。
任何人都可以建議爲什麼TreeNode.Handle不會返回正確的值嗎?
編輯:
下面的代碼的要求 -
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);
}
你可以在你刪除複選框並調用node.Handle的地方顯示你的代碼嗎? –
當您調用HideCheckBox()時TreeView是否可見?我的意思是TreeView.Visible設置爲true? –