武汉轻工大学
VC++程序设计大作业
鼠标点击绘制矩形
?? ? ?
院系电气与电子工程学院
班级
学号1204
姓名
日期2023.11.22
1题目
编写MFC下旳单文档程序,通过鼠标点击选择两个点,作为矩形左上角、右下角,然后绘制矩形。(对应12题)
2思绪
首先点击鼠标,选中左上角,然后拖动鼠标,在右下角拿起,这样矩形就就可以实现了。
3程序实现过程
首先建立MFC单文档程序
然后建立类向导
添加鼠标事件
添加CPoint组员变量m_OrigPoint
4关键代码
//RectangleView.cpp:implementationoftheCRectangleViewclass
//
#include"stdafx.h
#includeRectangle.h
#include"RectangleDoc.h"
#include"RectangleView.h
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CRectangleView
IMPLEMENT_DYNCREATE(CRectangleView,CView)
BEGIN_MESSAGE_MAP(CRectangleView,CView)
//{{AFX_MSG_MAP(CRectangleView)
ON_WM_LBUTTONDOWN()
?ON_WM_LBUTTONUP()
?//}}AFX_MSG_MAP
?//Standardprintingcommands
?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()
/////////////////////////////////////////////////////////////////////////////
//CRectangleViewconstruction/destruction
CRectangleView::CRectangleView()
{
?//TODO:addconstructioncodehere
}
CRectangleView::~CRectangleView()
{
}
BOOLCRectangleView::PreCreateWindow(CREATESTRUCTcs)
{
?//TODO:ModifytheWindowclassorstylesherebymodifying
//theCREATESTRUCTcs
returnCView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
//CRectangleViewdrawing
voidCRectangleView::OnDraw(CDC*pDC)
{
CRectangleDoc*pDoc=GetDocument();
?ASSERT_VALID(pDoc);
//TODO:adddrawcodefornativedatahere
}
/////////////////////////////////////////////////////////////////////////////
//CRectangleViewprinting
BOOLCRectangleView::OnPreparePrinting(CPrintInfo*pInfo)
{
?//defaultpreparation
?returnDoPreparePrinting(pInfo);
}
voidCRectan