/// <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); }
/// <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()); }
/// <summary> /// 获取控件的工作区的高度和宽度。 /// </summary> /// <param name="hWnd">控件句柄。</param> /// <returns>一个 System.Drawing.Size,表示控件的工作区的维数。</returns> public static Size GetClientSize(IntPtr hWnd) { NativeMethods.RECT lpRect = new NativeMethods.RECT(); UnsafeNativeMethods.GetClientRect(hWnd, ref lpRect); return(lpRect.Size); }