Exemplo n.º 1
0
        public static Rectangle GetWindowRect(this IntPtr hWnd)
        {
            var rect = new User32Api.RectApi();

            User32Api.GetWindowRect(hWnd, ref rect);
            return(rect.ToRectangle());
        }
Exemplo n.º 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);
            }
        }