GetWindowRect與GetClientRect 的區別

來源:互聯網
上載者:User

GetWindowRect
  函數功能:該函數返回指定視窗的邊框矩形的尺寸。該尺寸以相對於螢幕座標左上方的螢幕座標給出。
  函數原型:BOOL GetWindowRect(HWND hWnd,LPRECTlpRect);
  在Visual Studio 2005中,函數原型為void GetWindowRect(LPRECT lpRect) const;
  是屬於CWnd類的函數.
  參數:
  hWnd:視窗控制代碼。
  lpRect:指向一個RECT結構的指標,該結構接收視窗的左上方和右下角的螢幕座標。
  傳回值:如果函數成功,傳回值為非零:如果函數失敗,傳回值為零。若想獲得更多錯誤資訊,請調用GetLastError函數。
  速查:Windows NT:3.1以上版本:Windows:95以上版本;Windows CE:1.0以上版本;標頭檔:Winuser.h;庫檔案:User32.lib。
  
先調用GetWindowRect後再調用ScreenToClient,這個時候得到的rect和直接使用GetClientRect得到的值是相等的。

有時候需要獲得視窗矩形的大小和客戶區矩形的大小二者的值,故需要分別調用GetWindowRect和GetClientRect。

如果只需要獲得客戶區矩形的大小,調用GetClientRect就行了。

GetWindowRect和GetClientRect函數的說明如下:

CWnd::GetClientRect 
    void GetClientRect( LPRECT lpRect ) const;
Parameters:
lpRect
    Points to a RECT structure or a CRect object to receive the client coordinates. The left and top members will be 0. The right and bottom members will contain the width and height of the window.
Remarks:
    Copies the client coordinates of the CWnd client area into the structure pointed to by lpRect. The client coordinates specify the upper-left and lower-right corners of the client area. Since client coordinates are relative to the upper-left corners of the CWnd client area, the coordinates of the upper-left corner are (0,0).

CWnd::GetWindowRect
void GetWindowRect( LPRECT lpRect ) const;
Parameters:
lpRect
Points to a CRect object or a RECT structure that will receive the screen coordinates of the upper-left and lower-right corners.
Remarks:
Copies the dimensions of the bounding rectangle of the CWnd object to the structure pointed to by lpRect. The dimensions are given in screen coordinates relative to the upper-left corner of the display screen. The dimensions of the caption, border, and scroll bars, if present, are included.

GetWindowRect() 得到的是在螢幕座標系下的RECT;(即以螢幕左上方為原點)
GetClientRect() 得到的是在客戶區座標系下的RECT; (即以所在視窗左上方為原點)

GetWindowRect()取的是整個視窗的矩形;
GetClientRect()取的僅是客戶區的矩形,也就是說不包括標題列,外框等;

第一個函數獲得的是視窗在螢幕上的位置,得到的結果可能是這樣CRect(10,10,240,240);
第二個函數和它不同,它只獲得了客戶區的大小,因此得到的結果總是這樣CRect(0,0,width,height);

ScreenToClient() 就是把螢幕座標系下的RECT座標轉換為客戶區座標系下的RECT座標。

The GetClientRect function retrieves the coordinates of a window's client area. The client coordinates specify the upper-left and lower-right corners of the client area. Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).

GetClientRect得到的是客戶區的大小,也就是說這樣得到的左上方永遠是(0,0)

The GetWindowRect function retrieves the dimensions of the bounding rectangle of the specified window. The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen.

GetWindowRect 是視窗相對於整個螢幕的座標,螢幕左上點為0,0

相互轉化用ScreenToClient 或者 ClientToScreen

ClientToScreen
The ClientToScreen function converts the client coordinates of a specified point to screen coordinates.
BOOL ClientToScreen(
   HWND hWnd,        // window handle for source coordinates
   LPPOINT lpPoint   // pointer to structure containing screen coordinates
);
Parameters
hWnd
Handle to the window whose client area is used for the conversion.
lpPoint
Pointer to a POINT structure that contains the client coordinates to be converted. The new screen coordinates are copied into this structure if the function succeeds.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.

雖然存在調用GetWindowRect後再調用ScreenToClient==GetClientRect,但ScreenToClient()和ClientToScreen()兩者都是屬於WINDOWS API函數,可能是存在一定的冗餘設計,但意義不同。
不過在.Net Framework下對WINDOWS API函數進行了重新整理和最佳化,在擷取控制項或視窗的螢幕座標和客戶區座標時更方便的多,只需要得到與控制項或視窗相對應螢幕座標和客戶區座標屬性值就可以了。

ScreenToClient
The ScreenToClient function converts the screen coordinates of a specified point on the screen to client coordinates.
BOOL ScreenToClient(
   HWND hWnd,         // window handle for source coordinates
   LPPOINT lpPoint    // address of structure containing coordinates
);
Parameters:
hWnd
Handle to the window whose client area will be used for the conversion.
lpPoint
Pointer to a POINT structure that contains the screen coordinates to be converted.
Return Values:
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.