MFC基本绘图函数
在Windows平台上,应用程序的图形设备接口(Graphics Device Interface, GDI)被抽象为设备上下文CDC类(Device Context, DC)。因此,直接接受图形数据信息的不是显示器和打印机等硬件设备,而是CDC对象。
CDC类结构
CDC类
- CDC类派生:CClientDC类、CMetaFileDC类、CPaintDC类和CWindowDC类
- CClientDC类:显示器客户区设备上下文类,只能在窗口的客户区进行绘图,点(0,0)是客户区的左上角。构造函数自动调用GetDC()函数,析构函数自动调用ReleaseDC()函数
- CMetaFileDC类:Windows图元文件设备上下文类,封装了在Windows中绘图图元的方法
- CPaintDC类:只在响应WM_PAINT消息时使用,构造函数自动调用BeginPaint()函数,析构函数自动调用EndPaint()函数。使用在视图窗口中绘图时,需要先添加WM_PAINT消息的映射函数OnPaint(),然后在OnPaint()函数编写与CPaintDC类相关的代码,而不是编写在OnDraw()中。如果使用OnPaint()函数响应了WM_PAINT消息,OnDraw()函数会自动屏蔽
- CWindowDC类:整个屏幕区域的显示器设备上下文类,包括客户区和非客户区,允许在整个屏幕区域内进行绘图,构造函数自动调用GetWindowDC(),析构函数自动调用ReleaseDC()函数。点(0, 0) 位于屏幕的左上角,而CClientDC和CPaintDC中的点(0, 0)在屏幕客户区的左上角。如果在CTestView类中是同CWindowDC类对象进行绘图,只有在使用GetParent()函数获得CWnd指针后,才能在整个屏幕区域内绘图。
简单的数据类型
CPont, CSize, CRect是对Windows的POINT, RECT, SIZE结构体的封装(继承)
在c:\Program(x86)\Windows Kits\8.1\Include\Shared\windef.h已经定义了以上结构体:
POINT结构:
typedef struct tagPOINT
{LONG x;LONG y;
} POINT, *PPOINT, NEAR *NPPOINT, FAR *LPPOINT;
SIZE结构体:
typedef struct tagSIZE
{LONG cx;LONG cy;
} SIZE, *PSIZE, *LPSIZE;
RECT结构体:
typedef struct tagRECT
{LONG left;LONG top;LONG right;LONG bottom;
} RECT, *PRECT, NEAR *NPRECT, FAR *LPRECT;
在c:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\arlmfc\include\atltypes.h定义了以下类:
CPoint类:
class CPoint :public tagPOINT
{
public:
// Constructors// create an uninitialized pointCPoint() throw();// create from two integersCPoint(_In_ int initX,_In_ int initY) throw();// create from another pointCPoint(_In_ POINT initPt) throw();// create from a sizeCPoint(_In_ SIZE initSize) throw();// create from an LPARAM: x = LOWORD(dw) y = HIWORD(dw)CPoint(_In_ LPARAM dwPoint) throw();// Operations// translate the pointvoid Offset(_In_ int xOffset,_In_ int yOffset) throw();void Offset(_In_ POINT point) throw();void Offset(_In_ SIZE size) throw();void SetPoint(_In_ int X,_In_ int Y) throw();BOOL operator==(_In_ POINT point) const throw();BOOL operator!=(_In_ POINT point) const throw();void operator+=(_In_ SIZE size) throw();void operator-=(_In_ SIZE size) throw();void operator+=(_In_ POINT point) throw();void operator-=(_In_ POINT point) throw();// Operators returning CPoint valuesCPoint operator+(_In_ SIZE size) const throw();CPoint operator-(_In_ SIZE size) const throw();CPoint operator-() const throw();CPoint operator+(_In_ POINT point) const throw();// Operators returning CSize valuesCSize operator-(_In_ POINT point) const throw();// Operators returning CRect valuesCRect operator+(_In_ const RECT* lpRect) const throw();CRect operator-(_In_ const RECT* lpRect) const throw();
};
initX和initY是点x, y坐标,initPt是一个POINT结构
其中:函数后面加throw()的作用可以参考:C++函数后面加throw关键字的含义
CPoint类及其成员函数的初始化:
// CPoint
inline CPoint::CPoint() throw()
{x = 0;y = 0;
}inline CPoint::CPoint(_In_ int initX,_In_ int initY) throw()
{x = initX;y = initY;
}inline CPoint::CPoint(_In_ POINT initPt) throw()
{*(POINT*)this = initPt;
}inline CPoint::CPoint(_In_ SIZE initSize) throw()
{*(SIZE*)this = initSize;
}inline CPoint::CPoint(_In_ LPARAM dwPoint) throw()
{x = (short)LOWORD(dwPoint);y = (short)HIWORD(dwPoint);
}inline void CPoint::Offset(_In_ int xOffset,_In_ int yOffset) throw()
{x += xOffset;y += yOffset;
}inline void CPoint::Offset(_In_ POINT point) throw()
{x += point.x;y += point.y;
}inline void CPoint::Offset(_In_ SIZE size) throw()
{x += size.cx;y += size.cy;
}inline void CPoint::SetPoint(_In_ int X,_In_ int Y) throw()
{x = X;y = Y;
}
CPoint类的操作符重载:
inline BOOL CPoint::operator==(_In_ POINT point) const throw()
{return (x == point.x && y == point.y);
}inline BOOL CPoint::operator!=(_In_ POINT point) const throw()
{return (x != point.x || y != point.y);
}inline void CPoint::operator+=(_In_ SIZE size) throw()
{x += size.cx;y += size.cy;
}inline void CPoint::operator-=(_In_ SIZE size) throw()
{x -= size.cx;y -= size.cy;
}inline void CPoint::operator+=(_In_ POINT point) throw()
{x += point.x;y += point.y;
}inline void CPoint::operator-=(_In_ POINT point) throw()
{x -= point.x;y -= point.y;
}inline CPoint CPoint::operator+(_In_ SIZE size) const throw()
{return CPoint(x + size.cx, y + size.cy);
}inline CPoint CPoint::operator-(_In_ SIZE size) const throw()
{return CPoint(x - size.cx, y - size.cy);
}inline CPoint CPoint::operator-() const throw()
{return CPoint(-x, -y);
}inline CPoint CPoint::operator+(_In_ POINT point) const throw()
{return CPoint(x + point.x, y + point.y);
}inline CSize CPoint::operator-(_In_ POINT point) const throw()
{return CSize(x - point.x, y - point.y);
}inline CRect CPoint::operator+(_In_ const RECT* lpRect) const throw()
{return CRect(lpRect) + *this;
}inline CRect CPoint::operator-(_In_ const RECT* lpRect) const throw()
{return CRect(lpRect) - *this;
}
CSize类:
class CSize :public tagSIZE
{
public:// Constructors// construct an uninitialized sizeCSize() throw();// create from two integersCSize(_In_ int initCX,_In_ int initCY) throw();// create from another sizeCSize(_In_ SIZE initSize) throw();// create from a pointCSize(_In_ POINT initPt) throw();// create from a DWORD: cx = LOWORD(dw) cy = HIWORD(dw)CSize(_In_ DWORD dwSize) throw();// OperationsBOOL operator==(_In_ SIZE size) const throw();BOOL operator!=(_In_ SIZE size) const throw();void operator+=(_In_ SIZE size) throw();void operator-=(_In_ SIZE size) throw();void SetSize(_In_ int CX, _In_ int CY) throw();// Operators returning CSize valuesCSize operator+(_In_ SIZE size) const throw();CSize operator-(_In_ SIZE size) const throw();CSize operator-() const throw();// Operators returning CPoint valuesCPoint operator+(_In_ POINT point) const throw();CPoint operator-(_In_ POINT point) const throw();// Operators returning CRect valuesCRect operator+(_In_ const RECT* lpRect) const throw();CRect operator-(_In_ const RECT* lpRect) const throw();
};
initCX和initCY是举行的x, y长度,initSize是一个SIZE结构
CSize类及其成员函数的初始化:
// CSize
inline CSize::CSize() throw()
{cx = 0;cy = 0;
}inline CSize::CSize(_In_ int initCX,_In_ int initCY) throw()
{cx = initCX;cy = initCY;
}inline CSize::CSize(_In_ SIZE initSize) throw()
{*(SIZE*)this = initSize;
}inline CSize::CSize(_In_ POINT initPt) throw()
{*(POINT*)this = initPt;
}inline CSize::CSize(_In_ DWORD dwSize) throw()
{cx = (short)LOWORD(dwSize);cy = (short)HIWORD(dwSize);
}
CSize类的操作符重载:
inline BOOL CSize::operator==(_In_ SIZE size) const throw()
{return (cx == size.cx && cy == size.cy);
}inline BOOL CSize::operator!=(_In_ SIZE size) const throw()
{return (cx != size.cx || cy != size.cy);
}inline void CSize::operator+=(_In_ SIZE size) throw()
{cx += size.cx;cy += size.cy;
}inline void CSize::operator-=(_In_ SIZE size) throw()
{cx -= size.cx;cy -= size.cy;
}inline void CSize::SetSize(_In_ int CX,_In_ int CY) throw()
{cx = CX;cy = CY;
}inline CSize CSize::operator+(_In_ SIZE size) const throw()
{return CSize(cx + size.cx, cy + size.cy);
}inline CSize CSize::operator-(_In_ SIZE size) const throw()
{return CSize(cx - size.cx, cy - size.cy);
}inline CSize CSize::operator-() const throw()
{return CSize(-cx, -cy);
}inline CPoint CSize::operator+(_In_ POINT point) const throw()
{return CPoint(cx + point.x, cy + point.y);
}inline CPoint CSize::operator-(_In_ POINT point) const throw()
{return CPoint(cx - point.x, cy - point.y);
}inline CRect CSize::operator+(_In_ const RECT* lpRect) const throw()
{return CRect(lpRect) + *this;
}inline CRect CSize::operator-(_In_ const RECT* lpRect) const throw()
{return CRect(lpRect) - *this;
}
CRect类:
class CRect :public tagRECT
{
// Constructors
public:// uninitialized rectangleCRect() throw();// from left, top, right, and bottomCRect(_In_ int l,_In_ int t,_In_ int r,_In_ int b) throw();// copy constructorCRect(_In_ const RECT& srcRect) throw();// from a pointer to another rectCRect(_In_ LPCRECT lpSrcRect) throw();// from a point and sizeCRect(_In_ POINT point,_In_ SIZE size) throw();// from two pointsCRect(_In_ POINT topLeft,_In_ POINT bottomRight) throw();// Attributes (in addition to RECT members)// retrieves the widthint Width() const throw();// returns the heightint Height() const throw();// returns the sizeCSize Size() const throw();// reference to the top-left pointCPoint& TopLeft() throw();// reference to the bottom-right pointCPoint& BottomRight() throw();// const reference to the top-left pointconst CPoint& TopLeft() const throw();// const reference to the bottom-right pointconst CPoint& BottomRight() const throw();// the geometric center point of the rectangleCPoint CenterPoint() const throw();// swap the left and rightvoid SwapLeftRight() throw();static void WINAPI SwapLeftRight(_Inout_ LPRECT lpRect) throw();// convert between CRect and LPRECT/LPCRECT (no need for &)operator LPRECT() throw();operator LPCRECT() const throw();// returns TRUE if rectangle has no areaBOOL IsRectEmpty() const throw();// returns TRUE if rectangle is at (0,0) and has no areaBOOL IsRectNull() const throw();// returns TRUE if point is within rectangleBOOL PtInRect(_In_ POINT point) const throw();// Operations// set rectangle from left, top, right, and bottomvoid SetRect(_In_ int x1,_In_ int y1,_In_ int x2,_In_ int y2) throw();void SetRect(_In_ POINT topLeft,_In_ POINT bottomRight) throw();// empty the rectanglevoid SetRectEmpty() throw();// copy from another rectanglevoid CopyRect(_In_ LPCRECT lpSrcRect) throw();// TRUE if exactly the same as another rectangleBOOL EqualRect(_In_ 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(_In_ int x,_In_ 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(_In_ 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(_In_ LPCRECT lpRect) throw();void InflateRect(_In_ int l,_In_ int t,_In_ int r,_In_ int b) throw();// deflate the rectangle's width and height without// moving its top or leftvoid DeflateRect(_In_ int x,_In_ int y) throw();void DeflateRect(_In_ SIZE size) throw();void DeflateRect(_In_ LPCRECT lpRect) throw();void DeflateRect(_In_ int l,_In_ int t,_In_ int r,_In_ int b) throw();// translate the rectangle by moving its top and leftvoid OffsetRect(_In_ int x,_In_ int y) throw();void OffsetRect(_In_ SIZE size) throw();void OffsetRect(_In_ POINT point) throw();void NormalizeRect() throw();// absolute position of rectanglevoid MoveToY(_In_ int y) throw();void MoveToX(_In_ int x) throw();void MoveToXY(_In_ int x,_In_ int y) throw();void MoveToXY(_In_ POINT point) throw();// set this rectangle to intersection of two othersBOOL IntersectRect(_In_ LPCRECT lpRect1,_In_ LPCRECT lpRect2) throw();// set this rectangle to bounding union of two othersBOOL UnionRect(_In_ LPCRECT lpRect1,_In_ LPCRECT lpRect2) throw();// set this rectangle to minimum of two othersBOOL SubtractRect(_In_ LPCRECT lpRectSrc1,_In_ LPCRECT lpRectSrc2) throw();// Additional Operationsvoid operator=(_In_ const RECT& srcRect) throw();BOOL operator==(_In_ const RECT& rect) const throw();BOOL operator!=(_In_ const RECT& rect) const throw();void operator+=(_In_ POINT point) throw();void operator+=(_In_ SIZE size) throw();void operator+=(_In_ LPCRECT lpRect) throw();void operator-=(_In_ POINT point) throw();void operator-=(_In_ SIZE size) throw();void operator-=(_In_ LPCRECT lpRect) throw();void operator&=(_In_ const RECT& rect) throw();void operator|=(_In_ const RECT& rect) throw();// Operators returning CRect valuesCRect operator+(_In_ POINT point) const throw();CRect operator-(_In_ POINT point) const throw();CRect operator+(_In_ LPCRECT lpRect) const throw();CRect operator+(_In_ SIZE size) const throw();CRect operator-(_In_ SIZE size) const throw();CRect operator-(_In_ LPCRECT lpRect) const throw();CRect operator&(_In_ const RECT& rect2) const throw();CRect operator|(_In_ const RECT& rect2) const throw();CRect MulDiv(_In_ int nMultiplier,_In_ int nDivisor) const throw();
};
- l, t是矩形左上角的x,y坐标;r,b是矩形右下角的x,y坐标
- srcRect是一个RECT结构
- lpSrcRect是RECT结构的指针
- point 用来指定矩形的左上角,size指定矩形的x,y方向长度
- topLeft, bottomRight是矩形的POINT结构的左上角和右下角点
CRect类及其成员函数的初始化:
// CRect
inline CRect::CRect() throw()
{left = 0;top = 0;right = 0;bottom = 0;
}inline CRect::CRect(_In_ int l,_In_ int t,_In_ int r,_In_ int b) throw()
{left = l;top = t;right = r;bottom = b;
}inline CRect::CRect(_In_ const RECT& srcRect) throw()
{::CopyRect(this, &srcRect);
}inline CRect::CRect(_In_ LPCRECT lpSrcRect) throw()
{::CopyRect(this, lpSrcRect);
}inline CRect::CRect(_In_ POINT point,_In_ SIZE size) throw()
{right = (left = point.x) + size.cx;bottom = (top = point.y) + size.cy;
}inline CRect::CRect(_In_ POINT topLeft,_In_ POINT bottomRight) throw()
{left = topLeft.x;top = topLeft.y;right = bottomRight.x;bottom = bottomRight.y;
}inline int CRect::Width() const throw()
{return right - left;
}inline int CRect::Height() const throw()
{return bottom - top;
}inline CSize CRect::Size() const throw()
{return CSize(right - left, bottom - top);
}inline CPoint& CRect::TopLeft() throw()
{return *((CPoint*)this);
}inline CPoint& CRect::BottomRight() throw()
{return *((CPoint*)this+1);
}inline const CPoint& CRect::TopLeft() const throw()
{return *((CPoint*)this);
}inline const CPoint& CRect::BottomRight() const throw()
{return *((CPoint*)this+1);
}inline CPoint CRect::CenterPoint() const throw()
{return CPoint((left+right)/2, (top+bottom)/2);
}inline void CRect::SwapLeftRight() throw()
{SwapLeftRight(LPRECT(this));
}inline void WINAPI CRect::SwapLeftRight(_Inout_ LPRECT lpRect) throw()
{LONG temp = lpRect->left;lpRect->left = lpRect->right;lpRect->right = temp;
}inline BOOL CRect::IsRectEmpty() const throw()
{return ::IsRectEmpty(this);
}inline BOOL CRect::IsRectNull() const throw()
{return (left == 0 && right == 0 && top == 0 && bottom == 0);
}inline BOOL CRect::PtInRect(_In_ POINT point) const throw()
{return ::PtInRect(this, point);
}inline void CRect::SetRect(_In_ int x1,_In_ int y1,_In_ int x2,_In_ int y2) throw()
{::SetRect(this, x1, y1, x2, y2);
}inline void CRect::SetRect(_In_ POINT topLeft,_In_ POINT bottomRight) throw()
{::SetRect(this, topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
}inline void CRect::SetRectEmpty() throw()
{::SetRectEmpty(this);
}inline void CRect::CopyRect(_In_ LPCRECT lpSrcRect) throw()
{::CopyRect(this, lpSrcRect);
}inline BOOL CRect::EqualRect(_In_ LPCRECT lpRect) const throw()
{return ::EqualRect(this, lpRect);
}inline void CRect::InflateRect(_In_ int x,_In_ int y) throw()
{::InflateRect(this, x, y);
}inline void CRect::InflateRect(_In_ SIZE size) throw()
{::InflateRect(this, size.cx, size.cy);
}inline void CRect::DeflateRect(_In_ int x,_In_ int y) throw()
{::InflateRect(this, -x, -y);
}inline void CRect::DeflateRect(_In_ SIZE size) throw()
{::InflateRect(this, -size.cx, -size.cy);
}inline void CRect::OffsetRect(_In_ int x,_In_ int y) throw()
{::OffsetRect(this, x, y);
}inline void CRect::OffsetRect(_In_ POINT point) throw()
{::OffsetRect(this, point.x, point.y);
}inline void CRect::OffsetRect(_In_ SIZE size) throw()
{::OffsetRect(this, size.cx, size.cy);
}inline void CRect::MoveToY(_In_ int y) throw()
{bottom = Height() + y;top = y;
}inline void CRect::MoveToX(_In_ int x) throw()
{right = Width() + x;left = x;
}inline void CRect::MoveToXY(_In_ int x,_In_ int y) throw()
{MoveToX(x);MoveToY(y);
}inline void CRect::MoveToXY(_In_ POINT pt) throw()
{MoveToX(pt.x);MoveToY(pt.y);
}inline BOOL CRect::IntersectRect(_In_ LPCRECT lpRect1,_In_ LPCRECT lpRect2) throw()
{return ::IntersectRect(this, lpRect1, lpRect2);
}inline BOOL CRect::UnionRect(_In_ LPCRECT lpRect1,_In_ LPCRECT lpRect2) throw()
{return ::UnionRect(this, lpRect1, lpRect2);
}
inline BOOL CRect::SubtractRect(_In_ LPCRECT lpRectSrc1,_In_ LPCRECT lpRectSrc2) throw()
{return ::SubtractRect(this, lpRectSrc1, lpRectSrc2);
}inline void CRect::NormalizeRect() throw()
{int nTemp;if (left > right){nTemp = left;left = right;right = nTemp;}if (top > bottom){nTemp = top;top = bottom;bottom = nTemp;}
}inline void CRect::InflateRect(_In_ LPCRECT lpRect) throw()
{left -= lpRect->left;top -= lpRect->top;right += lpRect->right;bottom += lpRect->bottom;
}inline void CRect::InflateRect(_In_ int l,_In_ int t,_In_ int r,_In_ int b) throw()
{left -= l;top -= t;right += r;bottom += b;
}inline void CRect::DeflateRect(_In_ LPCRECT lpRect) throw()
{left += lpRect->left;top += lpRect->top;right -= lpRect->right;bottom -= lpRect->bottom;
}inline void CRect::DeflateRect(_In_ int l,_In_ int t,_In_ int r,_In_ int b) throw()
{left += l;top += t;right -= r;bottom -= b;
}inline CRect CRect::MulDiv(_In_ int nMultiplier,_In_ int nDivisor) const throw()
{return CRect(::MulDiv(left, nMultiplier, nDivisor),::MulDiv(top, nMultiplier, nDivisor),::MulDiv(right, nMultiplier, nDivisor),::MulDiv(bottom, nMultiplier, nDivisor));
}
- CRect::Width()计算宽度函数
- CRect::Height()计算高度函数
- CRect::TopLeft()计算左上角点函数
- CRect::BottomRight()计算右下角点函数
- CRect::CenterPoint()计算中心点函数
- CRect::InflateRect()扩大矩形函数
- CRect::DeflateRect()缩小矩形函数
- CRect::OffsetRect()移动矩形函数
CRect类的操作符重载:
inline CRect::operator LPRECT() throw()
{return this;
}inline CRect::operator LPCRECT() const throw()
{return this;
}
inline void CRect::operator=(_In_ const RECT& srcRect) throw()
{::CopyRect(this, &srcRect);
}inline BOOL CRect::operator==(_In_ const RECT& rect) const throw()
{return ::EqualRect(this, &rect);
}inline BOOL CRect::operator!=(_In_ const RECT& rect) const throw()
{return !::EqualRect(this, &rect);
}inline void CRect::operator+=(_In_ POINT point) throw()
{::OffsetRect(this, point.x, point.y);
}inline void CRect::operator+=(_In_ SIZE size) throw()
{::OffsetRect(this, size.cx, size.cy);
}inline void CRect::operator+=(_In_ LPCRECT lpRect) throw()
{InflateRect(lpRect);
}inline void CRect::operator-=(_In_ POINT point) throw()
{::OffsetRect(this, -point.x, -point.y);
}inline void CRect::operator-=(_In_ SIZE size) throw()
{::OffsetRect(this, -size.cx, -size.cy);
}inline void CRect::operator-=(_In_ LPCRECT lpRect) throw()
{DeflateRect(lpRect);
}inline void CRect::operator&=(_In_ const RECT& rect) throw()
{::IntersectRect(this, this, &rect);
}inline void CRect::operator|=(_In_ const RECT& rect) throw()
{::UnionRect(this, this, &rect);
}inline CRect CRect::operator+(_In_ POINT pt) const throw()
{CRect rect(*this);::OffsetRect(&rect, pt.x, pt.y);return rect;
}inline CRect CRect::operator-(_In_ POINT pt) const throw()
{CRect rect(*this);::OffsetRect(&rect, -pt.x, -pt.y);return rect;
}inline CRect CRect::operator+(_In_ SIZE size) const throw()
{CRect rect(*this);::OffsetRect(&rect, size.cx, size.cy);return rect;
}inline CRect CRect::operator-(_In_ SIZE size) const throw()
{CRect rect(*this);::OffsetRect(&rect, -size.cx, -size.cy);return rect;
}inline CRect CRect::operator+(_In_ LPCRECT lpRect) const throw()
{CRect rect(this);rect.InflateRect(lpRect);return rect;
}inline CRect CRect::operator-(_In_ LPCRECT lpRect) const throw()
{CRect rect(this);rect.DeflateRect(lpRect);return rect;
}inline CRect CRect::operator&(_In_ const RECT& rect2) const throw()
{CRect rect;::IntersectRect(&rect, this, &rect2);return rect;
}inline CRect CRect::operator|(_In_ const RECT& rect2) const throw()
{CRect rect;::UnionRect(&rect, this, &rect2);return rect;
}
CRect类重载了LPCRECT操作符,可以将CRect对象自动转换为CRect指针,为以CRect对象为参数的函数提供自动类型转换,包括InflateRect(), DeflateRect(), Rectangle(), GetClientRect()