Ejemplo n.º 1
0
        private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

            if (nCode < 0)
            {
                return(User32Utility.CallNextHookEx(hHook, nCode, wParam, lParam));
            }
            else
            {
                if (MouseClickEvent != null)
                {
                    MouseButtons button     = MouseButtons.None;
                    int          clickCount = 0;
                    switch ((Int32)wParam)
                    {
                    case WM_LBUTTONDOWN:
                        button     = MouseButtons.Left;
                        clickCount = 1;
                        break;

                    case WM_RBUTTONDOWN:
                        button     = MouseButtons.Right;
                        clickCount = 1;
                        break;
                    }

                    var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0);
                    MouseClickEvent(this, e);
                }
                this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y);
                return(User32Utility.CallNextHookEx(hHook, nCode, wParam, lParam));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取控件类
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        public static string GetHwndClass(IntPtr hwnd)
        {
            StringBuilder hwndClass = new StringBuilder(255);

            User32Utility.GetClassName(hwnd, hwndClass, hwndClass.Capacity);
            return(hwndClass.ToString());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取桌面句柄
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        public static List <IntPtr> GetDeskHwnds()
        {
            List <IntPtr> result = new List <IntPtr>();

            IntPtr hwnd = User32Utility.FindWindow(null, null);

            while (hwnd != IntPtr.Zero)
            {
                result.Add(hwnd);
                hwnd = GetNextHwnd(hwnd);
            }

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取桌面句柄
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        public static List <IntPtr> GetDeskHwnds2()
        {
            List <IntPtr> result = new List <IntPtr>();

            IntPtr hwnd = User32Utility.GetDesktopWindow();

            hwnd = GetChildHwnd(hwnd);

            while (hwnd != IntPtr.Zero)
            {
                result.Add(hwnd);
                hwnd = GetNextHwnd(hwnd);
            }

            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 获取控件值
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        public static string GetHwndValue(IntPtr hwnd, HwndItemType tp)
        {
            if (tp == HwndItemType.Exe)
            {
                StringBuilder hwndValue = new StringBuilder(GetHwndWinLen(hwnd) + 1);
                User32Utility.GetWindowText(hwnd, hwndValue, hwndValue.Capacity);
                return(hwndValue.ToString());
            }
            else
            {
                StringBuilder hwndValue = new StringBuilder(GetHwndValueLen(hwnd) + 1);

                User32Utility.SendMessage(hwnd, User32Utility.WM_GETTEXT, hwndValue.Capacity, hwndValue);
                return(hwndValue.ToString());
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 获取句柄Model
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        public static HwndModel GetHwndModel(IntPtr hwnd, IntPtr parentHwnd = new IntPtr())
        {
            HwndModel result = new HwndModel()
            {
                HwndId = hwnd
            };

            // 获取类型
            result.Type = GetHwndType(hwnd);

            Console.WriteLine(hwnd);
            // 获取控件值
            result.Value = GetHwndValue(hwnd, result.Type);
            Console.WriteLine(result.Value);
            // 获取控件类
            result.Class = GetHwndClass(hwnd);

            // Desk坐标
            RectModel rect = new RectModel();

            User32Utility.GetWindowRect(hwnd, out rect);
            result.DeskX = (int)rect.Left;
            result.DeskY = (int)rect.Top;

            // Exe坐标
            RectModel parRect = new RectModel();

            if (parentHwnd == IntPtr.Zero)
            {
                parentHwnd = GetTopParentHwnd(hwnd);
            }
            User32Utility.GetWindowRect(parentHwnd, out parRect);
            result.ExeX = (int)(rect.Left - parRect.Left);
            result.ExeY = (int)(rect.Top - parRect.Top);

            // 宽高
            result.Width  = (int)(rect.Right - rect.Left);
            result.Height = (int)(rect.Bottom - rect.Top);

            return(result);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 获取顶级父句柄
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        public static IntPtr GetTopParentHwnd(IntPtr hwnd)
        {
            List <IntPtr> topList = GetDeskHwnds();

            //IntPtr topHwnd = User32Utility.GetDesktopWindow();
            //if (topHwnd == hwnd) return hwnd;

            //IntPtr tempHwnd = User32Utility.GetParent(hwnd);
            //while (topHwnd != tempHwnd && tempHwnd != IntPtr.Zero)
            //{
            //    hwnd = tempHwnd;
            //    tempHwnd = User32Utility.GetParent(hwnd);
            //}

            while (!topList.Contains(hwnd) && hwnd != IntPtr.Zero)
            {
                hwnd = User32Utility.GetParent(hwnd);
            }

            return(hwnd);
        }
Ejemplo n.º 8
0
 public void UnHook()
 {
     User32Utility.UnhookWindowsHookEx(hHook);
 }
Ejemplo n.º 9
0
 public int SetHook()
 {
     hProc = new HookProc(MouseHookProc);
     hHook = User32Utility.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0);
     return(hHook);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// 获取同级别下一个控件
 /// </summary>
 /// <param name="hwnd"></param>
 /// <returns></returns>
 public static IntPtr GetOwnerHwnd(IntPtr hwnd)
 => User32Utility.GetWindow(hwnd, User32Utility.GW_OWNER);
Ejemplo n.º 11
0
 /// <summary>
 /// 获取同级别下一个控件
 /// </summary>
 /// <param name="hwnd"></param>
 /// <returns></returns>
 public static IntPtr GetNextHwnd(IntPtr hwnd)
 => User32Utility.GetWindow(hwnd, User32Utility.GW_HWNDNEXT);
Ejemplo n.º 12
0
 /// <summary>
 /// 获取第一个子控件
 /// </summary>
 /// <param name="hwnd"></param>
 /// <returns></returns>
 public static IntPtr GetChildHwnd(IntPtr hwnd)
 => User32Utility.GetWindow(hwnd, User32Utility.GW_CHILD);
Ejemplo n.º 13
0
 /// <summary>
 /// 获取控件值长度
 /// </summary>
 /// <param name="hwnd"></param>
 /// <returns></returns>
 public static int GetHwndWinLen(IntPtr hwnd)
 => User32Utility.GetWindowTextLength(hwnd);
Ejemplo n.º 14
0
 /// <summary>
 /// 获取控件值长度
 /// </summary>
 /// <param name="hwnd"></param>
 /// <returns></returns>
 public static int GetHwndValueLen(IntPtr hwnd)
 => User32Utility.SendMessage(hwnd, User32Utility.WM_GETTEXTLENGTH, 0, 0);