CGDIRect






4.27/5 (5 votes)
Dec 4, 2001
5 min read

67710

216
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:
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 ) |
Assignment Operators | operator=( CGDIRect& rhs ) |
Addition Operators | operator+( CRect& rhs ) |
Subtraction Operators | operator-( CRect& rhs ) |
Comparison Operators | operator==( REAL Value ) |
Replication | void ReplicateBelow( CGDIRect rcSource, REAL nOffset = 0) |
Size (Set) | void SetSize( CGDIRect rcSource ) |
Size (Get) | REAL Width() |
Position (Set) | void Offset( REAL nX, REAL nY ) |
Position (Get) | operator CPoint() |
Others | bool HitTest( CPoint 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.