GetWindowRect() 得到的是在屏幕坐标系下的RECT;(即以屏幕左上角为原点)
GetClientRect() 得到的是在客户区坐标系下的RECT; (即以所在窗口左上角为原点)
ScreenToClient() 就是把屏幕坐标系下的RECT坐标转换为客户区坐标系下的RECT坐标。
GetWindowRect()取的是整个窗口的矩形。。。
GetClientRect()取的仅是客户区的矩形,也就是说不包括标题栏,外框等。。。
第一个函数获得的是窗口在屏幕上的位置,得到的结果可能是这样CRect(10,10,240,240);
第二个函数和它不同,它只获得了客户区的大小,因此得到的结果总是这样CRect(0,0,width,height);
用法::
// get dialog's and control's rect(这时指ListBox)
CRect wndrc, objrc;
GetClientRect(&wndrc);
::GetWindowRect(wnd, &objrc);
ScreenToClient(&objrc);
//Demo.rc(96): LISTBOX IDC_LB,7,47,247,146,LBS_SORT | LBS_NOINTEGRALHEIGHT
//AddAnchor( IDC_LB, TOP_LEFT, BOTTOM_RIGHT );
void CResizableDialog::AddAnchor(HWND wnd, CSize tl_type, CSize br_type)
{
ASSERT(wnd != NULL && ::IsWindow(wnd));
ASSERT(::IsChild(*this, wnd));
ASSERT(tl_type != NOANCHOR);
// get control's window class
CString st;
GetClassName(wnd, st.GetBufferSetLength(MAX_PATH), MAX_PATH);
st.ReleaseBuffer();
st.MakeUpper();
// add the style 'clipsiblings' to a GroupBox
// to avoid unnecessary repainting of controls inside
if (st == "BUTTON")
{
DWORD style = GetWindowLong(wnd, GWL_STYLE);
if (style & BS_GROUPBOX)
SetWindowLong(wnd, GWL_STYLE, style | WS_CLIPSIBLINGS);
}
// wnd classes that don't redraw client area correctly
// when the hor scroll pos changes due to a resizing
BOOL hscroll = FALSE;
if (st == "LISTBOX")
hscroll = TRUE;
// wnd classes that need refresh when resized
BOOL refresh = FALSE;
if (st == "STATIC")
{
DWORD style = GetWindowLong(wnd, GWL_STYLE);
switch (style & SS_TYPEMASK)
{
case SS_LEFT:
case SS_CENTER:
case SS_RIGHT:
// word-wrapped text needs refresh
refresh = TRUE;
}
// centered images or text need refresh
if (style & SS_CENTERIMAGE)
refresh = TRUE;
// simple text never needs refresh
if (style & SS_TYPEMASK == SS_SIMPLE)
refresh = FALSE;
}
// get dialog's and control's rect
CRect wndrc, objrc;
GetClientRect(&wndrc);
::GetWindowRect(wnd, &objrc);
ScreenToClient(&objrc);
CSize tl_margin, br_margin;
if (br_type == NOANCHOR)
br_type = tl_type;
// calculate margin for the top-left corner
tl_margin.cx = objrc.left - wndrc.Width() * tl_type.cx / 100;
tl_margin.cy = objrc.top - wndrc.Height() * tl_type.cy / 100;
// calculate margin for the bottom-right corner
br_margin.cx = objrc.right - wndrc.Width() * br_type.cx / 100;
br_margin.cy = objrc.bottom - wndrc.Height() * br_type.cy / 100;
// add to the list
Layout obj(wnd, tl_type, tl_margin, br_type, br_margin, hscroll, refresh);
m_arrLayout.Add(obj);
}
阅读(3714) | 评论(0) | 转发(0) |