博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#下用API跨进程控制别的窗体的TreeView
阅读量:6171 次
发布时间:2019-06-21

本文共 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,如需转载请自行联系原作者
 
你可能感兴趣的文章
jstree
查看>>
linux进程状态浅析
查看>>
linux网络编程之一-----多播(组播)编程
查看>>
CRM 2013 相关下载 / 2013-10-11
查看>>
【转】Python函数默认参数陷阱
查看>>
Linux光纖卡配置,磁盤掛載,多路徑設置
查看>>
ROS探索总结(四)——简单的机器人仿真
查看>>
datatable 添加同一类型的datatable,字典添加数据
查看>>
(转)libvirt 部分API 介绍
查看>>
在WIN10下搭建Robot Framework环境
查看>>
船舶行业钢板堆垛管理特色和创新管理思路
查看>>
C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
查看>>
《当程序员的那些狗日日子》(四十一)卑微的角色
查看>>
SQL Server 查看identity值的几种方法。
查看>>
auto uninstaller (autodesk 修复大师) 简体中文版 更新下载地址
查看>>
MySQL 先按某字段分组,再取每组中前N条记录
查看>>
Windows 2008 r2 AD域自定义属性
查看>>
Eclipse - 循环cin的输出怎样终止
查看>>
shell文本过滤编程(十一):paste命令
查看>>
关于spring项目报错:找不到元素 'beans' 的声明的解决办法
查看>>