Screen 與Client座標轉換

一直以來不知道這兩個座標轉換有什麼意義,腦子裏相當然的認爲窗口的座標都是以實際屏幕爲相對值的。最近看師兄寫的代碼時,又範迷糊了,於是從MSDN找出了答案

CWnd類裏面有好多涉及到座標的函數,比如

void MoveWindow( int x, int y, int nWidth, int nHeight, BOOL bRepaint = TRUE );

形參裏面的x,y其實有兩層含義,MSDN的解釋是

For a top-level CWnd object, the x and y parameters are relative to the upper-left corner of the screen. For a child CWnd object, they are relative to the upper-left corner of the parent window’s client area.

從這句話可以看出,在程序設計時主窗口一般是以屏幕座標來定的,而子窗口座標又是以主窗口爲相對值定的,這樣一來我們對程序中子窗口的位置就比較容易控制了。當我們想在Screen和Client座標系之間切換時(微軟早就想好了),Windows恰好提供這樣的函數

void ClientToScreen( LPPOINT lpPoint ) const;//point

void ClientToScreen( LPRECT lpRect ) const;//Rectangle

void ScreenToClient( LPPOINT lpPoint ) const;//point

void ScreenToClient( LPRECT lpRect ) const;//Rectangle

這下算是明白了吧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章