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)); } }
/// <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()); }
/// <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); }
/// <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); }
/// <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()); } }
/// <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); }
/// <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); }
public void UnHook() { User32Utility.UnhookWindowsHookEx(hHook); }
public int SetHook() { hProc = new HookProc(MouseHookProc); hHook = User32Utility.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0); return(hHook); }
/// <summary> /// 获取同级别下一个控件 /// </summary> /// <param name="hwnd"></param> /// <returns></returns> public static IntPtr GetOwnerHwnd(IntPtr hwnd) => User32Utility.GetWindow(hwnd, User32Utility.GW_OWNER);
/// <summary> /// 获取同级别下一个控件 /// </summary> /// <param name="hwnd"></param> /// <returns></returns> public static IntPtr GetNextHwnd(IntPtr hwnd) => User32Utility.GetWindow(hwnd, User32Utility.GW_HWNDNEXT);
/// <summary> /// 获取第一个子控件 /// </summary> /// <param name="hwnd"></param> /// <returns></returns> public static IntPtr GetChildHwnd(IntPtr hwnd) => User32Utility.GetWindow(hwnd, User32Utility.GW_CHILD);
/// <summary> /// 获取控件值长度 /// </summary> /// <param name="hwnd"></param> /// <returns></returns> public static int GetHwndWinLen(IntPtr hwnd) => User32Utility.GetWindowTextLength(hwnd);
/// <summary> /// 获取控件值长度 /// </summary> /// <param name="hwnd"></param> /// <returns></returns> public static int GetHwndValueLen(IntPtr hwnd) => User32Utility.SendMessage(hwnd, User32Utility.WM_GETTEXTLENGTH, 0, 0);