1.首先建立MFC单文档工程lessonMyLine3.
2.在其类视图内选择lessonMyLine3View类,在其中添加消息事件“onLButtonUp”、OnLButtonDown、OnMouseMove。
3.完成上述消息内的代码。
// lessonMyLine3View.cpp : implementation of the ClessonMyLine3View class
//
#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "lessonMyLine3.h"
#endif
#include "lessonMyLine3Doc.h"
#include "lessonMyLine3View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// ClessonMyLine3View
IMPLEMENT_DYNCREATE(ClessonMyLine3View, CView)
BEGIN_MESSAGE_MAP(ClessonMyLine3View, CView)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &ClessonMyLine3View::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
// ClessonMyLine3View construction/destruction
ClessonMyLine3View::ClessonMyLine3View()
: m_MouseDown(false)
, m_point(0)
{
// TODO: add construction code here
}
ClessonMyLine3View::~ClessonMyLine3View()
{
}
BOOL ClessonMyLine3View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
// ClessonMyLine3View drawing
void ClessonMyLine3View::OnDraw(CDC* /*pDC*/)
{
ClessonMyLine3Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: add draw code for native data here
}
// ClessonMyLine3View printing
void ClessonMyLine3View::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL ClessonMyLine3View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void ClessonMyLine3View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void ClessonMyLine3View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void ClessonMyLine3View::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void ClessonMyLine3View::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// ClessonMyLine3View diagnostics
#ifdef _DEBUG
void ClessonMyLine3View::AssertValid() const
{
CView::AssertValid();
}
void ClessonMyLine3View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
ClessonMyLine3Doc* ClessonMyLine3View::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ClessonMyLine3Doc)));
return (ClessonMyLine3Doc*)m_pDocument;
}
#endif //_DEBUG
// ClessonMyLine3View message handlers
void ClessonMyLine3View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_MouseDown=false;
CView::OnLButtonUp(nFlags, point);
}
void ClessonMyLine3View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_point=point;
m_MouseDown=true;
CView::OnLButtonDown(nFlags, point);
}
void ClessonMyLine3View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC ccdc(this);
if(m_MouseDown)
{
ccdc.MoveTo(m_point);
ccdc.LineTo(point);
m_point=point;
}
CView::OnMouseMove(nFlags, point);
}
图如下所示“