Click here to Skip to main content
11,926,429 members (59,055 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked


, 10 Dec 2001
Rate this:
Please Sign up or sign in to vote.
A comprehensive class to help working with the different "Rect" classes in Win32 & .NET
<!-- Download Links --> <!-- Add the rest of your HTML here -->


CGDIRect was born around mid-day. The mid-wife on duty was a kind and gentle old lady who wasn't in a hurry to deliver what was destined to be a mediator between the obstinate and the uncompliant. It's parents are nowhere to be found and so our little orphan grew up acquiring the skills and abilities of it's peers, while energetically pursuing it's destiny as a facilitator. In fact, after a time in this environment, CGDIRect found that on his own, he is demoralisingly bored. Put him between, beside, in front or even around a CRect, CRect   or Rect, and he'll be "in his element". His peers would naturally refuse to talk to each other on any really productive level, and would sneer at those of a different class... until CGDIRect came along.

Why no parents? He could have been born a descendant of RectF and acquired his skills along the way. This is a matter that could be discussed endlessly, causing division and needless strife. It remains for those who would discriminate on account of CGDIRect's lineage, whether they would choose to enjoy his company and end employ his services... or not. CGDIRect is not easily offended. His work is cut out for him and is successfully employed in institutions around the world, only occasionaly temping at his local McDonalds. He loves a McFlurry, but not before sinking his teeth into a Quarter Pounder with cheese, and doused with a good splash of tabasco - an art learned from Leo Davidson.

Underneath that simple appearance lurks a heart that beats with floating point precision. Depending on the stethoscope used, the heart beat will resemble the precision of an integer, but one peek at the only public variables will reveal that it is an abstraction of something more capable of finer precision as well as wider compatibility.



CGDIRect can be used after the "CGDIRect.h" file is included for the project (e.g. in StdAfx.h) or for each appropriate TLU. It also requires that you have the GDI+ library in your library path.


CGDIRect can be constructed in many different ways, of which just a few are shown below:

CRect 		rcSource;
CGDIRect	rcNew( rcSource );
Rect 		rcSource;
CGDIRect	rcNew( rcSource );
RectF 		rcSource;
CGDIRect	rcNew( rcSource );
CGDIRect	rcNew( CRect( 0,0,10,20 ) );
CGDIRect	rcNew( RectF( 0,0,10,20 ) );
CGDIRect	rcNew( Rect( 0,0,10,20 )  );

Functions and Usage

Following are the overloaded functions and other methods for general use:



	CGDIRect rcNew = rcClient;
	CGDIRect rcNew = CRect( 0, 0, 21, 56 );
	CGDIRect rcNew = Rect( point, size );
	CGDIRect rcNew = RectF( point, size );
	CGDIRect rcNew = 0.0f;			// initialise with single float
	CGDIRect rcNew = 0;			// initialise with single int



	CGDIRect rcA = CRect( 0, 0, 21, 56 );

	ASSERT ( rcA == rcB );

	ASSERT ( rcA == CRect( CPoint(0,0), CSize(10,10) ) );

	ASSERT ( rcA != rcB );

	ASSERT ( rcA != Rect(91, 23, 45, 6) );

	ASSERT ( rcA != RectF(9.1f, 2.3f, 4.5f, 6.0f) );

	ASSERT ( rcA != 12 );



	CGDIRect rcA = CRect( 7, 9, 21, 57 );
	CGDIRect rcB = CRect( 5, 2, 29, 26 );

	// rcA will be the smallest rect that could 
	// encapsulate the previously defined rcA and rcB

	rcA |= rcB;

Make Equal to Intersecting Rectangle


	CGDIRect rcA = CRect( 7, 9, 21, 57 );
	CGDIRect rcB = CRect( 5, 2, 29, 26 );

	// rcA will be the rect that is the area of 
	// intersection of the previously defined rcA and rcB

	rcA &= rcB;

Addition and Subtraction


	CGDIRect rcA	= CRect( 7, 9, 21, 57 );

	rcA		+= CRect(9,2,5,7);

	rcA		+= CGDIRect(20) - RectF(100,20,50,7);

	rcA		-= 5;

Size and Position


CGDIRect rcA	= CRect( 7, 9, 21, 57 );

CPoint ptLoc	= (CPoint)ptLoc; // returns a CPoint that points to the 
                                 // "top left" of the rect
Point ptLoc	= (Point)ptLoc;	 // returns a Point that points to the 
                                 // "top left" of the rect
PointF ptLoc	= (PointF)ptLoc; // returns a PointF that points to the 
                                 // "top left" of the rect

There are all sorts of functions to help get instant access to information that would take just a few more lines to piece together, for example:

scrnshot1.jpg (18622 bytes)

There are also few extensions to the normally obvious functions, which make some everyday tasks just that little bit simpler. For example, when handling rects, I often have to change the width of an existing rectangle by tweaking it's 'left' rather than the 'right'. The SetWidth() function allows you to specify this alteration to the norm. It saves only one or two lines of code which may seem trivial to some, but when done regularly begins to necessitate that the class be able to handle these requests.

List of Member Functions:

So, having given examples of construction and overloads, etc, here is a summary of the member functions in the CGDIRect class:

Construction CGDIRect( int nValue = 0 ) CGDIRect( REAL fValue ) CGDIRect( Rect rcInit ) CGDIRect( RectF rcInit ) CGDIRect( CRect rcInit ) CGDIRect( CPoint point, CSize size ) CGDIRect( Point point, Size size ) CGDIRect( PointF point, SizeF size ) CGDIRect( int nLeft, int nTop, int nRight, int nBottom ) CGDIRect( REAL Left, REAL Top, REAL Right, REAL Bottom )
Assignment Operators operator=( CGDIRect& rhs ) operator|=( CGDIRect& rhs ) operator&=( CGDIRect& rhs ) operator=( int nValue ) operator=( REAL nValue )
Addition Operators operator+( CRect& rhs ) operator+( Rect& rhs ) operator+( RectF& rhs ) operator+( CGDIRect& rhs ) operator+=( CRect& rhs ) operator+=( Rect& rhs ) operator+=( RectF& rhs ) operator+=( CGDIRect& rhs ) operator+=( int nValue ) operator+=( REAL fValue )
Subtraction Operators operator-( CRect& rhs ) operator-( Rect& rhs ) operator-( RectF& rhs ) operator-( CGDIRect& rhs ) operator-=( CRect& rhs ) operator-=( Rect& rhs ) operator-=( RectF& rhs ) operator-=( CGDIRect& rhs ) operator-=( int nValue ) operator-=( REAL fValue )
Comparison Operators operator==( REAL Value ) operator==( int nValue ) operator==( CGDIRect& rhs ) operator==( CRect& rhs ) operator==( Rect& rhs ) operator==( RectF& rhs ) operator!=( REAL Value ) operator!=( int nValue ) operator!=( CGDIRect& rhs ) operator!=( CRect& rhs ) operator!=( Rect& rhs ) operator!=( RectF& rhs )
Replication void ReplicateBelow( CGDIRect rcSource, REAL nOffset = 0) void ReplicateAbove( CGDIRect rcSource, REAL Offset = 0) void ReplicateLeft( CGDIRect rcSource, REAL Offset = 0) void ReplicateRight( CGDIRect rcSource, REAL Offset = 0) void ReplicateBelow( CGDIRect rcSource, int nOffset = 0) void ReplicateAbove( CGDIRect rcSource, int nOffset = 0) void ReplicateLeft( CGDIRect rcSource, int nOffset = 0) void ReplicateRight( CGDIRect rcSource, int nOffset = 0)
Size (Set) void    SetSize( CGDIRect rcSource ) void    SetSize( CSize size ) void    SetSize( Size size ) void    SetSize( SizeF size )

void    SetWidth( REAL nValue, bool bMaintainRight=false )             void    SetWidth( int nValue, bool bMaintainRight=false )             void    SetHeight( REAL nValue, bool bMaintainBottom=false )             void    SetHeight( int nValue, bool bMaintainBottom=false ) void    InflateWidth(    REAL x ) void    InflateWidthInt( int nX ) void    InflateHeight( REAL y ) void    InflateHeightInt( int nY ) void    Inflate( CSize Size ) void    Inflate( Size Size ) void    Inflate( SizeF Size ) void    Inflate( REAL X, REAL Y) void    Inflate( REAL Val) void    InflateInt( int nX, int nY) void    InflateInt( int nVal) void    Deflate( CSize Size ) void    Deflate( Size Size ) void    Deflate( SizeF Size ) void    Deflate( REAL X, REAL Y ) void    Deflate( REAL Val) void    DeflateInt( int nX, int nY ) void    DeflateInt( int nVal) void    DeflateWidth( REAL X ) void    DeflateWidthInt( int nX ) void    DeflateHeight( REAL Y ) void    DeflateHeightInt( int nY ) void    Extend(    REAL nX, REAL nY ) void    ExtendInt( int nX, int nY ) void    Collapse(    REAL nX, REAL nY ) void    CollapseInt( int nX, int nY )

Size (Get) REAL         Width() REAL        Height() int         WidthInt() int         HeightInt() operator    CRect() operator    RectF() operator    Rect() operator    CSize() operator    Size() operator    SizeF()
Position (Set) void    Offset(    REAL nX, REAL nY ) void    OffsetInt( int nX, int nY )
Position (Get) operator  CPoint() operator  Point() operator  PointF() CPoint    TopLeftCPoint() Point     TopLeftPoint() PointF    TopLeftPointF() CPoint    TopRightCPoint() Point     TopRightPoint() PointF    TopRightPointF() CPoint    BottomRightCPoint() Point     BottomRightPoint() PointF    BottomRightPointF() CPoint    BottomLeftCPoint() Point     BottomLeftPoint() PointF    BottomLeftPointF()
Others bool    HitTest( CPoint point ) bool    HitTest( Point point ) bool    HitTest( PointF point )

Private Methods:

There are a few private methods that are used internally and can generally be ignored unless you want to expand on their functionality.


11 Dec 2001 - updated source code.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jason Troitsky
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

Generalone more on the same subject Pin
c-smile17-Sep-03 21:25
memberc-smile17-Sep-03 21:25 
GeneralAdd this code please... Pin
vawksel13-Jul-03 19:16
membervawksel13-Jul-03 19:16 
GeneralRe: Add this code please... Pin
J.G. Hattingh12-Oct-03 15:09
memberJ.G. Hattingh12-Oct-03 15:09 
GeneralThank you so much! Pin
vawksel15-Apr-03 15:27
membervawksel15-Apr-03 15:27 
GeneralIt's very Cool.. but.. Pin
Ende18-Nov-02 14:10
sussEnde18-Nov-02 14:10 
Generalparameter order Pin
luedi20-Mar-03 23:27
memberluedi20-Mar-03 23:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 11 Dec 2001
Article Copyright 2001 by Jason Troitsky
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid