// Eg9_2View.cpp : implementation of the CEg9_2View class
//

#include "stdafx.h"
#include "Eg9_2.h"

#include "Eg9_2Doc.h"
#include "Eg9_2View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEg9_2View

IMPLEMENT_DYNCREATE(CEg9_2View, CView)

BEGIN_MESSAGE_MAP(CEg9_2View, CView)
	//{{AFX_MSG_MAP(CEg9_2View)
	ON_COMMAND(ID_DRAW_ELLIPSE, OnDrawEllipse)
	ON_UPDATE_COMMAND_UI(ID_DRAW_ELLIPSE, OnUpdateDrawEllipse)
	ON_COMMAND(ID_DRAW_FILL, OnDrawFill)
	ON_UPDATE_COMMAND_UI(ID_DRAW_FILL, OnUpdateDrawFill)
	ON_COMMAND(ID_DRAW_HAND, OnDrawHand)
	ON_UPDATE_COMMAND_UI(ID_DRAW_HAND, OnUpdateDrawHand)
	ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
	ON_UPDATE_COMMAND_UI(ID_DRAW_LINE, OnUpdateDrawLine)
	ON_COMMAND(ID_DRAW_RECT, OnDrawRect)
	ON_UPDATE_COMMAND_UI(ID_DRAW_RECT, OnUpdateDrawRect)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEg9_2View construction/destruction

CEg9_2View::CEg9_2View()
{
	// TODO: add construction code here
	SetFlagsFalse();

}

CEg9_2View::~CEg9_2View()
{
}

BOOL CEg9_2View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CEg9_2View drawing

void CEg9_2View::OnDraw(CDC* pDC)
{
	CEg9_2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CEg9_2View printing

BOOL CEg9_2View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CEg9_2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CEg9_2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CEg9_2View diagnostics

#ifdef _DEBUG
void CEg9_2View::AssertValid() const
{
	CView::AssertValid();
}

void CEg9_2View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CEg9_2Doc* CEg9_2View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEg9_2Doc)));
	return (CEg9_2Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CEg9_2View message handlers



void CEg9_2View::SetFlagsFalse()
{
  m_bDrawFlag=false;
  m_bLineFlag=false;
  m_bEllipseFlag=false;
  m_bRectFlag=false;
  m_bFillFlag=false;
}



void CEg9_2View::OnDrawHand() 
{
	// TODO: Add your command handler code here
	SetFlagsFalse();              // 将所有菜单顶的标志都设置为False
	m_bDrawFlag=true;     //选中了手工绘图菜单，将m_bDrawFlag设置为True
	
}

void CEg9_2View::OnUpdateDrawHand(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here

	//如果当前菜单为手工绘图，则“手工绘图”菜单前加“√”标记
	pCmdUI->SetCheck (m_bDrawFlag);
	
}

void CEg9_2View::OnDrawLine() 
{
	// TODO: Add your command handler code here

	SetFlagsFalse();              // 将所有菜单顶的标志都设置为False
	m_bLineFlag=true;     //选中了画线菜单，将m_bLineFlag设置为True
	
}

void CEg9_2View::OnUpdateDrawLine(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here

	//如果当前菜单为线，则“线”菜单前加“√”标记
	pCmdUI->SetCheck (m_bLineFlag);
	
}

void CEg9_2View::OnDrawRect() 
{
	// TODO: Add your command handler code here

	SetFlagsFalse();              // 将所有菜单顶的标志都设置为False
	m_bRectFlag=true;     //选中了画矩形菜单，将m_bRectFlag设置为True
	
}

void CEg9_2View::OnUpdateDrawRect(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here

	//如果当前菜单为矩形，则“矩形”菜单前加“√”标记
	pCmdUI->SetCheck (	m_bRectFlag);
	
}
void CEg9_2View::OnDrawEllipse() 
{
	// TODO: Add your command handler code here

	SetFlagsFalse();              // 将所有菜单顶的标志都设置为False
	m_bEllipseFlag=true;     //选中了画椭圆菜单，将m_bEllipseFlag设置为True
	
}

void CEg9_2View::OnUpdateDrawEllipse(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here

	//如果当前菜单为椭圆，则“椭圆”菜单前加“√”标记
	pCmdUI->SetCheck (m_bEllipseFlag);
	
}

void CEg9_2View::OnDrawFill() 
{
	// TODO: Add your command handler code here

      SetFlagsFalse();              // 将所有菜单顶的标志都设置为False
	  m_bFillFlag=true;     //选中了填充菜单，将m_bFillFlag设置为True
	
}

void CEg9_2View::OnUpdateDrawFill(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here

	//如果当前菜单为填充，则“填充”菜单前加“√”标记
	pCmdUI->SetCheck (m_bFillFlag);
	
}

void CEg9_2View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    //鼠标被按下时,记录图形的起始点坐标
	   m_ptStart.x=point.x;       
	   m_ptStart.y=point.y;
	   m_ptOld.x=m_ptStart.x;   //用m_ptOld对象来保存先前的鼠标位置
       m_ptOld.y=m_ptStart.y;
	
	CView::OnLButtonDown(nFlags, point);
}

void CEg9_2View::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    //鼠标被松开,记录图形结束点坐标 
	m_ptEnd.x=point.x;         
	m_ptEnd.y=point.y;

    
    //CClientDC类构造函数需要一个指针来指向当前的视图对象，
	//这里采用关键字this来传递该指针,this通常用来指向当前对象。
	//通过这种方法，得到了当前视图的新设备环境pDC。
	CClientDC *pDC=new CClientDC(this);
	
	if(m_bLineFlag)  //如果m_bLineFlag的值为true，则画线　　　　
	{
		pDC->MoveTo (m_ptStart.x,m_ptStart.y);
		pDC->LineTo (m_ptEnd.x,m_ptEnd.y);
	}
	
	if(m_bRectFlag)   //如果m_bRectFlag的值为true，则画矩形
	{
		//使用CClientDC对象方法SelectStockObject()在设备环境中建立一个null brush
		//这样，当矩形覆盖在其它图表上时，后面的图形也能显示出来	
		pDC->SelectStockObject (NULL_BRUSH);
		pDC->Rectangle (m_ptStart.x,m_ptStart.y,m_ptEnd.x,m_ptEnd.y);
	}
	if(m_bEllipseFlag)   //如果m_bEllipseFlag的值为true，则画椭圆
	{
		
		pDC->SelectStockObject (NULL_BRUSH);
		pDC->Ellipse (m_ptStart.x,m_ptStart.y,m_ptEnd.x,m_ptEnd.y);
	}
	if(m_bFillFlag)     //填充
	{
		pDC->SelectStockObject (BLACK_BRUSH);
		pDC->FloodFill (m_ptStart.x,m_ptStart.y,RGB(0,0,0));
	}
	delete pDC;     //删除指针对象

	CView::OnLButtonUp(nFlags, point);
}

void CEg9_2View::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC * pDC=new CClientDC(this);
	
	if((nFlags && MK_LBUTTON)&&m_bDrawFlag)  //对手工绘图菜单的响应　
	{
		pDC->MoveTo (m_ptStart.x,m_ptStart.y);
		pDC->LineTo (point.x,point.y);
		m_ptStart.x=point.x;
		m_ptStart.y=point.y;
	}
    delete pDC;
	CView::OnMouseMove(nFlags, point);
}
