Role:
You can control the MessageBox () confirmation that the window can only be moved within the scope of the parent window, and cannot be moved out of the parent window.
Note: Always ensure that the child window coordinates are not out of bounds and that the window size is maintained
Method: Overload Message Wm_moving
Limit the window's move range (cannot be implemented in wm_move) void cdeltaskdlg::onmoving (UINT fwside, LPRECT prect) {//TODO: Add Message Handler code here//Get main window coordinates// Methods 1:crect rectparent; GetParent ()->getwindowrect (&rectparent);//method 2://hwnd Hmain = AfxGetMainWnd ()->getsafehwnd ();//:: GetWindowRect (Hmain, &rectparent);//method 3://hwnd Hmain = AfxGetApp ()->getmainwnd ()->getsafehwnd ();//:: GetWindowRect (Hmain, &rectparent); Current window coordinates crect Rectchild; GetWindowRect (&rectchild);p rect->left = max (Prect->left, rectparent.left);p rect->left = min (pRect-> Left, Rectparent.right-rectchild.width ());p rect->top = max (Prect->top, rectparent.top);p rect->top = min ( Prect->top, Rectparent.bottom-rectchild.height ());p rect->right = min (prect->right, rectParent.right); Prect->right = Max (prect->right, Rectparent.left + rectchild.width ());p rect->bottom = min (PRect->bottom, Rectparent.bottom);p rect->bottom = max (Prect->bottom, Rectparent.top + rectchild.height ()); Cdialogex::onmoving (Fwside, pRect);}
Or:
Restrict the window's move range (cannot be implemented in wm_move) void cdeltaskdlg::onmoving (UINT fwside, LPRECT prect) {//TODO: Add Message Handler code here CRect Rectparent; GetParent ()->getwindowrect (&rectparent); CRect Rectchild; GetClientRect (&rectchild);//prect->left = min (Prect->left, Rectparent.right-rectchild.width ());p rect- >left = Max (Prect->left, rectparent.left);p rect->right = Prect->left + rectchild.width ();//pRect->top = Min (Prect->top, Rectparent.bottom-rectchild.height ());p rect->top = max (Prect->top, rectparent.top);p rect- >bottom = Prect->top + rectchild.height ();p rect->right = min (prect->right, rectparent.right);p rect-> left = Prect->right-rectchild.width ();p rect->bottom = min (prect->bottom, rectparent.bottom);p rect->top = Prect->bottom-rectchild.height (); Cdialogex::onmoving (Fwside, prect);}
How to limit the movement range of a form