How to obtain the window handle of a TreeNode with v1.1.4322

  • Thread starter Thread starter SwissPinoy
  • Start date Start date
As far as i can tell, the TreeNode class in the CF does not inherit
from control, meaning it's not a window and won't have it's own window
handle...
 
The TreeNode handle isn't a window handle but an identifier for the node
within a specific treeview control. You can possibly read this via
reflection - the internal field m_htvi (IntPtr) within the TreeNode class.
However many of the handles within .NETCF are psuedohandles and aren't those
directly used by the native control, you'd need to do some testing.

Peter
 
It does appear as if m_htvi would be the correct value and I can get
the value of m_htvi using reflection, but it doesn't seem to work for
what I'm trying to do using the code below. Basically, I'm just trying
to hide a node checkbox.


[DllImport("coredll", CharSet=CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg,
IntPtr wParam, ref TVITEM lParam);

[DllImport("coredll.dll")]
private static extern IntPtr GetCapture();

public int TVIF_STATE = 8;
public int TVIS_STATEIMAGEMASK = 61440;
public int TV_FIRST = 4352;
public int TVM_SETITEM = 4415;

[StructLayout(LayoutKind.Sequential)] public struct TVITEM
{
public int mask;
public IntPtr hItem;
public int state;
public int stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public IntPtr lParam;
}

public void HideCheckBox(System.Windows.Forms.TreeNode node)
{
//Gets the node handle
System.Reflection.FieldInfo nodeInfo =
node.GetType().GetField("m_htvi", BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Public);

IntPtr nodeHandle = (System.IntPtr)nodeInfo.GetValue(node);

//Gets the treeview handle
node.TreeView.Capture = true;
IntPtr treeHandle = GetCapture();
node.TreeView.Capture = false;

//Sets the desired node state
TVITEM tvi = new TVITEM();
tvi.hItem = nodeHandle;
tvi.mask = TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0 << 12;

SendMessage(treeHandle, TVM_SETITEM, IntPtr.Zero, ref tvi);
}
 
Back
Top