1,响应WM_MOVE消息 在里面判断就完了
进消息向导找到它然后双击吧,然后添加你自己的限制代码(获取父窗口的clientRect,就是限制区域)
取得父窗口的顶点和右下角坐标,利用子窗口的顶点坐标和右下角坐标进行范围的判断即可
例如父窗口A(50.50)(100.100)子窗口的顶点坐标应该大于等于X1>=50&&Y1>=50
右下角坐标X2<=100&&Y2<=100
2,应该是WM_MOVING消息中进行处理,
3,如果子窗体非模态可以这样:
if(NULL==m_dlg)
{
CRect rect;
GetClientRect(&rect); //获得客户区的大小
m_dlg=new CDlg(this);
m_dlg-> Create(IDD_REMOTE,NULL);
m_dlg-> ModifyStyle(0,WS_CHILD|WS_MINIMIZEBOX );
m_dlg-> SetWindowPos(NULL,0,30,rect.Width(),rect.Height(),SWP_NOMOVE | SWP_NOZORDER);
m_dlg-> SetWindowPos(this,0,30,0,0, SWP_NOZORDER | SWP_NOSIZE);
m_dlg ->SetParent(this);
m_dlg-> ShowWindow(SW_SHOW);
}
4,SetWindowPlacement
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消息响应函数中也可以
6,不论是WM_SIZING或WM_MOVING,它们的lParam参数都是一个指向RECT结构的
对象的指针,该对象中装有窗口改变尺寸或位置后的左上角和右下角坐标。注意
此时窗口实际上还没有改变到这个位置,所以可以通过改变这个RECT结构的对象
的成员值来限制窗口尺寸和位置的变化方向。
在mfc里,就是在OnSizing和OnMoving函数里来做这个工作。这两个函数的p
Rect参数就是消息里的(RECT*)lParam。
void CMainFrame::OnSizing(UINT fwSide, LPRECT pRect)
{
CFrameWnd::OnSizing(fwSide, pRect);
//限制窗口只能改变右边的尺寸
RECT rect;
GetWindowRect(&rect);
pRect->left=rect.left;
pRect->bottom=rect.bottom;
pRect->top=rect.top;
}
void CMainFrame::OnMoving(UINT fwSide, LPRECT pRect)
{
CFrameWnd::OnMoving(fwSide, pRect);
//限制窗口只能上下移动
RECT rect;
GetWindowRect(&rect);
pRect->left=rect.left;
pRect->right=rect.right;
}