// homeworkView.cpp : implementation of the CHomeworkView class
//
#include "stdafx.h"
#include "homework.h"
#include "homeworkDoc.h"
#include "homeworkView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHomeworkView
IMPLEMENT_DYNCREATE(CHomeworkView, CView)
BEGIN_MESSAGE_MAP(CHomeworkView, CView)
//{{AFX_MSG_MAP(CHomeworkView)
ON_COMMAND(ID_DRAW_RECTANGLE, OnDrawRectangle)
ON_COMMAND(ID_DRAW_ELLIPSE, OnDrawEllipse)
ON_COMMAND(ID_DRAW_HUATU, OnDrawHuatu)
ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
ON_UPDATE_COMMAND_UI(ID_DRAW_RECTANGLE, OnUpdateDrawRectangle)
ON_UPDATE_COMMAND_UI(ID_DRAW_ELLIPSE, OnUpdateDrawEllipse)
ON_UPDATE_COMMAND_UI(ID_DRAW_HUATU, OnUpdateDrawHuatu)
ON_UPDATE_COMMAND_UI(ID_DRAW_LINE, OnUpdateDrawLine)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_DRAW_ERASER, OnDrawEraser)
ON_COMMAND(IDR_VIEW_CURSOR, OnViewCursor)
ON_WM_SETCURSOR()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CHomeworkView construction/destruction
CHomeworkView::CHomeworkView()
{
// TODO: add construction code here
m_bDrawMode=false;
}
CHomeworkView::~CHomeworkView()
{
}
BOOL CHomeworkView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.lpszClass=AfxRegisterWndClass(
CS_DBLCLKS,AfxGetApp()->LoadStandardCursor(IDC_CROSS),
(HBRUSH)(COLOR_WINDOW+1),
AfxGetApp()->LoadIcon(IDR_MAINFRAME));
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CHomeworkView drawing
void CHomeworkView::OnDraw(CDC* pDC)
{
CHomeworkDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//pDC->FillSolidRect(CRect(0,0,1200,800),RGB(125,125,125));
pDC->FillSolidRect(CRect(20,20,50,40),RGB(255,0,0)); //用颜色填充区域
pDC->FillSolidRect(CRect(55,20,80,40),RGB(0,255,0));
pDC->FillSolidRect(CRect(85,20,110,40),RGB(0,0,255));
pDC->FillSolidRect(CRect(115,20,140,40),RGB(0,0,0));
pDC->FillSolidRect(CRect(145,20,170,40),RGB(125,125,0));
pDC->Ellipse(CRect(50,50,100,100)); //画椭圆
pDC->Rectangle(CRect(50,110,100,160)); //画矩形
pDC->Ellipse(CRect(50,170,100,270)); //画圆
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CHomeworkView printing
BOOL CHomeworkView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CHomeworkView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CHomeworkView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CHomeworkView diagnostics
#ifdef _DEBUG
void CHomeworkView::AssertValid() const
{
CView::AssertValid();
}
void CHomeworkView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CHomeworkDoc* CHomeworkView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHomeworkDoc)));
return (CHomeworkDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CHomeworkView message handlers
void CHomeworkView::OnDrawRectangle()
{
// TODO: Add your command handler code here
ResetAllFlags();
m_bRectangle=true; //实现画矩形的菜单响应函数
}
void CHomeworkView::OnDrawEllipse()
{
// TODO: Add your command handler code here
ResetAllFlags(); //实现画椭圆的菜单响应函数
m_bEllipse=true;
}
void CHomeworkView::OnDrawHuatu()
{
// TODO: Add your command handler code here
ResetAllFlags(); //实现随手画的菜单响应函数
m_bHuatu=true;
}
void CHomeworkView::OnDrawLine()
{
// TODO: Add your command handler code here
ResetAllFlags(); //实现画直线的菜单响应函数
m_bLine=true;
}
void CHomeworkView::OnUpdateDrawRectangle(CCmdUI* pCmdUI) //为菜单添加画矩形的核对标记
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bRectangle);
}
void CHomeworkView::OnUpdateDrawEllipse(CCmdUI* pCmdUI) //为菜单添加画椭圆的核对标记
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bEllipse);
}
void CHomeworkView::OnUpdateDrawHuatu(CCmdUI* pCmdUI) //为菜单添加随手画的核对标记
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bHuatu);
}
void CHomeworkView::OnUpdateDrawLine(CCmdUI* pCmdUI) //为菜单添加画直线的核对标记
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bLine);
}
void CHomeworkView::ResetAllFlags() //设置默认标志变量
{
m_bRectangle=false;
m_bEllipse=false;
m_bHuatu=false;
m_bLine=false;
}
void CHomeworkView::OnLButtonDown(UINT nFlags, CPoint point) //鼠标按下的消息映射函数
{
// TODO: Add your message handler code here and/or call default
//
if(m_bRectangle||m_bEllipse)
m_LastEndPoint=point;
{
m_startPoint=point;
SetCapture();
}
if(m_bHuatu)
{
m_startPoint=point;
}
if(m_bLine)
{
m_startPoint=point;
m_LastEndPoint=point;
SetCapture();
}
::SetCursor(AfxGetApp()->LoadCursor(IDC_DRAW_CURSOR1)); //设置光标
//Invalidate();
CView::OnLButtonDown(nFlags, point);
}
void CHomeworkView::OnLButtonUp(UINT nFlags, CPoint point) //鼠标松开的消息映射函数
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
if(m_bRectangle&&(GetCapture()==this))
{
ReleaseCapture();
CGdiObject*pOldObject=dc.SelectStockObject(NULL_BRUSH);
dc.Rectangle(m_startPoint.x,m_startPoint.y,point.x,point.y);
dc.SelectObject(pOldObject);
}
if(m_bEllipse&&(GetCapture()==this))
{
ReleaseCapture();
CGdiObject*pOldObject=dc.SelectStockObject(NULL_BRUSH);
dc.Ellipse(m_startPoint.x,m_startPoint.y,point.x,point.y);
dc.SelectObject(pOldObject);
}
if(m_bLine&&(GetCapture()==this))
{
ReleaseCapture();
dc.MoveTo(m_startPoint);
dc.LineTo(point);
}
CView::OnLButtonUp(nFlags, point);
}
void CHomeworkView::OnMouseMove(UINT nFlags, CPoint point) //鼠标移动时的消息响应函数
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
if(m_bHuatu&&(nFlags&&MK_LBUTTON))
{
dc.MoveTo(m_startPoint);
dc.LineTo(point);
m_startPoint=point;
}
if(m_bRectangle&&(nFlags&&MK_LBUTTON))
{
CGdiObject*pOldObject=dc.SelectStockObject(NULL_BRUSH);
int nDrawmdoe=dc.GetROP2();
dc.SetROP2(R2_NOTCOPYPEN);
dc.Rectangle(m_LastEndPoint.x,m_LastEndPoint.y,m_startPoint.x,m_startPoint.y);
dc.SetROP2(nDrawmdoe);
dc.Rectangle(m_startPoint.x,m_startPoint.y,point.x,point.y);
dc.SelectObject(pOldObject);
m_LastEndPoint=point;
}
if(m_bEllipse&&(nFlags&&MK_LBUTTON))
{
CGdiObject*pOldObject=dc.SelectStockObject(NULL_BRUSH);
int nDrawmdoe=dc.GetROP2();
dc.SetROP2(R2_NOTCOPYPEN);
dc.Ellipse(m_LastEndPoint.x,m_LastEndPoint.y,m_startPoint.x,m_startPoint.y);
dc.SelectObject(pOldObject);
m_LastEndPoint=point;
}
if(m_bLine&&(nFlags&&MK_LBUTTON))
{
int nDrawmdoe=dc.GetROP2();
dc.SetROP2(R2_NOT);
dc.MoveTo(m_startPoint);
dc.LineTo(m_LastEndPoint);
dc.SetROP2(nDrawmdoe);
dc.MoveTo(m_startPoint);
dc.LineTo(point);
m_LastEndPoint=point;
}
CView::OnMouseMove(nFlags, point);
}
void CHomeworkView::OnDrawEraser() //橡皮擦功能
{
// TODO: Add your command ha
- 1
- 2
- 3
前往页