65.9K
CodeProject 正在变化。 阅读更多。
Home

CGDIRect

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.27/5 (5投票s)

2001年12月4日

5分钟阅读

viewsIcon

68250

downloadIcon

216

一个全面的类,用于处理 Win32 和 .NET 中不同的“Rect”类

引言

CGDIRect 在中午时诞生。当班的助产士是一位善良而温和的老太太,她不急于分娩,因为注定要成为固执者和不合作者之间的调解员。它的父母不知去向,所以我们的小孤儿在成长过程中掌握了同龄人的技能和能力,同时充满活力地追求其作为促进者的命运。事实上,在这样的环境中度过一段时间后,CGDIRect 发现自己独自一人时,会感到令人沮丧的无聊。把它放在 CRectCRect 或 Rect 之间、旁边、前面甚至周围,它都会“如鱼得水”。它的同类自然会拒绝在任何真正富有成效的层面上进行交流,并会鄙视不同阶层的人……直到 CGDIRect 的出现。

为什么没有父母?它可以是 RectF 的后代,并在途中获得了它的技能。这个问题可以无休止地讨论,造成分裂和不必要的冲突。对于那些因为 CGDIRect 的血统而歧视它的人来说,他们是选择享受它的陪伴并雇用它的服务……还是不选择,这仍然有待决定。CGDIRect 不容易冒犯。它的工作已经安排好了,并且在世界各地的机构中得到了成功运用,只是偶尔会在当地的麦当劳兼职。它喜欢麦旋风,但在品尝巨无霸芝士汉堡并淋上大量塔巴斯科酱之前,这是一个从 Leo Davidson 那里学到的技巧。

在它简单的外观之下,隐藏着一颗以浮点精度跳动的心。根据所使用的听诊器,心跳会类似于整数的精度,但只需看一眼唯一公开的变量,就会发现它是一个能够实现更精细精度和更广泛兼容性的抽象。

尽情享用!


用法

可以在项目中包含“CGDIRect.h”文件后(例如在 StdAfx.h 中)或为每个适当的 TLU 使用 CGDIRect。它还要求您在库路径中包含 GDI+ 库。

然后,当你开始迭代 2(这是构建迭代的开始)时,你可能想要复制测试用例并将它们重新分类到迭代 2。这还允许对测试用例进行粒度跟踪,并允许你说某个测试用例在一个迭代中是准备好的,但在另一个迭代中不是。同样,如何做到这一点取决于你以及你希望如何报告。 “场景”部分提供了更多细节。

CGDIRect 可以通过多种方式构造,下面仅展示其中几种。

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

函数和用法

以下是用于通用用途的重载函数和其他方法。

赋值

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

比较

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;

设为等于相交矩形

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;

加法和减法

Examples:

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

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

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

	rcA		-= 5;

大小和位置

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

有各种各样的函数可以帮助即时访问需要花费更多行代码才能组合起来的信息,例如:

scrnshot1.jpg (18622 bytes)

此外,还有一些对通常显而易见函数的扩展,这使得一些日常任务变得更加简单。例如,在处理矩形时,我经常需要通过调整现有矩形的“左”边而不是“右”边来更改其宽度。SetWidth() 函数允许您指定这种对常规的修改。它只节省了一两行代码,这可能对某些人来说微不足道,但当经常这样做时,就会开始需要类能够处理这些请求。

成员函数列表

因此,在给出了构造和重载等的示例之后,以下是 CGDIRect 类中成员函数的摘要:

然后,当你开始迭代 2(这是构建迭代的开始)时,你可能想要复制测试用例并将它们重新分类到迭代 2。这还允许对测试用例进行粒度跟踪,并允许你说某个测试用例在一个迭代中是准备好的,但在另一个迭代中不是。同样,如何做到这一点取决于你以及你希望如何报告。 “场景”部分提供了更多细节。 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 )
赋值运算符 operator=( CGDIRect& rhs )
operator|=( CGDIRect& rhs )
operator&=( CGDIRect& rhs )
operator=( int nValue )
operator=( REAL nValue )
加法运算符 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 )
减法运算符 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 )
比较运算符 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 )
复制 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)
大小(设置) 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 )

大小(获取) REAL         Width()
REAL        Height()

int         WidthInt()
int         HeightInt()

operator    CRect()
operator    RectF()
operator    Rect()

operator    CSize()
operator    Size()
operator    SizeF()

位置(设置) void    Offset(    REAL nX, REAL nY )
void    OffsetInt( int nX, int nY )
位置(获取) 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()
其他 bool    HitTest( CPoint point )
bool    HitTest( Point point )
bool    HitTest( PointF point )

私有方法

有几个私有方法用于内部使用,除非您想扩展它们的功能,否则通常可以忽略它们。

历史

2001 年 12 月 11 日 - 更新了源代码。
© . All rights reserved.