Example #1
0
        /// <summary>
        /// 获取控件(包括其非工作区元素)相对于其父控件的大小和位置(以像素为单位)。
        /// </summary>
        /// <param name="hWnd">控件句柄。</param>
        /// <returns>相对于父控件的 System.Drawing.Rectangle,表示控件(包括其非工作区元素)的大小和位置(以像素为单位)。</returns>
        public static Rectangle GetBounds(IntPtr hWnd)
        {
            NativeMethods.RECT lpRect = new NativeMethods.RECT();
            UnsafeNativeMethods.GetWindowRect(hWnd, ref lpRect);

            //父窗口不为空转换坐标
            IntPtr hWndParent = GetParent(hWnd);

            if (hWndParent != IntPtr.Zero)
            {
                UnsafeNativeMethods.MapWindowPoints(NativeMethods.HWND_DESKTOP, hWndParent, ref lpRect, 2);
            }

            return(lpRect.ToRectangle());
        }
Example #2
0
        /// <summary>
        /// 获取该控件的左上角相对于其容器的左上角的坐标。
        /// </summary>
        /// <param name="hWnd">控件句柄。</param>
        /// <returns>System.Drawing.Point,它表示控件的左上角相对于其容器的左上角。</returns>
        public static Point GetLocation(IntPtr hWnd)
        {
            NativeMethods.RECT lpRect = new NativeMethods.RECT();
            UnsafeNativeMethods.GetWindowRect(hWnd, ref lpRect);
            NativeMethods.POINT pt = new NativeMethods.POINT(lpRect.left, lpRect.top);

            //父窗口不为空转换坐标
            IntPtr hWndParent = GetParent(hWnd);

            if (hWndParent != IntPtr.Zero)
            {
                UnsafeNativeMethods.MapWindowPoints(NativeMethods.HWND_DESKTOP, hWndParent, ref pt, 1);
            }

            return(new Point(pt.x, pt.y));
        }
Example #3
0
        /// <summary>
        /// 获取窗口的客户区相对于窗口左上角的矩形.(特别注意:如果有非客户区起点一般不为0,0 如果没有非客户区该值同ClientRectangle相等).该函数非常特别尽量不要调用,在非客户区操作时可能使用到,其余问题请咨询编写人员. by Tim 2013.11.23
        /// </summary>
        /// <param name="hWnd">指定窗口句柄</param>
        /// <returns>客户区相对于窗口坐标系的坐标和大小</returns>
        public static NativeMethods.RECT GetClientRect(IntPtr hWnd)
        {
            NativeMethods.RECT wndRect    = new NativeMethods.RECT();                                 //窗口相对于屏幕的坐标和大小
            NativeMethods.RECT clientRect = new NativeMethods.RECT();                                 //以0,0开始的客户区坐标和大小

            UnsafeNativeMethods.GetWindowRect(hWnd, ref wndRect);                                     //窗口
            UnsafeNativeMethods.GetClientRect(hWnd, ref clientRect);                                  //客户区
            UnsafeNativeMethods.MapWindowPoints(hWnd, NativeMethods.HWND_DESKTOP, ref clientRect, 2); //客户区映射到屏幕

            //偏移
            clientRect.left   -= wndRect.left;
            clientRect.top    -= wndRect.top;
            clientRect.right  -= wndRect.left;
            clientRect.bottom -= wndRect.top;

            //返回
            return(clientRect);
        }
Example #4
0
 /// <summary>
 /// 计算指定工作区矩形的大小和位置(以屏幕坐标表示)。
 /// </summary>
 /// <param name="hWnd">控件句柄。</param>
 /// <param name="r">要转换的工作区坐标 System.Drawing.Rectangle。</param>
 /// <returns>一个 System.Drawing.Rectangle,它表示转换后的 System.Drawing.Rectangle、r(以屏幕坐标表示)。</returns>
 public static Rectangle RectangleToScreen(IntPtr hWnd, Rectangle r)
 {
     NativeMethods.RECT rect = new NativeMethods.RECT(r);
     UnsafeNativeMethods.MapWindowPoints(hWnd, NativeMethods.HWND_DESKTOP, ref rect, 2);
     return(rect.ToRectangle());
 }
Example #5
0
 /// <summary>
 /// 将指定工作区点的位置计算成屏幕坐标。
 /// </summary>
 /// <param name="hWnd">控件句柄。</param>
 /// <param name="p">要转换的工作区坐标 System.Drawing.Point。</param>
 /// <returns>一个 System.Drawing.Point,它表示转换后的 System.Drawing.Point、p(以屏幕坐标表示)。</returns>
 public static Point PointToScreen(IntPtr hWnd, Point p)
 {
     NativeMethods.POINT pt = new NativeMethods.POINT(p.X, p.Y);
     UnsafeNativeMethods.MapWindowPoints(hWnd, NativeMethods.HWND_DESKTOP, ref pt, 1);
     return(new Point(pt.x, pt.y));
 }