www.pudn.com > subject_1_113294.rar > PaintView.cpp, change:2002-05-25,size:18469b
// PaintView.cpp : implementation of the CPaintView class
//
#include "stdafx.h"
#include "Paint.h"
#include "PaintDoc.h"
#include "PaintView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPaintView
IMPLEMENT_DYNCREATE(CPaintView, CScrollView)
BEGIN_MESSAGE_MAP(CPaintView, CScrollView)
//{{AFX_MSG_MAP(CPaintView)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_CURSOR, OnCursor)
ON_UPDATE_COMMAND_UI(ID_CURSOR, OnUpdateCursor)
ON_COMMAND(ID_LINE, OnLine)
ON_UPDATE_COMMAND_UI(ID_LINE, OnUpdateLine)
ON_COMMAND(ID_RECT, OnRect)
ON_UPDATE_COMMAND_UI(ID_RECT, OnUpdateRect)
ON_COMMAND(ID_CIRCLE, OnCircle)
ON_UPDATE_COMMAND_UI(ID_CIRCLE, OnUpdateCircle)
ON_COMMAND(ID_ELLISPE, OnEllispe)
ON_UPDATE_COMMAND_UI(ID_ELLISPE, OnUpdateEllispe)
ON_COMMAND(ID_ARC, OnArc)
ON_UPDATE_COMMAND_UI(ID_ARC, OnUpdateArc)
ON_COMMAND(ID_POLYLINE, OnPolyline)
ON_UPDATE_COMMAND_UI(ID_POLYLINE, OnUpdatePolyline)
ON_COMMAND(ID_POLYGON, OnPolygon)
ON_UPDATE_COMMAND_UI(ID_POLYGON, OnUpdatePolygon)
ON_COMMAND(ID_TEXT, OnText)
ON_UPDATE_COMMAND_UI(ID_TEXT, OnUpdateText)
ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_LINEFILL, OnLinefill)
ON_UPDATE_COMMAND_UI(ID_LINEFILL, OnUpdateLinefill)
ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPaintView construction/destruction
CPaintView::CPaintView()
{
// TODO: add construction code here
m_Type=0;
First=CPoint(0,0);
Second=CPoint(0,0);
Third=CPoint(0,0);
IsDraw=false;
ClickCount=0;
PointList=new CPoint[100];
PtCounter=0;m_dlg=NULL;
UndoList=new CPtrList();
TypeList=new int[100];
UndoCounter=0;
}
CPaintView::~CPaintView()
{
}
BOOL CPaintView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CPaintView drawing
void CPaintView::OnDraw(CDC* pDC)
{
CPaintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CLine *Line=new CLine();
CRectangle *Rect;
CCircle *Circle;
CEllisp *Ellispe=new CEllisp();
CArc *Arc;
CPolyline *Pline;
CPolygon *Pgon;
CText *Text;
POSITION Head=pDoc->m_Element->GetHeadPosition();
POSITION Tail=pDoc->m_Element->GetTailPosition();
POSITION Pos;
int i=0;
bool BreakFlag=false;
for(Pos=Head;Pos=Tail;)
{
if(BreakFlag||i>=pDoc->Pointer)
break;
switch(pDoc->m_TypeList[i])
{
case P_LINE:
if(Pos==Tail)
{
Line=(CLine *)pDoc->m_Element->GetAt(Pos);
BreakFlag=true;
}
else
Line=(CLine *)pDoc->m_Element->GetNext(Pos);
Line->DrawItem(pDC);
break;
case P_RECTANGLE:
if(Pos==Tail)
{
Rect=(CRectangle *)pDoc->m_Element->GetAt(Pos);
BreakFlag=true;
}
else
Rect=(CRectangle *)pDoc->m_Element->GetNext(Pos);
Rect->DrawItem(pDC);
break;
case P_CIRCLE:
if(Pos==Tail)
{
Circle=(CCircle *)pDoc->m_Element->GetAt(Pos);
BreakFlag=true;
}
else
Circle=(CCircle *)pDoc->m_Element->GetNext(Pos);
Circle->DrawItem(pDC);
break;
case P_ELLISPE:
if(Pos==Tail)
{
Ellispe=(CEllisp *)pDoc->m_Element->GetAt(Pos);
BreakFlag=true;
}
else
Ellispe=(CEllisp *)pDoc->m_Element->GetNext(Pos);
Ellispe->DrawItem(pDC);
break;
case P_ARC:
if(Pos==Tail)
{
Arc=(CArc *)pDoc->m_Element->GetAt(Pos);
BreakFlag=true;
}
else
Arc=(CArc *)pDoc->m_Element->GetNext(Pos);
Arc->DrawItem(pDC);
break;
case P_POLYLINE:
if(Pos==Tail)
{
Pline=(CPolyline *)pDoc->m_Element->GetAt(Pos);
BreakFlag=true;
}
else
Pline=(CPolyline *)pDoc->m_Element->GetNext(Pos);
Pline->DrawItem(pDC);
break;
case P_POLYGON:
if(Pos==Tail)
{
Pgon=(CPolygon *)pDoc->m_Element->GetAt(Pos);
BreakFlag=true;
}
else
Pgon=(CPolygon *)pDoc->m_Element->GetNext(Pos);
Pgon->DrawItem(pDC);
break;
case P_TEXT:
if(Pos==Tail)
{
Text=(CText *)pDoc->m_Element->GetAt(Pos);
BreakFlag=true;
}
else
Text=(CText *)pDoc->m_Element->GetNext(Pos);
Text->DrawItem(pDC);
break;
}
i++;
}
}
void CPaintView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
if(m_dlg==NULL)
{
m_dlg=new CFillDlg ();
m_dlg->Create(IDD_DIALOG2,this);
m_dlg->ShowWindow(true);
CRect rectView;
GetClientRect(&rectView);
CRect rectDlg;
m_dlg->GetWindowRect(&rectDlg);
int Height=rectDlg.Height();
int Width=rectDlg.Width();
rectDlg.right=rectView.right;
rectDlg.top=(rectView.Height()-rectDlg.Height())/2;
rectDlg.bottom=rectDlg.top+Height;
rectDlg.left=rectView.right-Width;
ClientToScreen(rectDlg);
m_dlg->MoveWindow(rectDlg);
}
}
/////////////////////////////////////////////////////////////////////////////
// CPaintView diagnostics
#ifdef _DEBUG
void CPaintView::AssertValid() const
{
CScrollView::AssertValid();
}
void CPaintView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CPaintDoc* CPaintView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPaintDoc)));
return (CPaintDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CPaintView message handlers
void CPaintView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CPaintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if((m_Type==P_LINE||m_Type==P_RECTANGLE||m_Type==P_CIRCLE
||m_Type==P_ELLISPE||m_Type==P_ARC)
&&ClickCount==0)
{
First=point;
Second=point;
ClickCount++;
IsDraw=true;
}
else if(m_Type==P_ARC&&ClickCount==0)
{
First=point;
Second=point;
Third=point;
ClickCount++;
IsDraw=true;
}
else if(m_Type==P_LINE&&ClickCount==1)
{
IsDraw=false;
ClickCount=0;
CLine *line=new CLine();
line->StartPoint=First;
line->EndPoint=Second;
line->SetLine(m_dlg->m_Style,m_dlg->m_Width,m_dlg->m_LineColor);
pDoc->m_Element->AddTail(line);
pDoc->m_TypeList[pDoc->Pointer]=P_LINE;
pDoc->Pointer++;
pDoc->SetModifiedFlag(true);
Invalidate(true);
}
else if(m_Type==P_RECTANGLE&&ClickCount==1)
{
IsDraw=false;
ClickCount=0;
CRectangle *rect=new CRectangle();
rect->LeftTop=First;
rect->RightBottom=Second;
rect->SetLine(m_dlg->m_Style,m_dlg->m_Width,m_dlg->m_LineColor);
rect->SetFillColor(m_dlg->m_FillColor);
rect->SetFillMode(m_dlg->m_FillMode);
rect->SetIsFill(m_dlg->m_IsFill);
pDoc->m_Element->AddTail(rect);
pDoc->m_TypeList[pDoc->Pointer]=P_RECTANGLE;
pDoc->Pointer++;
pDoc->SetModifiedFlag(true);
Invalidate(true);
}
else if(m_Type==P_CIRCLE&&ClickCount==1)
{
IsDraw=false;
ClickCount=0;
CCircle *circle=new CCircle();
circle->Center=First;
circle->End=Second;
circle->SetLine(m_dlg->m_Style,m_dlg->m_Width,m_dlg->m_LineColor);
circle->SetFillColor(m_dlg->m_FillColor);
circle->SetFillMode(m_dlg->m_FillMode);
circle->SetIsFill(m_dlg->m_IsFill);
pDoc->m_Element->AddTail(circle);
pDoc->m_TypeList[pDoc->Pointer]=P_CIRCLE;
pDoc->Pointer++;
pDoc->SetModifiedFlag(true);
Invalidate(true);
}
else if(m_Type==P_ARC&&ClickCount==1)
{
ClickCount++;
Second=point;
Third=point;
}
else if(m_Type==P_ARC&&ClickCount==2)
{
IsDraw=false;
ClickCount=0;
CArc *arc=new CArc();
arc->Center=First;
arc->End=Second;
arc->End2=Third;
arc->SetLine(m_dlg->m_Style,m_dlg->m_Width,m_dlg->m_LineColor);
pDoc->m_Element->AddTail(arc);
pDoc->m_TypeList[pDoc->Pointer]=P_ARC;
pDoc->Pointer++;
pDoc->SetModifiedFlag(true);
Invalidate(true);
}
else if(m_Type==P_ELLISPE&&ClickCount==1)
{
IsDraw=false;
ClickCount=0;
CEllisp *ellispe=new CEllisp();
ellispe->LeftTop=First;
ellispe->RightBottom=Second;
ellispe->SetLine(m_dlg->m_Style,m_dlg->m_Width,m_dlg->m_LineColor);
ellispe->SetFillColor(m_dlg->m_FillColor);
ellispe->SetFillMode(m_dlg->m_FillMode);
ellispe->SetIsFill(m_dlg->m_IsFill);
pDoc->m_Element->AddTail(ellispe);
pDoc->m_TypeList[pDoc->Pointer]=P_ELLISPE;
pDoc->Pointer++;
pDoc->SetModifiedFlag(true);
Invalidate(true);
}
else if((m_Type==P_POLYLINE||m_Type==P_POLYGON)&&ClickCount==0)
{
CPoint Temp=CPoint(point);
PointList[PtCounter]=Temp;
PtCounter++;
First=point;
Second=point;
ClickCount++;
IsDraw=true;
}
else if((m_Type==P_POLYLINE||m_Type==P_POLYGON)&&ClickCount!=0)
{
CPoint Temp=CPoint(point);
PointList[PtCounter]=Temp;
PtCounter++;
First=Second;
Second=point;
}
else if(m_Type==P_TEXT)
{
CTextDlg dlg;
if(dlg.DoModal()!=IDOK)
return;
if(!dlg.m_Text.Compare(""))
return;
CText *text=new CText();
text->Position=point;
text->Content=dlg.m_Text;
text->SetLine(m_dlg->m_Style,m_dlg->m_Width,m_dlg->m_LineColor);
pDoc->m_Element->AddTail(text);
pDoc->m_TypeList[pDoc->Pointer]=P_TEXT;
pDoc->Pointer++;
pDoc->SetModifiedFlag(true);
Invalidate(true);
}
CScrollView::OnLButtonDown(nFlags, point);
}
void CPaintView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CPaintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(m_Type!=P_CURSOR)
{
HCURSOR cur=::LoadCursor(NULL,IDC_CROSS);
::SetCursor(cur);
}
if(!IsDraw)
return;
CDC *pDC=GetDC();
CPen pen;
pen.CreatePen(m_dlg->m_Style,m_dlg->m_Width,m_dlg->m_LineColor);
pDC->SelectObject(pen);
int oldROP=pDC->SetROP2(R2_NOTXORPEN);
if(m_Type==P_LINE)
{
pDC->MoveTo(First);
pDC->LineTo(Second);
pDC->MoveTo(First);
pDC->LineTo(point);
Second=point;
}
else if(m_Type==P_RECTANGLE)
{
pDC->Rectangle(First.x,First.y,Second.x,Second.y);
pDC->Rectangle(First.x,First.y,point.x,point.y);
Second=point;
}
else if(m_Type==P_CIRCLE)
{
int Radias=(int)sqrt((First.x-Second.x)*(First.x-Second.x)+(First.y-Second.y)*(First.y-Second.y));
pDC->Ellipse(First.x-sqrt(2)*Radias,First.y-sqrt(2)*Radias,First.x+sqrt(2)*Radias,First.y+sqrt(2)*Radias);
Radias=(int)sqrt((First.x-point.x)*(First.x-point.x)+(First.y-point.y)*(First.y-point.y));
pDC->Ellipse(First.x-sqrt(2)*Radias,First.y-sqrt(2)*Radias,First.x+sqrt(2)*Radias,First.y+sqrt(2)*Radias);
Second=point;
}
else if(m_Type==P_ARC&&ClickCount==1)
{
pDC->MoveTo(First);
pDC->LineTo(Second);
pDC->MoveTo(First);
pDC->LineTo(point);
Second=point;
}
else if(m_Type==P_ARC&&ClickCount==2)
{
int Radias=(int)sqrt((First.x-Second.x)*(First.x-Second.x)+(First.y-Second.y)*(First.y-Second.y));
CRect rect=CRect();
rect.left=First.x-Radias;
rect.top=First.y-Radias;
rect.right=First.x+Radias;
rect.bottom=First.y+Radias;
if(Third.operator !=(Second))
pDC->Arc(rect,Second,Third);
pDC->Arc(rect,Second,point);
Third=point;
}
else if(m_Type==P_ELLISPE)
{
pDC->Ellipse(First.x,First.y,Second.x,Second.y);
pDC->Ellipse(First.x,First.y,point.x,point.y);
Second=point;
}
else if((m_Type==P_POLYLINE||m_Type==P_POLYGON))
{
pDC->MoveTo(First);
pDC->LineTo(Second);
pDC->MoveTo(First);
pDC->LineTo(point);
Second=point;
}
pDC->SetROP2(oldROP);
CScrollView::OnMouseMove(nFlags, point);
}
void CPaintView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CPaintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(!IsDraw)
return;
CDC *pDC=GetDC();
CPen pen;
pen.CreatePen(m_dlg->m_Style,m_dlg->m_Width,m_dlg->m_LineColor);
pDC->SelectObject(pen);
int oldROP=pDC->SetROP2(R2_NOTXORPEN);
if(m_Type==P_POLYLINE)
{
IsDraw=false;
ClickCount=0;
CPolyline *pline=new CPolyline();
pline->PointNum=PtCounter;
pline->PointList=PointList;
pline->SetLine(m_dlg->m_Style,m_dlg->m_Width,m_dlg->m_LineColor);
pline->SetFillColor(m_dlg->m_FillColor);
pline->SetFillMode(m_dlg->m_FillMode);
pline->SetIsFill(m_dlg->m_IsFill);
pDoc->m_Element->AddTail(pline);
pDoc->m_TypeList[pDoc->Pointer]=P_POLYLINE;
pDoc->Pointer++;
PtCounter=0;
PointList=new CPoint[100];
pDoc->SetModifiedFlag(true);
Invalidate(true);
}
else if(m_Type==P_POLYGON)
{
IsDraw=false;
ClickCount=0;
CPolygon *pgon=new CPolygon();
pgon->PointNum=PtCounter;
pgon->PointList=PointList;
pgon->SetLine(m_dlg->m_Style,m_dlg->m_Width,m_dlg->m_LineColor);
pgon->SetFillColor(m_dlg->m_FillColor);
pgon->SetFillMode(m_dlg->m_FillMode);
pgon->SetIsFill(m_dlg->m_IsFill);
pDoc->m_Element->AddTail(pgon);
pDoc->m_TypeList[pDoc->Pointer]=P_POLYGON;
pDoc->Pointer++;
PtCounter=0;
PointList=new CPoint[100];
pDoc->SetModifiedFlag(true);
Invalidate(true);
}
else if(m_Type==P_LINE)
{
pDC->MoveTo(First);
pDC->LineTo(Second);
}
else if(m_Type==P_RECTANGLE)
{
pDC->Rectangle(First.x,First.y,Second.x,Second.y);
}
else if(m_Type==P_CIRCLE)
{
int Radias=(int)sqrt((First.x-Second.x)*(First.x-Second.x)+(First.y-Second.y)*(First.y-Second.y));
pDC->Ellipse(First.x-sqrt(2)*Radias,First.y-sqrt(2)*Radias,First.x+sqrt(2)*Radias,First.y+sqrt(2)*Radias);
}
else if(m_Type==P_ARC&&ClickCount==1)
{
pDC->MoveTo(First);
pDC->LineTo(Second);
}
else if(m_Type==P_ARC&&ClickCount==2)
{
int Radias=(int)sqrt((First.x-Second.x)*(First.x-Second.x)+(First.y-Second.y)*(First.y-Second.y));
CRect rect=CRect();
rect.left=First.x-Radias;
rect.top=First.y-Radias;
rect.right=First.x+Radias;
rect.bottom=First.y+Radias;
if(Third.operator !=(Second))
pDC->Arc(rect,Second,Third);
}
else if(m_Type==P_ELLISPE)
{
pDC->Ellipse(First.x,First.y,Second.x,Second.y);
}
First=CPoint(0,0);
Second=CPoint(0,0);
Third=CPoint(0,0);
ClickCount=0;
IsDraw=false;
Invalidate(true);
pDC->SetROP2(oldROP);
CScrollView::OnRButtonDown(nFlags, point);
}
void CPaintView::OnCursor()
{
// TODO: Add your command handler code here
m_Type=P_CURSOR;
}
void CPaintView::OnUpdateCursor(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_Type==P_CURSOR);
}
void CPaintView::OnLine()
{
// TODO: Add your command handler code here
m_Type=P_LINE;
}
void CPaintView::OnUpdateLine(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_Type==P_LINE);
}
void CPaintView::OnRect()
{
// TODO: Add your command handler code here
m_Type=P_RECTANGLE;
}
void CPaintView::OnUpdateRect(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_Type==P_RECTANGLE);
}
void CPaintView::OnCircle()
{
// TODO: Add your command handler code here
m_Type=P_CIRCLE;
}
void CPaintView::OnUpdateCircle(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_Type==P_CIRCLE);
}
void CPaintView::OnEllispe()
{
// TODO: Add your command handler code here
m_Type=P_ELLISPE;
}
void CPaintView::OnUpdateEllispe(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_Type==P_ELLISPE);
}
void CPaintView::OnArc()
{
// TODO: Add your command handler code here
m_Type=P_ARC;
}
void CPaintView::OnUpdateArc(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_Type==P_ARC);
}
void CPaintView::OnPolyline()
{
// TODO: Add your command handler code here
m_Type=P_POLYLINE;
}
void CPaintView::OnUpdatePolyline(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_Type==P_POLYLINE);
}
void CPaintView::OnPolygon()
{
// TODO: Add your command handler code here
m_Type=P_POLYGON;
}
void CPaintView::OnUpdatePolygon(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_Type==P_POLYGON);
}
void CPaintView::OnText()
{
// TODO: Add your command handler code here
m_Type=P_TEXT;
}
void CPaintView::OnUpdateText(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_Type==P_TEXT);
}
void CPaintView::OnEditUndo()
{
// TODO: Add your command handler code here
CPaintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
void *TempPointer=pDoc->m_Element->RemoveTail();
pDoc->m_TypeList[pDoc->Pointer--]=0;
TypeList[UndoCounter++]=pDoc->m_TypeList[pDoc->Pointer];
UndoList->AddTail(TempPointer);
pDoc->SetModifiedFlag(true);
Invalidate(true);
}
void CPaintView::OnEditRedo()
{
// TODO: Add your command handler code here
CPaintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
void *TempPointer=UndoList->RemoveTail();
pDoc->m_Element->AddTail(TempPointer);
TypeList[UndoCounter--]=0;
pDoc->m_TypeList[pDoc->Pointer++]=TypeList[UndoCounter];
pDoc->SetModifiedFlag(true);
Invalidate(true);
}
void CPaintView::OnUpdateEditUndo(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
CPaintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pCmdUI->Enable(pDoc->Pointer>0);
}
void CPaintView::OnLinefill()
{
// TODO: Add your command handler code here
IsShow=!IsShow;
m_dlg->ShowWindow(IsShow);
}
void CPaintView::OnUpdateLinefill(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(IsShow);
}
void CPaintView::OnUpdateEditRedo(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(UndoCounter>0);
}