今天介绍一个比较简单的类,但是用起来还是挺方便的: CDuiRect
老规矩,二话不说,直接贴代码 :
typedef struct tagRECT
{
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT, *PRECT, NEAR *NPRECT, FAR *LPRECT;
class DUILIB_API CDuiRect : public tagRECT
{
public:
CDuiRect();
CDuiRect(const RECT& src);
CDuiRect(long iLeft, long iTop, long iRight, long iBottom);
CDuiRect(LPCTSTR pstrValue);
CDuiString ToString();
int GetWidth() const;
int GetHeight() const;
void Empty();
bool IsNull() const;
void Join(const RECT& rc);
void ResetOffset();
void Normalize();
void Offset(int cx, int cy);
void Inflate(int cx, int cy);
void Deflate(int cx, int cy);
void Union(CDuiRect& rc);
};
由于是继承的 tagRECT
, 所以也有 left/top/right/bottom 属性, 接下来看一下成员函数
void Empty()
将左上右下坐标全部重置为 0bool IsNull() const
是否左上右下全为 0, 不过我认为这个函数是用来作判断是否有效的,最好是加上一个判断:left < right && top < bottom
void Join(const RECT& rc)
将矩形扩大,扩大的方法是取两个举行中较小的left
和top
,但是取较大的right
和bottom
void ResetOffset()
实现为::OffsetRect(this, -left, -top)
,所以可以知道这个函数是作用是移动此矩形到 (0,0) 位置void Normalize()
若left
大于right
或者top
大于bottom
则交换值,不过我认为这个函数没有多少适用场合吧void Offset(int cx, int cy)
按指定的 x 和 y 移动矩形void Inflate(int cx, int cy)
扩大矩形,扩大的方法是left
和right
各延长cx
,top
和bottom
各延长cy
, 当然,如果cx
或cy
为负数,则就变成了缩小了,还有可能结果变为负数,就是无效矩形了void Deflate(int cx, int cy)
,缩小矩形,和void Inflate(int cx, int cy)
正好相反void Union(CDuiRect& rc)
调用的是UnionRect()
,所以作用应该和void Join(const RECT& rc)
是一样的,- 应该还少了一个函数:
void CDuiRect::Intersect(const CDuiRect& rc)
, 作用是取得2个矩形的交集