TreeView,TVM_DELETEITEM,HTREEITEM,SendMesage - 如何在C#中开发它?

时间:2014-07-16 08:20:57

标签: c# winapi treeview

我需要一些关于这个主题的帮助:

我有一个TreeView句柄,可以说十个项目,我想删除第三个TreeView项目。

但我必须在C#中使用WinApi。

我已达到这一点:

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);

private const int TV_FIRST = 0x1100;
private const int TVM_DELETEITEM = TV_FIRST + 1;

private void xx(...)
{
   /* "treeHwnd" is the hwnd to the TreeView (and not to the TreeView containig window)
       "itemHwnd" thats the problem! See below
   */
   var treeHwnd = ...
   var itemHwnd = 3; // NOTE, that here is the ITEM NUMBER assigned that should be deleted!
   DeleteTreeViewItem((IntPtr) treeHwnd, (IntPtr) itemHwnd);
}

我知道这个非常好的链接: What is a HTREEITEM?

并了解" itemHwnd"不是"项目编号"应删除。 它是一个指向struct的指针......好的......但是如何用C#编写代码?

我的尝试不起作用(SendMessage返回值为0表示错误):

[Serializable]
public struct _TreeItem
{ 
  // EMPTY! because next line of code is commented ...  
  // public IntPtr Number; 
}

private static unsafe bool DeleteTreeViewItem(IntPtr hwnd, IntPtr item)
{
    var rc = new _TreeItem();
    unsafe
    {
      *(IntPtr*)&rc = item;
    }
    var success = SendMessage(hwnd, TVM_DELETEITEM, (IntPtr)0, *(IntPtr*)&item) == 1;
    return success;
}

是的,我知道有些"不安全"是多余的,SendMessage也可以写为

SendMessage(hwnd, TVM_DELETEITEM, (IntPtr)0, *&item)

......如上所述,我在坚持。 :-(任何帮助真的很棒!

提前致谢!!

0 个答案:

没有答案