예제 #1
0
        public static string QueryWindowText(this IntPtr hWnd)
        {
            var sb = new StringBuilder(hWnd.GetWindowTextLength());

            User32Api.SendMessage(hWnd, WindowsMessageApi.WM_GETTEXT, sb.Capacity, sb);

            return(sb.ToString());
        }
예제 #2
0
        public static void ItemClick(this IntPtr treeViewHwnd, IntPtr itemHwnd, Size offSet)
        {
            uint vProcessId;

            User32Api.GetWindowThreadProcessId(treeViewHwnd, out vProcessId);
            var vProcess = Kernel32Api.OpenProcess(
                WindowsMessageApi.PROCESS_ALL_ACCESS,
                false,
                vProcessId
                );

            var remoteBuffer = Kernel32Api.VirtualAllocEx(vProcess,
                                                          IntPtr.Zero,
                                                          (uint)Marshal.SizeOf(typeof(User32Api.RectApi)),
                                                          WindowsMessageApi.MEM_COMMIT,
                                                          WindowsMessageApi.PAGE_EXECUTE_READWRITE);

            try
            {
                var rc = new User32Api.RectApi {
                    left = itemHwnd.ToInt32()
                };
                var localBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(rc));
                Marshal.StructureToPtr(rc, localBuffer, false);

                int vNumberOfBytes;
                Kernel32Api.WriteProcessMemory(vProcess, remoteBuffer, localBuffer,
                                               Marshal.SizeOf(typeof(User32Api.RectApi)), out vNumberOfBytes);
                User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.TVM_SELECTITEM, WindowsMessageApi.TVGN_CARET,
                                      itemHwnd.ToInt32());
                User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.TVM_ENSUREVISIBLE, 0, itemHwnd.ToInt32());
                User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.TVM_GETITEMRECT, 1, remoteBuffer.ToInt32());
                Kernel32Api.ReadProcessMemory(vProcess, remoteBuffer, localBuffer,
                                              Marshal.SizeOf(typeof(User32Api.RectApi)), out vNumberOfBytes);
                rc = (User32Api.RectApi)Marshal.PtrToStructure(localBuffer, typeof(User32Api.RectApi));
                var pt = rc.ToRectangle().Center() + offSet;

                User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.WM_LBUTTONDBLCLK, 0, (int)MAKELPARAM((uint)pt.X, (uint)pt.Y));
            }
            finally
            {
                Kernel32Api.VirtualFreeEx(vProcess, remoteBuffer, (uint)Marshal.SizeOf(typeof(User32Api.RectApi)),
                                          WindowsMessageApi.MEM_FREE);
                Kernel32Api.CloseHandle(vProcess);
            }
        }
예제 #3
0
 //获取根节点
 public static IntPtr GetRootItem(this IntPtr treeViewHwnd)
 {
     return(User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.TVM_GETNEXTITEM,
                                  WindowsMessageApi.TVGN_ROOT, 0));
 }
예제 #4
0
 //获取下个子节点
 public static IntPtr GetNextItem(this IntPtr treeViewHwnd, IntPtr prevItemHwnd)
 {
     return(User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.TVM_GETNEXTITEM,
                                  WindowsMessageApi.TVGN_NEXT, prevItemHwnd.ToInt32()));
 }
예제 #5
0
        public static string GetItemText(this IntPtr treeViewHwnd, IntPtr itemHwnd)
        {
            var result = new StringBuilder(1024);

            uint vProcessId;

            User32Api.GetWindowThreadProcessId(treeViewHwnd, out vProcessId);
            var vProcess = Kernel32Api.OpenProcess(
                WindowsMessageApi.PROCESS_VM_OPERATION |
                WindowsMessageApi.PROCESS_VM_READ |
                WindowsMessageApi.PROCESS_VM_WRITE,
                false,
                vProcessId
                );

            var pStrBufferMemory = Kernel32Api.VirtualAllocEx(vProcess,
                                                              IntPtr.Zero,
                                                              1024,
                                                              WindowsMessageApi.MEM_COMMIT,
                                                              WindowsMessageApi.PAGE_READWRITE);
            var remoteBuffer = Kernel32Api.VirtualAllocEx(vProcess,
                                                          IntPtr.Zero,
                                                          (uint)Marshal.SizeOf(typeof(User32Api.TVITEM)),
                                                          WindowsMessageApi.MEM_COMMIT,
                                                          WindowsMessageApi.PAGE_EXECUTE_READWRITE);

            try
            {
                var tvItem = new User32Api.TVITEM
                {
                    mask       = WindowsMessageApi.TVIF_TEXT,
                    hItem      = itemHwnd,
                    pszText    = pStrBufferMemory,
                    cchTextMax = 1024
                };

                var localBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(tvItem));
                Marshal.StructureToPtr(tvItem, localBuffer, false);

                int vNumberOfBytesWrite;
                Kernel32Api.WriteProcessMemory(
                    vProcess,
                    remoteBuffer,
                    localBuffer,
                    Marshal.SizeOf(typeof(User32Api.TVITEM)),
                    out vNumberOfBytesWrite);

                User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.TVM_GETITEM, 0, remoteBuffer.ToInt32());

                int vNumberOfBytesRead;
                Kernel32Api.ReadProcessMemory(
                    vProcess,
                    pStrBufferMemory,
                    result,
                    1024,
                    out vNumberOfBytesRead
                    );
            }
            finally
            {
                Kernel32Api.VirtualFreeEx(vProcess, pStrBufferMemory, 0, WindowsMessageApi.MEM_RELEASE);
                Kernel32Api.VirtualFreeEx(vProcess, remoteBuffer, 0, WindowsMessageApi.MEM_RELEASE);
                Kernel32Api.CloseHandle(vProcess);
            }

            return(result.ToString());
        }
예제 #6
0
        public static IntPtr SetWindowText(this IntPtr hWnd, string text)
        {
            User32Api.SendMessage(hWnd, WindowsMessageApi.WM_SETTEXT, 0, text);

            return(hWnd);
        }