2009年12月27日 星期日

CuRect - Similar to a Windows RECT structure.

// Similar to a Windows RECT structure. 

CuRect rt;
GetClientRect(hWnd, &rt);

// CuRect.h

class CuRect : public tagRECT
{
    // Constructors
public:
    // uninitialized rectangle
    CuRect() throw();
    // from left, top, right, and bottom
    CuRect(int l, int t, int r, int b) throw();
    // copy constructor
    CuRect(const RECT& srcRect) throw();
    // from a pointer to another rect
    CuRect(LPCRECT lpSrcRect) throw();
    // from a point and size
    CuRect(POINT point, SIZE size) throw();
    // from two points
    CuRect(POINT topLeft, POINT bottomRight) throw();

    // Attributes (in addition to RECT members)

    // retrieves the width
    int Width() const throw();
    // returns the height
    int Height() const throw();
    // returns the size
    CuSize Size() const throw();
    // reference to the top-left point
    CuPoint& TopLeft() throw();
    // reference to the bottom-right point
    CuPoint& BottomRight() throw();
    // const reference to the top-left point
    const CuPoint& TopLeft() const throw();
    // const reference to the bottom-right point
    const CuPoint& BottomRight() const throw();
    // the geometric center point of the rectangle
    CuPoint CenterPoint() const throw();
    // swap the left and right
    void SwapLeftRight() throw();
    static void WINAPI SwapLeftRight(LPRECT lpRect) throw();

    // convert between CuRect and LPRECT/LPCRECT (no need for &)
    operator LPRECT() throw();
    operator LPCRECT() const throw();

    // returns TRUE if rectangle has no area
    BOOL IsRectEmpty() const throw();
    // returns TRUE if rectangle is at (0,0) and has no area
    BOOL IsRectNull() const throw();
    // returns TRUE if point is within rectangle
    BOOL PtInRect(POINT point) const throw();

    // Operations

    // set rectangle from left, top, right, and bottom
    void SetRect(int x1, int y1, int x2, int y2) throw();
    void SetRect(POINT topLeft, POINT bottomRight) throw();
    // empty the rectangle
    void SetRectEmpty() throw();
    // copy from another rectangle
    void CopyRect(LPCRECT lpSrcRect) throw();
    // TRUE if exactly the same as another rectangle
    BOOL EqualRect(LPCRECT lpRect) const throw();

    // Inflate rectangle's width and height by
    // x units to the left and right ends of the rectangle
    // and y units to the top and bottom.
    void InflateRect(int x, int y) throw();
    // Inflate rectangle's width and height by
    // size.cx units to the left and right ends of the rectangle
    // and size.cy units to the top and bottom.
    void InflateRect(SIZE size) throw();
    // Inflate rectangle's width and height by moving individual sides.
    // Left side is moved to the left, right side is moved to the right,
    // top is moved up and bottom is moved down.
    void InflateRect(LPCRECT lpRect) throw();
    void InflateRect(int l, int t, int r, int b) throw();

    // deflate the rectangle's width and height without
    // moving its top or left
    void DeflateRect(int x, int y) throw();
    void DeflateRect(SIZE size) throw();
    void DeflateRect(LPCRECT lpRect) throw();
    void DeflateRect(int l, int t, int r, int b) throw();

    // translate the rectangle by moving its top and left
    void OffsetRect(int x, int y) throw();
    void OffsetRect(SIZE size) throw();
    void OffsetRect(POINT point) throw();
    void NormalizeRect() throw();

    // absolute position of rectangle
    void MoveToY(int y) throw();
    void MoveToX(int x) throw();
    void MoveToXY(int x, int y) throw();
    void MoveToXY(POINT point) throw();

    // set this rectangle to intersection of two others
    BOOL IntersectRect(LPCRECT lpRect1, LPCRECT lpRect2) throw();

    // set this rectangle to bounding union of two others
    BOOL UnionRect(LPCRECT lpRect1, LPCRECT lpRect2) throw();

    // set this rectangle to minimum of two others
    BOOL SubtractRect(LPCRECT lpRectSrc1, LPCRECT lpRectSrc2) throw();

    // Additional Operations
    void operator=(const RECT& srcRect) throw();
    BOOL operator==(const RECT& rect) const throw();
    BOOL operator!=(const RECT& rect) const throw();
    void operator+=(POINT point) throw();
    void operator+=(SIZE size) throw();
    void operator+=(LPCRECT lpRect) throw();
    void operator-=(POINT point) throw();
    void operator-=(SIZE size) throw();
    void operator-=(LPCRECT lpRect) throw();
    void operator&=(const RECT& rect) throw();
    void operator|=(const RECT& rect) throw();

    // Operators returning CuRect values
    CuRect operator+(POINT point) const throw();
    CuRect operator-(POINT point) const throw();
    CuRect operator+(LPCRECT lpRect) const throw();
    CuRect operator+(SIZE size) const throw();
    CuRect operator-(SIZE size) const throw();
    CuRect operator-(LPCRECT lpRect) const throw();
    CuRect operator&(const RECT& rect2) const throw();
    CuRect operator|(const RECT& rect2) const throw();
    //  CuRect MulDiv(int nMultiplier, int nDivisor) const throw();
};


inline CuRect::CuRect()
{ /* random filled */ }
inline CuRect::CuRect(int l, int t, int r, int b)
{ left = l; top = t; right = r; bottom = b; }
inline CuRect::CuRect(const RECT& srcRect)
{ ::CopyRect(this, &srcRect); }
inline CuRect::CuRect(LPCRECT lpSrcRect)
{ ::CopyRect(this, lpSrcRect); }
inline CuRect::CuRect(POINT point, SIZE size)
{ right = (left = point.x) + size.cx; bottom = (top = point.y) + size.cy; }
inline CuRect::CuRect(POINT topLeft, POINT bottomRight)
{ left = topLeft.x; top = topLeft.y;
right = bottomRight.x; bottom = bottomRight.y; }
inline int CuRect::Width() const
{ return right - left; }
inline int CuRect::Height() const
{ return bottom - top; }
inline CuSize CuRect::Size() const
{ return CuSize(right - left, bottom - top); }
inline CuPoint& CuRect::TopLeft()
{ return *((CuPoint*)this); }
inline CuPoint& CuRect::BottomRight()
{ return *((CuPoint*)this+1); }
inline const CuPoint& CuRect::TopLeft() const
{ return *((CuPoint*)this); }
inline const CuPoint& CuRect::BottomRight() const
{ return *((CuPoint*)this+1); }
inline CuPoint CuRect::CenterPoint() const
{ return CuPoint((left+right)/2, (top+bottom)/2); }
inline void CuRect::SwapLeftRight()
{ SwapLeftRight(LPRECT(this)); }
inline void WINAPI CuRect::SwapLeftRight(LPRECT lpRect)
{ LONG temp = lpRect->left; lpRect->left = lpRect->right; lpRect->right = temp; }
inline CuRect::operator LPRECT()
{ return this; }
inline CuRect::operator LPCRECT() const
{ return this; }
inline BOOL CuRect::IsRectEmpty() const
{ return ::IsRectEmpty(this); }
inline BOOL CuRect::IsRectNull() const
{ return (left == 0 && right == 0 && top == 0 && bottom == 0); }
inline BOOL CuRect::PtInRect(POINT point) const
{ return ::PtInRect(this, point); }
inline void CuRect::SetRect(int x1, int y1, int x2, int y2)
{ ::SetRect(this, x1, y1, x2, y2); }
inline void CuRect::SetRect(POINT topLeft, POINT bottomRight)
{ ::SetRect(this, topLeft.x, topLeft.y, bottomRight.x, bottomRight.y); }
inline void CuRect::SetRectEmpty()
{ ::SetRectEmpty(this); }
inline void CuRect::CopyRect(LPCRECT lpSrcRect)
{ ::CopyRect(this, lpSrcRect); }
inline BOOL CuRect::EqualRect(LPCRECT lpRect) const
{ return ::EqualRect(this, lpRect); }
inline void CuRect::InflateRect(int x, int y)
{ ::InflateRect(this, x, y); }
inline void CuRect::InflateRect(SIZE size)
{ ::InflateRect(this, size.cx, size.cy); }
inline void CuRect::DeflateRect(int x, int y)
{ ::InflateRect(this, -x, -y); }
inline void CuRect::DeflateRect(SIZE size)
{ ::InflateRect(this, -size.cx, -size.cy); }
inline void CuRect::OffsetRect(int x, int y)
{ ::OffsetRect(this, x, y); }
inline void CuRect::OffsetRect(POINT point)
{ ::OffsetRect(this, point.x, point.y); }
inline void CuRect::OffsetRect(SIZE size)
{ ::OffsetRect(this, size.cx, size.cy); }
inline void CuRect::MoveToY(int y)
{ bottom = Height() + y; top = y; }
inline void CuRect::MoveToX(int x)
{ right = Width() + x; left = x; }
inline void CuRect::MoveToXY(int x, int y)
{ MoveToX(x); MoveToY(y); }
inline void CuRect::MoveToXY(POINT pt)
{ MoveToX(pt.x); MoveToY(pt.y); }
inline BOOL CuRect::IntersectRect(LPCRECT lpRect1, LPCRECT lpRect2)
{ return ::IntersectRect(this, lpRect1, lpRect2);}
inline BOOL CuRect::UnionRect(LPCRECT lpRect1, LPCRECT lpRect2)
{ return ::UnionRect(this, lpRect1, lpRect2); }
inline void CuRect::operator=(const RECT& srcRect)
{ ::CopyRect(this, &srcRect); }
inline BOOL CuRect::operator==(const RECT& rect) const
{ return ::EqualRect(this, &rect); }
inline BOOL CuRect::operator!=(const RECT& rect) const
{ return !::EqualRect(this, &rect); }
inline void CuRect::operator+=(POINT point)
{ ::OffsetRect(this, point.x, point.y); }
inline void CuRect::operator+=(SIZE size)
{ ::OffsetRect(this, size.cx, size.cy); }
inline void CuRect::operator+=(LPCRECT lpRect)
{ InflateRect(lpRect); }
inline void CuRect::operator-=(POINT point)
{ ::OffsetRect(this, -point.x, -point.y); }
inline void CuRect::operator-=(SIZE size)
{ ::OffsetRect(this, -size.cx, -size.cy); }
inline void CuRect::operator-=(LPCRECT lpRect)
{ DeflateRect(lpRect); }
inline void CuRect::operator&=(const RECT& rect)
{ ::IntersectRect(this, this, &rect); }
inline void CuRect::operator|=(const RECT& rect)
{ ::UnionRect(this, this, &rect); }
inline CuRect CuRect::operator+(POINT pt) const
{ CuRect rect(*this); ::OffsetRect(&rect, pt.x, pt.y); return rect; }
inline CuRect CuRect::operator-(POINT pt) const
{ CuRect rect(*this); ::OffsetRect(&rect, -pt.x, -pt.y); return rect; }
inline CuRect CuRect::operator+(SIZE size) const
{ CuRect rect(*this); ::OffsetRect(&rect, size.cx, size.cy); return rect; }
inline CuRect CuRect::operator-(SIZE size) const
{ CuRect rect(*this); ::OffsetRect(&rect, -size.cx, -size.cy); return rect; }
inline CuRect CuRect::operator+(LPCRECT lpRect) const
{ CuRect rect(this); rect.InflateRect(lpRect); return rect; }
inline CuRect CuRect::operator-(LPCRECT lpRect) const
{ CuRect rect(this); rect.DeflateRect(lpRect); return rect; }
inline CuRect CuRect::operator&(const RECT& rect2) const
{ CuRect rect; ::IntersectRect(&rect, this, &rect2);
return rect; }
inline CuRect CuRect::operator|(const RECT& rect2) const
{ CuRect rect; ::UnionRect(&rect, this, &rect2);
return rect; }
inline BOOL CuRect::SubtractRect(LPCRECT lpRectSrc1, LPCRECT lpRectSrc2)
{ return ::SubtractRect(this, lpRectSrc1, lpRectSrc2); }

inline void CuRect::NormalizeRect()
{
    int nTemp;
    if (left > right)
    {
        nTemp = left;
        left = right;
        right = nTemp;
    }
    if (top > bottom)
    {
        nTemp = top;
        top = bottom;
        bottom = nTemp;
    }
}

inline void CuRect::InflateRect(LPCRECT lpRect)
{
    left -= lpRect->left;       top -= lpRect->top;
    right += lpRect->right;     bottom += lpRect->bottom;
}

inline void CuRect::InflateRect(int l, int t, int r, int b)
{
    left -= l;          top -= t;
    right += r;         bottom += b;
}

inline void CuRect::DeflateRect(LPCRECT lpRect)
{
    left += lpRect->left;   top += lpRect->top;
    right -= lpRect->right; bottom -= lpRect->bottom;
}

inline void CuRect::DeflateRect(int l, int t, int r, int b)
{
    left += l;      top += t;
    right -= r;     bottom -= b;
}

1 則留言: