Click here to Skip to main content
Licence CPOL
First Posted 18 Jan 2008
Views 10,442
Downloads 56
Bookmarked 9 times

Bounded rectangles

By | 18 Jan 2008 | Article
A very brief discussion on how to restrict a window's position to a bounding rectangle.

Introduction

For most applications, dragging a window around on the screen should be mostly uninhibited. There are, however, a few scenarios where you may want to restrict the window to some bounding rectangle. In this article, I'll lay out all of the steps necessary (both of them!) to accomplish this.

Step 1

The first thing we need to do is figure out the initial size of our window so when we go to adjust the window's position in step 2, we don't accidently change its size in the process. So, in the dialog's OnInitDialog() method, simply make a call to GetWindowRect() like:

SetOwner(CWnd::GetDesktopWindow());

CRect rc;
GetWindowRect(rc);
m_nWidth = rc.Width(); // member variables of the dialog
m_nHeight = rc.Height();

Step 2

The second (and final) step is to create a handler function for the WM_MOVING message. In that function, we first need to get the position of the parent (bounding) rectangle. Then, we adjust the (child) dialog's four sides by ensuring they fall within each side of the parent. It looks sort of "mathy," but it's really straightforward.

CRect rcParent;
GetOwner()->GetWindowRect(rcParent);

// keep the child's left edge inside the parent's right edge
pRect->left = min(pRect->left, rcParent.right - m_nWidth);
// keep the child's left edge inside the parent's left edge
pRect->left = max(pRect->left, rcParent.left);

// keep the child's top edge inside the parent's bottom edge    
pRect->top = min(pRect->top, rcParent.bottom - m_nHeight);
// keep the child's top edge inside the parent's top edge
pRect->top = max(pRect->top, rcParent.top);

// keep the child's right edge inside the parent's right edge
pRect->right = min(pRect->right, rcParent.right);
// keep the child's right edge inside the parent's left edge
pRect->right = max(pRect->right, rcParent.left + m_nWidth);

// keep the child's bottom edge inside the parent's bottom edge
pRect->bottom = min(pRect->bottom, rcParent.bottom);
// keep the child's bottom edge inside the parent's top edge
pRect->bottom = max(pRect->bottom, rcParent.top + m_nHeight);

That's all there is to it.

Summary

While my example used a modal dialog owned by the desktop, this same code can be employed in situations such as an About box owned by its application, or a frame within an MDI application. The only difference lies in the parent/owner of the window being restricted.

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

DavidCrow

Software Developer (Senior)
Pinnacle Business Systems
United States United States

Member


The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.
 
HTTP 404 - File not found
Internet Information Services


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 18 Jan 2008
Article Copyright 2008 by DavidCrow
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid