Duilib 源码分析之工具类 CDuiRect 篇

来源:互联网 发布:centos 6.5更新时间 编辑:程序博客网 时间:2024/06/16 16:20

今天介绍一个比较简单的类,但是用起来还是挺方便的: 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() 将左上右下坐标全部重置为 0
  • bool IsNull() const 是否左上右下全为 0, 不过我认为这个函数是用来作判断是否有效的,最好是加上一个判断:left < right && top < bottom
  • void Join(const RECT& rc) 将矩形扩大,扩大的方法是取两个举行中较小的 lefttop,但是取较大的 rightbottom
  • 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) 扩大矩形,扩大的方法是 leftright 各延长 cxtopbottom 各延长 cy, 当然,如果 cxcy 为负数,则就变成了缩小了,还有可能结果变为负数,就是无效矩形了
  • 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个矩形的交集
原创粉丝点击