www.pudn.com > jtlab.rar > fill.cpp
#include "stdafx.h"
#include "VCad.h"
#include "VCadView.h"
#include "Entity.h"
#include "VCadDoc.h"
#include "MainFrm.h"
#include "CreateCmd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////
//define CFill class说明于Entity.h中
IMPLEMENT_SERIAL(CFill, CEntity, 0)
CFill::CFill()
{
Init() ;
}
CFill::CFill(const CFill& fill)
: CEntity(fill)
{
m_poly = fill.m_poly ;
m_points = fill.m_points ;
}
CFill::CFill(POLYP* poly, int points)
{
Init() ;
m_poly = poly ;
m_points = points ;
}
CFill::~CFill()
{
}
CEntity* CFill::Copy()
{
CEntity* pEntity = new CFill(m_poly, m_points);
return pEntity;
}
void CFill::Init()
{
CEntity::Init();
m_type = etFill;
// m_poly.Init();
// m_points.Init();
}
int CFill::GetType()
{
return etFill ;
}
POLYP* CFill::Getpoly()
{
return m_poly;
}
int CFill::Getpoints()
{
return m_points;
}
///////////////////////
void CFill::Draw(CDC * pDC, int drawMode /* = dmNormal */)
{
CPoint slpt[20],kp;int i;
Position pp;
POLYP* qq=m_poly;
kp.x=0;
kp.y=0;
for (i=0;ip;
g_pView->WorldtoScreen(pp, slpt[i]) ;
qq=qq->next;
};
i=0;
//while(slpt[0].x
kp.x=(slpt[i].x+slpt[i+1].x+slpt[i+2].x)/3;
kp.y=(slpt[i].y+slpt[i+1].y+slpt[i+2].y)/3;
int n = GetROP2(pDC->GetSafeHdc());
// create a pen by following rules:
// if in normal draw mode, create a pen by its member variables
// if else, create a pen using global funtion "SetDrawEnvir"
CPen pen;
if( drawMode == dmNormal )
pen.CreatePen(m_lineStyle,m_lineWidth,m_color) ;
else
::SetDrawEnvir(pDC, drawMode, &pen);
CPen* pOldPen = pDC->SelectObject(&pen) ;
pDC->SetMapMode(MM_LOENGLISH);
//修改点:将自编的面区填充函数替换这儿的pDC->FloodFill
pDC->FloodFill(kp.x,kp.y,RGB(255,0,122)) ;
pDC->SelectObject(pOldPen) ;
pDC->SetROP2(n);
}
void CFill::Serialize(CArchive& ar)
{
CEntity::Serialize(ar);
// m_poly.Serialize(ar);
// m_points.Serialize(ar);
}
//CLASS CCreateFill 说明于CreateCmd.h中
CCreateFill::CCreateFill()
: m_poly(NULL), m_points(0)
{
m_nStep = 0; // 初始化操作步为 0
}
CCreateFill::~CCreateFill()
{
}
int CCreateFill::GetType()
{
return ctCreateFill;
}
int CCreateFill::OnLButtonDown(UINT nFlags, const Position& pos)
{POLYP * q;
m_nStep++; // 每次单击鼠标左键时操作步加 1
if(m_nStep==1) // 根据操作步执行相应的操作
{
q=new POLYP;
q->p = pos;
q->next=NULL;
m_poly=q;
m_fp1=m_ep1=pos;
::Prompt("请输入多边形的下一点:") ;
}
else
{
CDC* pDC = g_pView->GetDC(); // 得到设备环境指针
CLine* pTempLine = new CLine(m_fp1,m_ep1);
pTempLine->Draw(pDC, dmDrag);
delete pTempLine;
q=new POLYP;
q->p = pos;
q->next=m_poly;
m_poly=q;
CLine* pNewLine = new CLine(m_fp1,m_ep1);// 根据起点和终点创建直线
pNewLine->Draw(pDC,dmNormal); // 绘制直线
g_pDoc->m_EntityList.AddTail(pNewLine); // 将直线指针添加到图元链表
g_pDoc->SetModifiedFlag(TRUE);// set modified flag ;
m_fp1=m_ep1;
m_ep1=pos;
g_pView->ReleaseDC(pDC);
::Prompt("请输入下一点(点击右键,结束):") ;
}
return 0;
}
int CCreateFill::OnMouseMove(UINT nFlags, const Position& pos)
{
// 用一静态变量nPreRefresh记录进入本函数的刷新次数
static int nPreRefresh = g_nRefresh;
// 布尔变量bRefresh说明在本函数中视窗是否被刷新
BOOL bRefresh = FALSE;
// nCurRefresh用于记录当前的刷新次数
int nCurRefresh = g_nRefresh;
// 如果nCurRefresh和nPreRefresh不相等,说明视窗曾被刷新过
if(nCurRefresh != nPreRefresh){
bRefresh = TRUE;
nPreRefresh = nCurRefresh;
}
if(m_nStep==0)
{
::Prompt("请输入多边形的第一点:") ;}
else
{
Position prePos, curPos;
prePos = m_ep1; // 获得鼠标所在的前一个位置
curPos = pos;
CDC* pDC = g_pView->GetDC(); // 得到设备环境指针
// 创建临时对象擦除上一条橡皮线
CLine* pTempLine1 = new CLine(m_fp1, prePos);
if(!bRefresh) // 当视窗没有被刷新时,重画原来的橡皮线使其被擦除
pTempLine1->Draw(pDC, dmDrag);
delete pTempLine1;
// 创建临时对象,根据当前位置绘制一条橡皮线
CLine* pTempLine2 = new CLine(m_fp1, curPos);
pTempLine2->Draw(pDC, dmDrag);
delete pTempLine2;
g_pView->ReleaseDC(pDC); // 释放设备环境指针
m_ep1 = curPos; // 将当前位置设置为直线终点,以备下一次鼠标移动时用
}
return 0;
}
// 单击鼠标右键结束多边形输入操作
int CCreateFill::OnRButtonDown(UINT nFlags, const Position& pos)
{POLYP *pp;
// 如果当前的操作步为 1 ,那么要在结束本次操作前擦除上次鼠标移动时绘制的橡皮线
CDC* pDC = g_pView->GetDC(); // 得到设备环境指针
CLine* pTempLine = new CLine(m_fp1,m_ep1);
pTempLine->Draw(pDC, dmDrag);
delete pTempLine;
CLine* pNewLine1 = new CLine(m_fp1,m_ep1);// 根据起点和终点创建直线
pNewLine1->Draw(pDC,dmNormal); // 绘制直线
g_pDoc->m_EntityList.AddTail(pNewLine1); // 将直线指针添加到图元链表
g_pDoc->SetModifiedFlag(TRUE);// set modified flag ;
pp=new POLYP;
pp->p = pos;
pp->next=m_poly;
m_poly=pp;
m_fp1=pos;
pp=m_poly;
while (pp->next)pp=pp->next;
m_ep1=pp->p;
m_points=m_nStep+1;
m_nStep=0;
CLine* pNewLine = new CLine(m_fp1,m_ep1);// 根据起点和终点创建直线
pNewLine->Draw(pDC,dmNormal); // 绘制直线
g_pDoc->m_EntityList.AddTail(pNewLine); // 将直线指针添加到图元链表
g_pDoc->SetModifiedFlag(TRUE);// set modified flag ;
CFill* pTempFill = new CFill(m_poly,m_points);
pTempFill->Draw(pDC, dmDrag);
delete pTempFill; // 释放设备环境指针
g_pView->ReleaseDC(pDC);
return 0;
}
// 调用Cancel 函数取消本次操作
int CCreateFill::Cancel()
{
return 0 ;
}