?? stroke.cpp
字號:
#include "stdafx.h"
#include "Stroke.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStroke
IMPLEMENT_SERIAL(CStroke, CObject, 2)
CStroke::CStroke()
{
//序列化將使用該空構造函數,不能少
}
CStroke::CStroke(UINT nPenWidth)
{
m_nPenWidth = nPenWidth;
m_rectBounding.SetRectEmpty(); //設置為(0,0,0,0)
}
void CStroke::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << m_rectBounding; //m_rectBounding信息序列化到ar中
ar << (WORD)m_nPenWidth; //m_nPenWidth信息序列化到ar中
m_pointArray.Serialize(ar); //m_pointArray信息序列化到ar中
}
else
{
ar >> m_rectBounding; //ar信息序列化到m_rectBounding中
WORD w;
ar >> w; //ar信息序列化到w中
m_nPenWidth = w;
m_pointArray.Serialize(ar); //ar信息序列化到m_pointArray中
}
}
BOOL CStroke::DrawStroke(CDC* pDC) //畫線函數
{
CPen penStroke;
if (!penStroke.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,255)))//創建筆對象
return FALSE;
CPen* pOldPen = pDC->SelectObject(&penStroke); //把新創建的筆選進設備上下文,并保存舊的筆對象
pDC->MoveTo(m_pointArray[0]); //移到第一個點
for (int i=1; i < m_pointArray.GetSize(); i++) //循環連接所有的點
{
pDC->LineTo(m_pointArray[i]);
}
pDC->SelectObject(pOldPen);
return TRUE;
}
void CStroke::FinishStroke()
{
//計算最小矩形,重畫時需要該矩形。
if (m_pointArray.GetSize()==0)
{
m_rectBounding.SetRectEmpty();
return;
}
CPoint pt = m_pointArray[0];
m_rectBounding = CRect(pt.x, pt.y, pt.x, pt.y);
for (int i=1; i < m_pointArray.GetSize(); i++)
{
//如果點在矩形m_rectBounding之外,就增大矩形把該點包含進來
pt = m_pointArray[i];
m_rectBounding.left = min(m_rectBounding.left, pt.x);
m_rectBounding.right = max(m_rectBounding.right, pt.x);
m_rectBounding.top = max(m_rectBounding.top, pt.y);
m_rectBounding.bottom = min(m_rectBounding.bottom, pt.y);
}
//使m_rectBounding的長寬各增大筆寬m_nPenWidth個單位,以免部分象素不能被更新。
m_rectBounding.InflateRect(CSize(m_nPenWidth, -(int)m_nPenWidth));
return;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -