MFC限制子視窗只在父視窗內移動
阿新 • • 發佈:2019-01-06
5,/******************************************************************
* 函式名稱: OnMoving(UINT nSide, LPRECT lpRect)
* 函式描述: 限制子視窗的移動範圍
********************************************************************/
void CChildFrame::OnMoving( UINT nSide, LPRECT lpRect )
{
CRect rectParent;
GetParent()->GetClientRect(&rectParent);
GetParent()->ClientToScreen(&rectParent);
CRect rectChild;
GetWindowRect(&rectChild);
lpRect->left = min(lpRect->left, rectParent.right - rectChild.Width());
lpRect->left = max(lpRect->left, rectParent.left);
lpRect->top = min(lpRect->top, rectParent.bottom - rectChild.Height());
lpRect->top = max(lpRect->top, rectParent.top);
lpRect->right = min(lpRect->right, rectParent.right);
lpRect->right = max(lpRect->right, rectParent.left + rectChild.Width());
lpRect->bottom = min(lpRect->bottom, rectParent.bottom);
lpRect->bottom = max(lpRect->bottom, rectParent.top + rectChild.Height());
CMDIChildWnd::OnMoving(nSide, lpRect);
}
//////////////
這個是響應子視窗的WM_MOVING訊息
在子視窗的WM_MOVE訊息響應函式中也可以
* 函式名稱: OnMoving(UINT nSide, LPRECT lpRect)
* 函式描述: 限制子視窗的移動範圍
********************************************************************/
void CChildFrame::OnMoving( UINT nSide, LPRECT lpRect )
{
CRect rectParent;
GetParent()->GetClientRect(&rectParent);
GetParent()->ClientToScreen(&rectParent);
CRect rectChild;
GetWindowRect(&rectChild);
lpRect->left = min(lpRect->left, rectParent.right - rectChild.Width());
lpRect->left = max(lpRect->left, rectParent.left);
lpRect->top = min(lpRect->top, rectParent.bottom - rectChild.Height());
lpRect->top = max(lpRect->top, rectParent.top);
lpRect->right = min(lpRect->right, rectParent.right);
lpRect->right = max(lpRect->right, rectParent.left + rectChild.Width());
lpRect->bottom = min(lpRect->bottom, rectParent.bottom);
lpRect->bottom = max(lpRect->bottom, rectParent.top + rectChild.Height());
CMDIChildWnd::OnMoving(nSide, lpRect);
}
//////////////
這個是響應子視窗的WM_MOVING訊息
在子視窗的WM_MOVE訊息響應函式中也可以