实验八MFC鼠标作图编程实验_第1页
实验八MFC鼠标作图编程实验_第2页
实验八MFC鼠标作图编程实验_第3页
实验八MFC鼠标作图编程实验_第4页
实验八MFC鼠标作图编程实验_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验八MFC鼠标作图编程实验电气1002班 李震 学号10053212实验目的熟悉VisualC++6.0开发环境;掌握应用MFC类库编写鼠标绘图程序的方法;掌握MFC环境中绘图函数的使用方法。实验内容创建一个单文档应用程序,实现鼠标的绘图功能。要求:创建一个工具栏,有线段、矩形、椭圆三个按钮;绘图前,选择工具栏上的按钮,确定图形的形状。按下鼠标左键,开始绘图,结合鼠标光标的坐标值,来确定图形的形状和大小,并随着鼠标的移动在屏幕上实时绘制图形,放开鼠标左键,确定最后的图形,绘制在屏幕上。程序清单//DemoView.cpp:implementationoftheCDemoViewclass#include"stdafx.h"#include"Demo.h"#include"DemoDoc.h"#include"DemoView.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endif///////////////////////////////////////////////////////////////////////////////CDemoViewIMPLEMENT_DYNCREATE(CDemoView,CView)BEGIN_MESSAGE_MAP(CDemoView,CView) //{{AFX_MSG_MAP(CDemoView) ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() ON_COMMAND(ID_FILEOP_OPEN,OnFileopOpen) ON_UPDATE_COMMAND_UI(ID_FILEOP_OPEN,OnUpdateFileopOpen) ON_COMMAND(ID_FILEOP_SAVE,OnFileopSave) ON_UPDATE_COMMAND_UI(ID_FILEOP_SAVE,OnUpdateFileopSave) //}}AFX_MSG_MAP //Standardprintingcommands ON_COMMAND_RANGE(ID_DRAW_LINE,ID_DRAW_ELLIPSE,OnDrawType) ON_UPDATE_COMMAND_UI_RANGE(ID_DRAW_LINE,ID_DRAW_ELLIPSE,OnDrawTypeUI) 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()///////////////////////////////////////////////////////////////////////////////CDemoViewconstruction/destructionCDemoView::CDemoView(){ //TODO:addconstructioncodehere m_nPicNum=0; m_nDrawType=0;}CDemoView::~CDemoView(){}BOOLCDemoView::PreCreateWindow(CREATESTRUCT&cs){ //TODO:ModifytheWindowclassorstylesherebymodifying //theCREATESTRUCTcs returnCView::PreCreateWindow(cs);}///////////////////////////////////////////////////////////////////////////////CDemoViewdrawingvoidCDemoView::OnDraw(CDC*pDC){ CDemoDoc*pDoc=GetDocument(); ASSERT_VALID(pDoc); //TODO:adddrawcodefornativedatahere CPenBluePen,*pOldPen; BluePen.CreatePen(PS_SOLID,3,RGB(0,0,255)); pOldPen=pDC->SelectObject(&BluePen); for(inti=0;i<m_nPicNum;i++) { switch(m_Pic[i].m_nDrawType) { case0: pDC->MoveTo(m_Pic[i].m_nX0,m_Pic[i].m_nY0); pDC->LineTo(m_Pic[i].m_nX1,m_Pic[i].m_nY1); break; case1: pDC->Rectangle(m_Pic[i].m_nX0,m_Pic[i].m_nY0, m_Pic[i].m_nX1,m_Pic[i].m_nY1); break; case2: pDC->Ellipse(m_Pic[i].m_nX0,m_Pic[i].m_nY0, m_Pic[i].m_nX1,m_Pic[i].m_nY1); break; } } pDC->SelectObject(pOldPen);}///////////////////////////////////////////////////////////////////////////////CDemoViewprintingBOOLCDemoView::OnPreparePrinting(CPrintInfo*pInfo){ //defaultpreparation returnDoPreparePrinting(pInfo);}voidCDemoView::OnBeginPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/){ //TODO:addextrainitializationbeforeprinting}voidCDemoView::OnEndPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/){ //TODO:addcleanupafterprinting}///////////////////////////////////////////////////////////////////////////////CDemoViewdiagnostics#ifdef_DEBUGvoidCDemoView::AssertValid()const{ CView::AssertValid();}voidCDemoView::Dump(CDumpContext&dc)const{ CView::Dump(dc);}CDemoDoc*CDemoView::GetDocument()//non-debugversionisinline{ ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDemoDoc))); return(CDemoDoc*)m_pDocument;}#endif//_DEBUG///////////////////////////////////////////////////////////////////////////////CDemoViewmessagehandlersvoidCDemoView::OnLButtonDown(UINTnFlags,CPointpoint){ //TODO:Addyourmessagehandlercodehereand/orcalldefault m_nX0=m_nX1=point.x; m_nY0=m_nY1=point.y; CView::OnLButtonDown(nFlags,point);}voidCDemoView::OnMouseMove(UINTnFlags,CPointpoint){ //TODO:Addyourmessagehandlercodehereand/orcalldefault CClientDCdc(this); CPenWhitePen,RedPen,*pOldPen; WhitePen.CreatePen(PS_SOLID,1,RGB(255,255,255)); RedPen.CreatePen(PS_DASH,1,RGB(255,0,0)); if(nFlags&MK_LBUTTON) { switch(m_nDrawType) { case0: pOldPen=dc.SelectObject(&WhitePen); dc.MoveTo(m_nX0,m_nY0); dc.LineTo(m_nX1,m_nY1); m_nX1=point.x; m_nY1=point.y; dc.SelectObject(&RedPen); dc.MoveTo(m_nX0,m_nY0); dc.LineTo(m_nX1,m_nY1); dc.SelectObject(pOldPen); break; case1: pOldPen=dc.SelectObject(&WhitePen); dc.Rectangle(m_nX0,m_nY0,m_nX1,m_nY1); m_nX1=point.x; m_nY1=point.y; dc.SelectObject(&RedPen); dc.Rectangle(m_nX0,m_nY0,m_nX1,m_nY1); dc.SelectObject(pOldPen); break; case2: pOldPen=dc.SelectObject(&WhitePen); dc.Ellipse(m_nX0,m_nY0,m_nX1,m_nY1); m_nX1=point.x; m_nY1=point.y; dc.SelectObject(&RedPen); dc.Ellipse(m_nX0,m_nY0,m_nX1,m_nY1); dc.SelectObject(pOldPen); break; } } CView::OnMouseMove(nFlags,point);}voidCDemoView::OnLButtonUp(UINTnFlags,CPointpoint){ //TODO:Addyourmessagehandlercodehereand/orcalldefault CClientDCdc(this); CPenWhitePen,BluePen,*pOldPen; WhitePen.CreatePen(PS_SOLID,1,RGB(255,255,255)); BluePen.CreatePen(PS_SOLID,3,RGB(0,0,255)); switch(m_nDrawType) { case0: pOldPen=dc.SelectObject(&WhitePen); dc.MoveTo(m_nX0,m_nY0); dc.LineTo(m_nX1,m_nY1); m_nX1=point.x; m_nY1=point.y; dc.SelectObject(&BluePen); dc.MoveTo(m_nX0,m_nY0); dc.LineTo(m_nX1,m_nY1); dc.SelectObject(pOldPen); break; case1: pOldPen=dc.SelectObject(&WhitePen); dc.Rectangle(m_nX0,m_nY0,m_nX1,m_nY1); m_nX1=point.x; m_nY1=point.y; dc.SelectObject(&BluePen); dc.Rectangle(m_nX0,m_nY0,m_nX1,m_nY1); dc.SelectObject(pOldPen); break; case2: pOldPen=dc.SelectObject(&WhitePen); dc.Ellipse(m_nX0,m_nY0,m_nX1,m_nY1); m_nX1=point.x; m_nY1=point.y; dc.SelectObject(&BluePen); dc.Ellipse(m_nX0,m_nY0,m_nX1,m_nY1); dc.SelectObject(pOldPen); break; } m_Pic[m_nPicNum].m_nDrawType=m_nDrawType; m_Pic[m_nPicNum].m_nX0=m_nX0; m_Pic[m_nPicNum].m_nY0=m_nY0; m_Pic[m_nPicNum].m_nX1=m_nX1; m_Pic[m_nPicNum].m_nY1=m_nY1; m_nPicNum++; CView::OnLButtonUp(nFlags,point);}voidCDemoView::OnFileopOpen(){ //TODO:Addyourcommandhandlercodehere CFileDialogdlg(TRUE); if(dlg.DoModal()==IDOK) { CStringszPathName; szPathName=dlg.GetPathName(); CFilefile(szPathName,CFile::modeRead); file.Read(&m_nPicNum,sizeof(int)); for(inti=0;i<m_nPicNum;i++) { file.Read(&m_Pic[i],sizeof(CPic)); } file.Close(); Invalidate(); }}voidCDemoView::OnUpdateFileopOpen(CCmdUI*pCmdUI){ //TODO:AddyourcommandupdateUIhandlercodehere}voidCDemoView::OnFileopSave(){ //TODO:Addyourcommandhandlercodehere CFileDialogdlg(FALSE)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论