在新建的MFC单文档文件中,选择类视图,在其中选择视图类,定义一个新的点类型变量myPoint,并在视图类构造函数中初始化为零。在鼠标单击按下事件内将当前点赋给myPoint,在抬起事件中,实现画矩形。
// lessonMyBrushView.cpp : implementation of the ClessonMyBrushView 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 "lessonMyBrush.h"
#endif
#include "lessonMyBrushDoc.h"
#include "lessonMyBrushView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// ClessonMyBrushView
IMPLEMENT_DYNCREATE(ClessonMyBrushView, CView)
BEGIN_MESSAGE_MAP(ClessonMyBrushView, 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, &ClessonMyBrushView::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
// ClessonMyBrushView construction/destruction
ClessonMyBrushView::ClessonMyBrushView()
: myPoint(0)
{
// TODO: add construction code here
}
ClessonMyBrushView::~ClessonMyBrushView()
{
}
BOOL ClessonMyBrushView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
// ClessonMyBrushView drawing
void ClessonMyBrushView::OnDraw(CDC* /*pDC*/)
{
ClessonMyBrushDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: add draw code for native data here
}
// ClessonMyBrushView printing
void ClessonMyBrushView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL ClessonMyBrushView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void ClessonMyBrushView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void ClessonMyBrushView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void ClessonMyBrushView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void ClessonMyBrushView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// ClessonMyBrushView diagnostics
#ifdef _DEBUG
void ClessonMyBrushView::AssertValid() const
{
CView::AssertValid();
}
void ClessonMyBrushView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
ClessonMyBrushDoc* ClessonMyBrushView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ClessonMyBrushDoc)));
return (ClessonMyBrushDoc*)m_pDocument;
}
#endif //_DEBUG
// ClessonMyBrushView message handlers
void ClessonMyBrushView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//CBitmap bitmap;
CBrush brush(RGB(255,0,0));
CClientDC ccdc(this);
ccdc.FillRect(CRect(myPoint,point),&brush);
CView::OnLButtonUp(nFlags, point);
}
void ClessonMyBrushView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
myPoint=point;
CView::OnLButtonDown(nFlags, point);
}
![](https://img-my.csdn.net/uploads/201206/30/1341061784_9056.png)