Exemplo n.º 1
0
        /// <summary>
        /// 获取某个窗口中,某个位置的控件上的文本
        /// </summary>
        /// <param name="hWnd">要获取的窗口的句柄</param>
        /// <param name="x">要获取文本的控件的x坐标</param>
        /// <param name="y">要获取文本的控件的y坐标</param>
        /// <returns>获取到的文本</returns>
        public static string GetWindowText(IntPtr hWnd, int x, int y)
        {
            Win32API.POINT point = new Win32API.POINT()
            {
                x = x,
                y = y
            };

            IntPtr handle = Win32API.ChildWindowFromPoint(hWnd, point);

            if (handle == IntPtr.Zero)
            {
                logger.ErrorFormat("ChildWindowFromPoint失败, {0}", Marshal.GetLastWin32Error());
                return(null);
            }

            int size = Win32API.SendMessage(handle, Win32API.WM_GETTEXTLENGTH, 0, 0);

            if (size == 0)
            {
                return(string.Empty);
            }

            char[] title = new char[size];
            SendMessage(handle, Win32API.WM_GETTEXT, size, Marshal.UnsafeAddrOfPinnedArrayElement(title, 0));
            return(new string(title));
        }