Click here to Skip to main content
15,879,326 members
Articles / Desktop Programming / MFC
Article

CGDIRect

Rate me:
Please Sign up or sign in to vote.
4.27/5 (5 votes)
10 Dec 20015 min read 67.1K   215   24   6
A comprehensive class to help working with the different "Rect" classes in Win32 & .NET

Introduction

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.

Enjoy!


Usage

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.

Construction

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:

Assignment

Examples:

	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

Comparison

Examples:

	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 );

Union

Examples:

	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

Examples:

	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

Examples:

	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

Examples:

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:

ConstructionCGDIRect( int nValue = 0 )<BR>CGDIRect( REAL fValue )<BR>CGDIRect( Rect rcInit )<BR>CGDIRect( RectF rcInit )<BR>CGDIRect( CRect rcInit )<BR>CGDIRect( CPoint point, CSize size )<BR>CGDIRect( Point point, Size size )<BR>CGDIRect( PointF point, SizeF size )<BR>CGDIRect( int nLeft, int nTop, int nRight, int nBottom )<BR>CGDIRect( REAL Left, REAL Top, REAL Right, REAL Bottom )
Assignment Operatorsoperator=( CGDIRect& rhs )<BR>operator|=( CGDIRect& rhs )<BR>operator&=( CGDIRect& rhs )<BR>operator=( int nValue )<BR>operator=( REAL nValue )
Addition Operatorsoperator+( CRect& rhs )<BR>operator+( Rect& rhs )<BR>operator+( RectF& rhs )<BR>operator+( CGDIRect& rhs )<BR>operator+=( CRect& rhs )<BR>operator+=( Rect& rhs )<BR>operator+=( RectF& rhs )<BR>operator+=( CGDIRect& rhs )<BR>operator+=( int nValue )<BR>operator+=( REAL fValue )
Subtraction Operatorsoperator-( CRect& rhs )<BR>operator-( Rect& rhs )<BR>operator-( RectF& rhs )<BR>operator-( CGDIRect& rhs )<BR>operator-=( CRect& rhs )<BR>operator-=( Rect& rhs )<BR>operator-=( RectF& rhs )<BR>operator-=( CGDIRect& rhs )<BR>operator-=( int nValue )<BR>operator-=( REAL fValue )
Comparison Operatorsoperator==( REAL Value )<BR>operator==( int nValue )<BR>operator==( CGDIRect& rhs )<BR>operator==( CRect& rhs )<BR>operator==( Rect& rhs )<BR>operator==( RectF& rhs )<BR><BR>operator!=( REAL Value )<BR>operator!=( int nValue )<BR>operator!=( CGDIRect& rhs )<BR>operator!=( CRect& rhs )<BR>operator!=( Rect& rhs )<BR>operator!=( RectF& rhs )
Replicationvoid ReplicateBelow( CGDIRect rcSource, REAL nOffset = 0)<BR>void ReplicateAbove( CGDIRect rcSource, REAL Offset = 0)<BR>void ReplicateLeft( CGDIRect rcSource, REAL Offset = 0)<BR>void ReplicateRight( CGDIRect rcSource, REAL Offset = 0)<BR>void ReplicateBelow( CGDIRect rcSource, int nOffset = 0)<BR>void ReplicateAbove( CGDIRect rcSource, int nOffset = 0)<BR>void ReplicateLeft( CGDIRect rcSource, int nOffset = 0)<BR>void ReplicateRight( CGDIRect rcSource, int nOffset = 0)
Size (Set)
void    SetSize( CGDIRect rcSource )<BR>void    SetSize( CSize size )<BR>void    SetSize( Size size )<BR>void    SetSize( SizeF size )
<P>void    SetWidth( REAL nValue, bool bMaintainRight=false )             <BR>void    SetWidth( int nValue, bool bMaintainRight=false )             <BR>void    SetHeight( REAL nValue, bool bMaintainBottom=false )             <BR>void    SetHeight( int nValue, bool bMaintainBottom=false )<BR><BR>void    InflateWidth(    REAL x )<BR>void    InflateWidthInt( int nX )<BR><BR>void    InflateHeight( REAL y )<BR>void    InflateHeightInt( int nY )<BR><BR>void    Inflate( CSize Size )<BR>void    Inflate( Size Size )<BR>void    Inflate( SizeF Size )<BR>void    Inflate( REAL X, REAL Y)<BR>void    Inflate( REAL Val)<BR>void    InflateInt( int nX, int nY)<BR>void    InflateInt( int nVal)<BR><BR>void    Deflate( CSize Size )<BR>void    Deflate( Size Size )<BR>void    Deflate( SizeF Size )<BR>void    Deflate( REAL X, REAL Y )<BR>void    Deflate( REAL Val)<BR>void    DeflateInt( int nX, int nY )<BR>void    DeflateInt( int nVal)<BR><BR>void    DeflateWidth( REAL X )<BR>void    DeflateWidthInt( int nX )<BR><BR>void    DeflateHeight( REAL Y )<BR>void    DeflateHeightInt( int nY )<BR><BR>void    Extend(    REAL nX, REAL nY )<BR>void    ExtendInt( int nX, int nY )<BR><BR>void    Collapse(    REAL nX, REAL nY )<BR>void    CollapseInt( int nX, int nY )

Size (Get)REAL         Width()<BR>REAL        Height()<BR><BR>int         WidthInt()<BR>int         HeightInt()<BR><BR>operator    CRect()<BR>operator    RectF()<BR>operator    Rect()<BR><BR>operator    CSize()<BR>operator    Size()<BR>operator    SizeF()
Position (Set)void    Offset(    REAL nX, REAL nY )<BR>void    OffsetInt( int nX, int nY )
Position (Get)operator  CPoint()<BR>operator  Point()<BR>operator  PointF()<BR><BR>CPoint    TopLeftCPoint()<BR>Point     TopLeftPoint()<BR>PointF    TopLeftPointF()<BR><BR>CPoint    TopRightCPoint()<BR>Point     TopRightPoint()<BR>PointF    TopRightPointF()<BR><BR>CPoint    BottomRightCPoint()<BR>Point     BottomRightPoint()<BR>PointF    BottomRightPointF()<BR><BR>CPoint    BottomLeftCPoint()<BR>Point     BottomLeftPoint()<BR>PointF    BottomLeftPointF()
Othersbool    HitTest( CPoint point )<BR>bool    HitTest( Point point )<BR>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.

History

11 Dec 2001 - updated source code.

License

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


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalone more on the same subject Pin
c-smile17-Sep-03 20:25
c-smile17-Sep-03 20:25 
GeneralAdd this code please... Pin
vawksel13-Jul-03 18:16
vawksel13-Jul-03 18:16 
GeneralRe: Add this code please... Pin
Jason Troitsky (was Hattingh)12-Oct-03 14:09
Jason Troitsky (was Hattingh)12-Oct-03 14:09 
GeneralThank you so much! Pin
vawksel15-Apr-03 14:27
vawksel15-Apr-03 14:27 
GeneralIt's very Cool.. but.. Pin
Ende18-Nov-02 13:10
Ende18-Nov-02 13:10 
Generalparameter order Pin
luedi20-Mar-03 22:27
luedi20-Mar-03 22: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.