首页 > 系统相关 >打工笔记----------------------------跨进程控制SysTreeView32树状图控件的问题

打工笔记----------------------------跨进程控制SysTreeView32树状图控件的问题

时间:2023-12-07 15:55:53浏览次数:28  
标签:控件 const IntPtr int ---------------------------- SysTreeView32 uint WindowAPI pu

跨进程控制SysTreeView32树状图控件的问题,啥也不说了,直接上代码:

    public partial class Form1 : Form
    {
        // 定义常量
        public const int WM_LBUTTONDBLCLK = 0x020B; // 左键双击消息

        public const int WM_RBUTTONDOWN = 0x0204; // 右键按下消息
        public const int WM_RBUTTONUP = 0x0205; // 右键松开消息

        public const int TV_FIRST = 0x1100;
        public const int TVM_GETCOUNT = TV_FIRST + 5;
        public const int TVM_GETNEXTITEM = TV_FIRST + 10;
        public const int TVM_GETITEMA = TV_FIRST + 12;
        public const int TVM_GETITEMW = TV_FIRST + 62;
        private const int TVM_GETITEM = 0x0000110c;
        private const int TVM_SELECTITEM = 0x0000110b;
        private const int TVM_GETTEXTCOLOR = 0x0000110b;
        public const int TVM_EXPAND = 0x0131;
        public const int WM_GETTEXT = 0x000D;
        public const int TVGN_ROOT = 0x0000;
        public const int TVGN_NEXT = 0x0001;
        public const int TVGN_PREVIOUS = 0x0002;
        public const int TVGN_PARENT = 0x0003;
        public const int TVGN_CHILD = 0x0004;
        public const int TVGN_FIRSTVISIBLE = 0x0005;
        public const int TVGN_NEXTVISIBLE = 0x0006;
        public const int TVGN_PREVIOUSVISIBLE = 0x0007;
        public const int TVGN_DROPHILITE = 0x0008;
        public const int TVGN_CARET = 0x0009;
        public const int TVGN_LASTVISIBLE = 0x000A;

        public const int TVIF_TEXT = 0x0001;
        public const int TVIF_IMAGE = 0x0002;
        public const int TVIF_PARAM = 0x0004;
        public const int TVIF_STATE = 0x0008;
        public const int TVIF_HANDLE = 0x0010;
        public const int TVIF_SELECTEDIMAGE = 0x0020;
        public const int TVIF_CHILDREN = 0x0040;
        public const int TVIF_INTEGRAL = 0x0080;

        [DllImport("User32.DLL")]
        public static extern int SendMessage(IntPtr hWnd,
            uint Msg, int wParam, int lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam);

        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(uint dwDesiredAccess,
            bool bInheritHandle, uint dwProcessId);

        public const uint MEM_COMMIT = 0x1000;
        public const uint MEM_RELEASE = 0x8000;

        public const uint MEM_RESERVE = 0x2000;
        public const uint PAGE_READWRITE = 4;

        public const uint PROCESS_VM_OPERATION = 0x0008;
        public const uint PROCESS_VM_READ = 0x0010;
        public const uint PROCESS_VM_WRITE = 0x0020;

        [DllImport("kernel32.dll")]
        public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
            uint dwSize, uint flAllocationType, uint flProtect);

        [DllImport("kernel32.dll")]
        public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,
           uint dwSize, uint dwFreeType);

        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr handle);

        [DllImport("kernel32.dll")]
        public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
           IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);

        [DllImport("kernel32.dll")]
        public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
           IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);

        [DllImport("user32.dll")]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd,
            out uint dwProcessId);

        public Form1()
        {
            InitializeComponent();
        }

        private List<string> strings = new List<string>();

        private void button1_Click(object sender, EventArgs e)
        {
            WindowAPI.WindowInfo allDesktopWindowsFirst = WindowAPI.GetWindowsControlFirst("合区工具 1.1", null);
            //启动合区工具
            WindowAPI.WindowInfo ChildWindowsInfo2 =
    WindowAPI.GetChildWindowsControlFirst
    (allDesktopWindowsFirst.intPtr, "添加合区服", "Button");
            WindowAPI.PostMessage(ChildWindowsInfo2.intPtr, WindowAPI.CLICKINT, 0, 0);
            Thread.Sleep(300);
            WindowAPI.PostMessage(ChildWindowsInfo2.intPtr, WindowAPI.CLICKINT, 0, 0);
            Thread.Sleep(300);
            //设置SysTreeView32为D:\系统测试光芒231205SS
            WindowAPI.WindowInfo windowInfoByFuncAndTimeOut = WindowAPI.GetWindowsControlFirst("浏览文件夹", null);
            Thread.Sleep(300);
            WindowAPI.WindowInfo hwndTreeView = WindowAPI.GetChildWindowsControlFirst(windowInfoByFuncAndTimeOut.intPtr, "", "SysTreeView32");
            string selectpath = @"E:\迅雷云盘\Access2003";
            var pathstr = selectpath.Split('\\');
            List<string> AOutput = new List<string>();
            var lists = GetTreeViewText((IntPtr)hwndTreeView.intPtr, ref AOutput);
            //展开所有节点
            foreach (var l in lists)
            {
                int result = WindowAPI.PostMessage(l, WM_LBUTTONDBLCLK, 0, 0);
            }
            //查询所有节点
            lists = GetTreeViewText((IntPtr)hwndTreeView.intPtr, ref AOutput);
            foreach (var l in lists)
            {
            }
            var selnode = lists[40];
            TreeView_SelectNode(hwndTreeView.intPtr, selnode);
            WindowAPI.WindowInfo WindowInfo2 = WindowAPI.GetChildWindowsControlFirst(windowInfoByFuncAndTimeOut.intPtr, "确定", "Button");
            WindowAPI.PostMessage(WindowInfo2.intPtr, WindowAPI.CLICKINT, 0, 0);
            Thread.Sleep(300);
            WindowAPI.PostMessage(WindowInfo2.intPtr, WindowAPI.CLICKINT, 0, 0);
            Thread.Sleep(300);
        }

        public static bool TreeView_SelectNode(IntPtr TreeViewHwnd, IntPtr ItemHwnd)
        {
            int result = SendMessage(TreeViewHwnd, TVM_SELECTITEM, TVGN_CARET, (int)ItemHwnd);
            if (result == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public static uint TreeView_GetCount(IntPtr hwnd)
        {
            return (uint)SendMessage(hwnd, TVM_GETCOUNT, 0, 0);
        }

        public static IntPtr TreeView_GetNextItem(IntPtr hwnd, IntPtr hitem, int code)
        {
            return (IntPtr)SendMessage(hwnd, TVM_GETNEXTITEM, code, (int)hitem);
        }

        public static IntPtr TreeView_GetRoot(IntPtr hwnd)
        {
            return TreeView_GetNextItem(hwnd, IntPtr.Zero, TVGN_ROOT);
        }

        public static IntPtr TreeView_GetChild(IntPtr hwnd, IntPtr hitem)
        {
            return TreeView_GetNextItem(hwnd, hitem, TVGN_CHILD);
        }

        public static IntPtr TreeView_GetParent(IntPtr hwnd, IntPtr hitem)
        {
            return TreeView_GetNextItem(hwnd, hitem, TVGN_PARENT);
        }

        public static IntPtr TreeView_GetNextSibling(IntPtr hwnd, IntPtr hitem)
        {
            return TreeView_GetNextItem(hwnd, hitem, TVGN_NEXT);
        }

        public static IntPtr TreeNodeGetNext(IntPtr AHandle, IntPtr ATreeItem)
        {
            if (AHandle == IntPtr.Zero || ATreeItem == IntPtr.Zero)
                return IntPtr.Zero;
            IntPtr result = TreeView_GetChild(AHandle, ATreeItem);
            if (result == IntPtr.Zero)
                result = TreeView_GetNextSibling(AHandle, ATreeItem);
            else
                return result;
            IntPtr vParentID = ATreeItem;
            while (result == IntPtr.Zero && vParentID != IntPtr.Zero)
            {
                vParentID = TreeView_GetParent(AHandle, vParentID);
                result = TreeView_GetNextSibling(AHandle, vParentID);
            }
            return result;
        }

        public static List<IntPtr> GetTreeViewText(IntPtr AHandle,ref List<string> AOutput)
        {
            List<IntPtr> list = new List<IntPtr>();
            if (AOutput == null) return list;
            uint vProcessId;
            GetWindowThreadProcessId(AHandle, out vProcessId);

            IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ |
                PROCESS_VM_WRITE, false, vProcessId);
            IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 4096,
                MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
            try
            {
                uint vItemCount = TreeView_GetCount(AHandle);
                IntPtr vTreeItem = TreeView_GetRoot(AHandle);
                for (int i = 0; i < vItemCount; i++)
                {
                    list.Add(vTreeItem);
                    byte[] vBuffer = new byte[256];
                    TVITEM[] vItem = new TVITEM[1];
                    vItem[0] = new TVITEM();
                    vItem[0].mask = TVIF_TEXT;
                    vItem[0].hItem = vTreeItem;
                    vItem[0].pszText = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(TVITEM)));
                    vItem[0].cchTextMax = vBuffer.Length;
                    uint vNumberOfBytesRead = 0;
                    WriteProcessMemory(vProcess, vPointer,
                        Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),
                        Marshal.SizeOf(typeof(TVITEM)), ref vNumberOfBytesRead);
                    SendMessage(vTreeItem, TVM_GETITEMA, 0, (int)vPointer);
                    ReadProcessMemory(vProcess,
                        (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(TVITEM))),
                        Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),
                        vBuffer.Length, ref vNumberOfBytesRead);
                    var str = Marshal.PtrToStringAnsi(
                        Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0));

                    vTreeItem = TreeNodeGetNext(AHandle, vTreeItem);
                }
                return list;
            }
            finally
            {
                VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
                CloseHandle(vProcess);
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            WindowAPI.WindowInfo allDesktopWindowsFirst = WindowAPI.GetWindowsControlFirst("合区工具 1.1", null);
            //启动合区工具
            WindowAPI.WindowInfo ChildWindowsInfo2 =
    WindowAPI.GetChildWindowsControlFirst
    (allDesktopWindowsFirst.intPtr, "添加合区服", "Button");
            WindowAPI.PostMessage(ChildWindowsInfo2.intPtr, WindowAPI.CLICKINT, 0, 0);
            Thread.Sleep(300);
            WindowAPI.PostMessage(ChildWindowsInfo2.intPtr, WindowAPI.CLICKINT, 0, 0);
            Thread.Sleep(300);
            //设置SysTreeView32为D:\系统测试光芒231205SS
            WindowAPI.WindowInfo windowInfoByFuncAndTimeOut = WindowAPI.GetWindowsControlFirst("浏览文件夹", null);
            Thread.Sleep(300);
            WindowAPI.WindowInfo hwndTreeView = WindowAPI.GetChildWindowsControlFirst(windowInfoByFuncAndTimeOut.intPtr, "", "SysTreeView32");
            //获取TreeView的左上坐标点
        }
    }

 

标签:控件,const,IntPtr,int,----------------------------,SysTreeView32,uint,WindowAPI,pu
From: https://www.cnblogs.com/misakayoucn/p/17882203.html

相关文章

  • redis集群双活-数据迁移
    一、redis双活1、集群搭建(一主两从)Redis-Sentinel是redis官方推荐的高可用性解决方案,sentinel哨兵是特殊的redis服务,不提供读写服务,主要用来监控redis实例节点,当用redis作master-slave的高可用时,如果master本身宕机,redis本身或者客户端都没有实现主从切换的功能,而redis-sentine......
  • [3分钟]GO:不要错误的使用并发
    七、并发由于Go语言的并发功能,经常被选作项目编程语言。Go语言团队已经竭尽全力以廉价(在硬件资源方面)和高性能来实现并发,但是Go语言的并发功能也可以被用来编写性能不高同时也不太可靠的代码。这里有一个二分法;Go语言的最大特点是简单、轻量级的并发模型。作为一种产品......
  • [效率大杀器] go工具篇:dlv + goland 实现远程调试
    dlv+Goland远程调试远程调试就是使用使用本地IDE来调试远程服务器上的服务。本地打断点,调用远程服务的接口。本地就会停在断点。相当于debug远程服务。优点是可以保持环境的一致性。dlv的安装git仓库:https://github.com/derekparker/delve将div安装到远程服务器。......
  • [代码之美] 简洁代码风格终极指南
    什么是“干净的代码”?原文地址(英文)什么是“干净的代码“可读优雅易于理解,符合单一职责原则易于理解,易于修改,易于维护按照测试策略进行测试写“干净的代码”的重要性解决问题更容易团队中思路的交流更清晰不需要过多的维护TheBestTipstoFollow1.使用描述性......
  • 1234435
    我觉得这道题目如果只有第一个问题的话,排序方式是多种多样的,而且考虑的对象也可以是机器比如我可以给机器按照\(y\)从小到大排序,然后依次考虑每个机器,对于每个机器,在能选择的任务中选择\(x\)最大的即可但这个时候就没有办法保证价值最大了,所以这道题启发我们,如果一道题目有多维......
  • [电脑装机] 内存篇
    [装机|配置升级]内存篇内存也称内存储器和主存储器,它用于暂时存放CPU中的运算数据,与硬盘等外部存储器交换的数据。一、部分内存参数我们一般能在内存条(SDRAM)的标签上的可以看到型号和参数。型号是内存条厂商自己定义的,不同厂商的定义规则不一致,一般可以通过型号识别内存参......
  • [小白入门指南] 如何使用gorm操作MySQL
    1.使用gorm的前置条件1.1MySQL的安装和配置1.2NaviacteForMySQL162.gorm基本操作2.1增2.2查2.3改2.4删2.5gorm迁移1.使用gorm的前置条件1.1MySQL的安装和配置Debian中安装MySQL|MySQL创建用户并授予权限#用rpm查看是否安装了MySQLrpm-qa|gr......
  • 99. 恢复二叉搜索树(中)
    目录题目法一、中序遍历法二、中序遍历+排序题目给你二叉搜索树的根节点root,该树中的恰好两个节点的值被错误地交换。请在不改变其结构的情况下,恢复这棵树。法一、中序遍历classSolution:defrecoverTree(self,root:TreeNode)->None:"""......
  • 可调用对象包装器和绑定器
    文章参考:爱编程的大丙(subingwen.cn)1.可调用对象一组执行任务的语句都可以视作一个函数、一个可调用对象。C++中提供了可调用对象的概念,其应用相当广泛,例如在处理一些回调函数、触发函数时就会用到。可调用对象有如下几种类型:函数指针:intadd(inta,intb){ret......
  • 探索正则可视化工具:让编程更直观、高效
    导语:在当今的编程世界中,正则表达式已成为不可或缺的技能。然而,理解和编写正则表达式往往是一项具有挑战性的任务。为了降低门槛,提高编程效率,正则可视化工具应运而生。一、正则表达式的简介与历史正则表达式(RegularExpression,简称:Regex)是一种强大的文本处理工具,其最早的雏形可......