<p>In a C++ app, I have an hWnd pointing to a window running in a third party process. This window contains controls which extend the COM TreeView control. I am interested in obtaining the CheckState of this control.<br>
I use the hWnd to get an HTREEITEM using TreeView_GetRoot(hwnd) from commctrl.h</p>

<p>hwnd points to the window and hItem is return value from TreeView_GetRoot(hwnd). They are used as follows:</p>

<pre><code>int iCheckState = TreeView_GetCheckState(hwnd,  hItem);
switch (iCheckState)
{
   case 0:
      // (unchecked)
   case 1:
      // checked
   ...
}
</code></pre>

<p>I'm looking to port this code into a C# app which does the same thing (switches off the CheckState of the TreeView control). I have never used COM and am quite unfamiliar.</p>

<p>I have tried using the .NET mscomctl but can't find equivalent methods to TreeView_GetRoot or TreeView_GetCheckState. I'm totally stuck and don't know how to recreate this code in C# :(</p>

<p>Suggestions?</p>

[{'answer_id': 78229, 'author': 'Mike Dimmick', 'author_id': 6970, 'author_profile': 'https://Stackoverflow.com/users/6970', 'pm_score': 1, 'selected': False, 'text': "<p>Why are you not using a Windows Forms TreeView control? If you are using this control, set the control's CheckBoxes property to true to enable check boxes, and set the Checked property on the nodes you want to display checked.</p>\n\n<p>To get the collection of root nodes, use the TreeView's Nodes property. This returns a TreeNodeCollection which you can then index or add items to.</p>\n"}, {'answer_id': 79794, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 3, 'selected': True, 'text': '<p>We have these definitions from CommCtrl.h:</p>\n\n<pre><code>#define TreeView_SetItemState(hwndTV, hti, data, _mask) \\\n{ TVITEM _ms_TVi;\\\n  _ms_TVi.mask = TVIF_STATE; \\\n  _ms_TVi.hItem = (hti); \\\n  _ms_TVi.stateMask = (_mask);\\\n  _ms_TVi.state = (data);\\\n  SNDMSG((hwndTV), TVM_SETITEM, 0, (LPARAM)(TV_ITEM *)&amp;_ms_TVi);\\\n}\n\n#define TreeView_SetCheckState(hwndTV, hti, fCheck) \\\n  TreeView_SetItemState(hwndTV, hti, INDEXTOSTATEIMAGEMASK((fCheck)?2:1), TVIS_STATEIMAGEMASK)\n</code></pre>\n\n<p>We can translate this to C# using PInvoke. First, we implement these macros as functions, and then add whatever\nother support is needed to make those functions work. Here is my first shot at it. You should double check my\ncode especially when it comes to the marshalling of the struct. Further, you may want to Post the message cross-thread\ninstead of calling SendMessage.</p>\n\n<p>Lastly, I am not sure if this will work at all since I believe that the common\ncontrols use WM_USER+ messages. When these messages are sent cross-process, the data parameter\'s addresses\nare unmodified and the resulting process may cause an Access Violation. This would be a problem in whatever\nlanguage you use (C++ or C#), so perhaps I am wrong here (you say you have a working C++ program).</p>\n\n<pre><code>static class Interop {\n\npublic static IntPtr TreeView_SetCheckState(HandleRef hwndTV, IntPtr hti, bool fCheck) {\n    return TreeView_SetItemState(hwndTV, hti, INDEXTOSTATEIMAGEMASK((fCheck) ? 2 : 1), (uint)TVIS.TVIS_STATEIMAGEMASK);\n}\n\npublic static IntPtr TreeView_SetItemState(HandleRef hwndTV, IntPtr hti, uint data, uint _mask) {\n    TVITEM _ms_TVi = new TVITEM();\n    _ms_TVi.mask = (uint)TVIF.TVIF_STATE;\n    _ms_TVi.hItem = (hti);\n    _ms_TVi.stateMask = (_mask);\n    _ms_TVi.state = (data);\n    IntPtr p = Marshal.AllocCoTaskMem(Marshal.SizeOf(_ms_TVi));\n    Marshal.StructureToPtr(_ms_TVi, p, false);\n    IntPtr r = SendMessage(hwndTV, (int)TVM.TVM_SETITEMW, IntPtr.Zero, p);\n    Marshal.FreeCoTaskMem(p);\n    return r;\n}\n\nprivate static uint INDEXTOSTATEIMAGEMASK(int i) { return ((uint)(i) &lt;&lt; 12); }\n\n[DllImport("user32.dll", CharSet = CharSet.Auto)]\nprivate static extern IntPtr SendMessage(HandleRef hWnd, int msg, IntPtr wParam, IntPtr lParam);\n\nprivate enum TVIF : uint {\n    TVIF_STATE = 0x0008\n}\n\nprivate enum TVIS : uint {\n    TVIS_STATEIMAGEMASK = 0xF000\n}\n\nprivate enum TVM : int {\n    TV_FIRST = 0x1100,\n    TVM_SETITEMA = (TV_FIRST + 13),\n    TVM_SETITEMW = (TV_FIRST + 63)\n}\n\nprivate struct TVITEM {\n    public uint mask;\n    public IntPtr hItem;\n    public uint state;\n    public uint stateMask;\n    public IntPtr pszText;\n    public int cchTextMax;\n    public int iImage;\n    public int iSelectedImage;\n    public int cChildren;\n    public IntPtr lParam;\n}\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/78161', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/165305/']