本文共 5118 字,大约阅读时间需要 17 分钟。
using System.Text;using System;using System.Runtime.InteropServices;class API{ #region "API函数定义 " [DllImport("user32.dll ", CharSet = CharSet.Unicode)] private static extern int SendMessage(int hWnd, int msg, IntPtr wParam, IntPtr lparam); [DllImport("user32.dll ", CharSet = CharSet.Unicode)] private static extern int SendMessage(int hWnd, int msg, IntPtr wParam, TVITEM lparam); [DllImport("user32.dll ")] public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount); [DllImport("user32.dll ")] public static extern int EnumChildWindows(int hWndParent, MyCallBack lpfn, StringBuilder value); [DllImport("user32.dll ")] public static extern int GetClassNameA(int hwnd, StringBuilder lpClassName, int nMaxCount); [DllImport("kernel32 ", CharSet = CharSet.Unicode)] public static extern int CopyMemory(StringBuilder Destination, IntPtr Source, int Length); [DllImport("kernel32 ", CharSet = CharSet.Unicode)] public static extern int GlobalAlloc(int wFlags, int dwBytes); [DllImport("kernel32 ", CharSet = CharSet.Unicode)] public static extern int GlobalFree(IntPtr hMem); #endregion #region "常量定义 " private const int TVGN_CARET = 0x00000009; private const int TVM_SELECTITEM = 0x0000110b; private const int TVGN_ROOT = 0x00000000; private const int TVM_GETNEXTITEM = 0x0000110a; private const int TVIF_TEXT = 0x0001; private const int TVGN_NEXT = 0x00000001; private const int TVM_GETITEM = 0x0000110c; private const int TVGN_CHILD = 0x00000004; private const int GMEM_FIXED = 0x0000; #endregion ////// 选取TreeView指定项 /// /// 树对象句柄 /// 节点对象句柄 ///成功选中返回true 没找到返回false public static bool SelectNode(IntPtr TreeViewHwnd, IntPtr ItemHwnd) { int result = SendMessage(TreeViewHwnd.ToInt32(), TVM_SELECTITEM, new IntPtr(TVGN_CARET), ItemHwnd); if (result == 0) { return false; } else { return true; } } ////// 获取TreeView根节点 /// /// 树对象句柄 ///成功返回根节点句柄 否则返回 0 public static IntPtr GetRoot(IntPtr TreeViewHwnd) { TVITEM tv = new TVITEM(); IntPtr hStr = Marshal.AllocHGlobal(1024); tv.hItem = TreeViewHwnd; tv.mask = TVIF_TEXT; tv.pszText = hStr; tv.cchTextMax = 1024; int result = SendMessage(TreeViewHwnd.ToInt32(), TVM_GETNEXTITEM, new IntPtr(TVGN_ROOT), tv); Marshal.FreeHGlobal(hStr); return new IntPtr(result); } ////// 获取同级下一节点句柄 /// /// 树对象句柄 /// 上一项节点句柄 ///成功返回下一项节点句柄 否则返回 0 public static IntPtr GetNextItem(IntPtr TreeViewHwnd, IntPtr PrevHwnd) { int mbHwnd = 0; mbHwnd = SendMessage(TreeViewHwnd.ToInt32(), TVM_GETNEXTITEM, new IntPtr(TVGN_NEXT), PrevHwnd); return new IntPtr(mbHwnd); } ////// 获取第一个子节点句柄 /// /// 树对象句柄 /// 父节点句柄 ///成功返回第一个子节点句柄 否则返回 0 public static IntPtr GetFirstChildItem(IntPtr TreeViewHwnd, IntPtr ParentHwnd) { int result = SendMessage(TreeViewHwnd.ToInt32(), TVM_GETNEXTITEM, new IntPtr(TVGN_CHILD), ParentHwnd); return new IntPtr(result); } ////// 获取指定项文本 /// /// 树对象句柄 /// 节点句柄 ///public static string GetItemText(IntPtr TreeViewHwnd, IntPtr ItemHwnd) { StringBuilder sb = new StringBuilder(1024); int hStr = GlobalAlloc(GMEM_FIXED, 1024); if (hStr > 0) { TVITEM tv = new TVITEM(); tv.mask = TVIF_TEXT; tv.HTreeItem = ItemHwnd.ToInt32(); tv.pszText = new IntPtr(hStr); tv.cchTextMax = 1023; int i = SendMessage(TreeViewHwnd.ToInt32(), TVM_GETITEM, IntPtr.Zero, tv); int iCopy = CopyMemory(sb, new IntPtr(hStr), 1024); GlobalFree(new IntPtr(hStr)); } return sb.ToString(); }}/// /// TV_ITEM结构 /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]public class 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 int HTreeItem;}
////// 获取指定项文本 /// /// 树对象句柄 /// 节点句柄 ///public static string GetItemText(IntPtr TreeViewHwnd, IntPtr ItemHwnd){ IntPtr hStr = Marshal.AllocHGlobal(1024); TVITEM tv = new TVITEM(); tv.hItem = ItemHwnd; tv.mask = TVIF_TEXT; tv.pszText = hStr; tv.cchTextMax = 1024; StringBuilder sb = new StringBuilder(1024); int result = SendMessage(TreeViewHwnd.ToInt32(), TVM_GETITEM, new IntPtr(0), tv); int iCopy = CopyMemory(sb, hStr, 1024); Marshal.FreeHGlobal(hStr); return sb.ToString();}本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/09/07/1561808.html,如需转载请自行联系原作者