コード例 #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());
        }
コード例 #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));
        }
コード例 #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);
        }
コード例 #4
0
 /// <summary>
 /// 获取控件的高度和宽度。
 /// </summary>
 /// <param name="hWnd">控件句柄。</param>
 /// <returns>System.Drawing.Size,表示控件的高度和宽度(以像素为单位)。</returns>
 public static Size GetSize(IntPtr hWnd)
 {
     NativeMethods.RECT lpRect = new NativeMethods.RECT();
     UnsafeNativeMethods.GetWindowRect(hWnd, ref lpRect);
     return(lpRect.Size);
 }