Exemplo n.º 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());
        }
Exemplo n.º 2
0
 /// <summary>
 /// 获取表示控件的工作区的矩形。
 /// </summary>
 /// <param name="hWnd">控件句柄。</param>
 /// <returns>一个 System.Drawing.Rectangle,它表示控件的工作区。</returns>
 public static Rectangle GetClientRectangle(IntPtr hWnd)
 {
     NativeMethods.RECT lpRect = new NativeMethods.RECT();
     UnsafeNativeMethods.GetClientRect(hWnd, ref lpRect);
     return(lpRect.ToRectangle());
 }
Exemplo n.º 3
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());
 }