How to limit the movement range of a form

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.