How do you change the height (or width) of a MFC CRect, without moving the rectangle (top left static) ?
It seems there is no direct setter to the (calculated) height ( int CRect::Height() ).
The current way I find is to recalculate the bottom:
// Create a 40 x 40 rectangle
CRect myRect(10, 10, 50, 50);
// Set the Height to 80
myRect.bottom = myRect.top + 80;
A
RECTin Windows programming is a structure that stores values forleft,top,right, andbottom(conventionally endpoint exclusive). The width and height are derived values (right - leftandbottom - top, respectively).There is no API to change the width (or height) directly since that would be an ambiguous operation: Should a hypothetical
SetWidth()API grow or shrink towards the left? The right? Or maintain the center?MFC's
CRectclass merely encapsulates aRECTand provides some convenience members (such asHeight()) but not much more. If you want to increase the height of aCRecteither add to itsbottomor subtract from itstop. If you want to set the height to a specific value update the value opposite to the one you wish to keep stable (as you're doing in the question already).Calling
NormalizeRect()is a good idea if you cannot otherwise ensure thatleftis less than or equal torightandtopis less than or equal tobottom.