您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > MFC单文档应用程序练2
MFC单文档应用程序1.一个简单的单文档应用程序文件-新建项目-MFC应用程序,出现窗口按“下一步”,出现下面窗口选择单文档点调试—运行,最后运行界面为下图:一个简单的单文档应用程序向导自动建立五个类:向导自动建立的类相应源文件其他源文件相应头文件其他头文件CAboutDlgStdAfx.cppStdAfx.hCXxxAppXxx.cppXxx.rcXxx.hResource.hCXxxViewXxxView.cppXxxView.hCXxxDocXxxDoc.cppXxxDoc.hCMainFrameMainFrame.cppMainFrame.h文档—视结构每个类的主要函数:voidCAboutDlg::DoDataExchange(CDataExchange*pDX)intCMainFrame::OnCreate(LPCREATESTRUCTlpCreateStruct)//创建窗体的工具栏,状态栏等BOOLCMainFrame::PreCreateWindow(CREATESTRUCT&cs)BOOLCXxxApp::InitInstance()voidCXxxApp::OnAppAbout()BOOLCXxxDoc::OnNewDocument()//初始化文档字符数据、字体信息等voidCXxxDoc::Serialize(CArchive&ar)BOOLCXxxView::PreCreateWindow(CREATESTRUCT&cs)voidCXxxView::OnDraw(CDC*pDC)BOOLCXxxView::OnPreparePrinting(CPrintInfo*pInfo)//弹出打印对话框voidCXxxView::OnBeginPrinting(CDC*pDC,CPrintInfo*pInfo)voidCXxxView::OnEndPrinting(CDC*pDC,CPrintInfo*pInfo)CXxxDoc*CXxxView::GetDocument()//non-debugversionisinline创建单文档应用程序的一般步骤:利用AppWizard创建应用程序框架在文档内中声明保存文档数据所需要数据对象完成文档类的OnNewDocument函数,初始化新文档完成文档类的Serialize函数,保持和加载文档数据完成视类的OnInitialUpdate函数,初始化显示完成视类的OnDraw函数,显示当前文档内容在视类中加入可以使用户编辑文档数据所需的代码视(View)显示用户程序的数据及处理用户与文档的交互文档对象永久存储(磁盘文件)MainFrame框架窗口2.简单文本和图形输出建立一个简单的单文档应用程序SimpleTextPicture,通过修改OnDraw函数在视(View)中输出信息或画图。找到函数OnDraw(CDC*/*pDC*/),改为OnDraw(CDC*pDC);OnDraw函数在SimpleTextPicture.cpp文件中,专门用于输出信息或画图。负责视图显示功能(包括打印、显示、预览等)例2.1显示正方形和文本voidCSimpleTextPictureView::OnDraw(CDC*pDC){CSimpleTextPictureDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedataherepDC-TextOut(10,120,_T(HelloWorld!));pDC-Rectangle(20,20,100,100);}在AppWizard生成的OnDraw函数中加入以黑体字给出的两行代码,运行程序,显示如下:例2.2显示彩色图形和文本voidCSimpleTextPictureView::OnDraw(CDC*pDC){CSimpleTextPictureDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahereCPenpen;CBrushbrush;pen.CreatePen(PS_SOLID,3,RGB(250,0,0));brush.CreateSolidBrush(RGB(0,250,0));pDC-SelectObject(pen);pDC-SelectObject(brush);pDC-Ellipse(100,200,300,300);DeleteObject(pen);DeleteObject(brush);CStringstr(_T(HelloWorld!));//用UNICODE编码pDC-SetTextColor(RGB(0,0,250));pDC-TextOut(120,150,str);pDC-SetTextColor(RGB(125,125,125));doublea[]={31.4,888.54};charch[10];CStringstr1;str1.Format(_T(%f),a[0]);pDC-TextOut(20,40,str1);str1.Format(_T(%f),a[1]);pDC-TextOut(20,60,str1);}3.滚动字幕创建一个简单的单文档应用程序Xxx2;[1]在Xxx2View.h中添加代码如下:public:virtual~CXxx2View();#ifdef_DEBUGvirtualvoidAssertValid()const;virtualvoidDump(CDumpContext&dc)const;#endifprotected:private:intn;//定义变量[2]在CXxx2View构造函数中初始化变量nCXxx2View::CXxx2View(){//TODO:addconstructioncodeheren=0;}[3]在Xxx2View.cpp文件中修改OnDraw函数:voidCXxx2View::OnDraw(CDC*pDC){CXxx2Doc*pDoc=GetDocument();ASSERT_VALID(pDoc);if(!pDoc)return;pDC-SetTextColor(RGB(0,0,235));pDC-SetBkMode(TRANSPARENT);CFontfont;font.CreateFont(28,15,0,0,FW_NORMAL,false,false,false,DEFAULT_CHARSET,OUT_DEVICE_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,_T(隶书));pDC-SelectObject(&font);pDC-TextOut(n,100,_T(世上无难事,只要肯登攀!));n=n+20;RECTr;GetClientRect(&r);//获得窗口if(nr.right-r.left)//窗口如果n右坐标减去左坐标n=0;}[4]添加三条消息映射,并加入相应代码,在CXxx2View类上点击“右键-属性”,找到消息按扭,如下图:点击voidCXxx2View::OnLButtonDblClk(UINTnFlags,CPointpoint){//鼠标左双击函数//TODO:Addyourmessagehandlercodehereand/orcalldefaultSetTimer(1,500,NULL);CView::OnLButtonDblClk(nFlags,point);}voidCXxx2View::OnRButtonDblClk(UINTnFlags,CPointpoint){//鼠标右双击函数//TODO:Addyourmessagehandlercodehereand/orcalldefaultKillTimer(1);CView::OnRButtonDblClk(nFlags,point);}voidCXxx2View::OnTimer(UINTnIDEvent){//TODO:Addyourmessagehandlercodehereand/orcalldefaultInvalidate();//刷新该函数的作用是使整个窗口客户区无效。需要重绘窗口CView::OnTimer(nIDEvent);}[5]添加菜单控制与代理点击视图-资源视图,如下图,双击IDR_MAINFRAME,进入菜单编辑。在资源Menu中添加运行子菜单:启动(IDC_MOVE)和停止(IDC_STOP)及其消息映射(右键-添加事件处理程序)。下为“启动”对应的消息映射:并修改代码:voidCXxx2View::OnMove()//启动对应消息{//TODO:AddyourcommandhandlercodehereSetTimer(1,300,NULL);}voidCXxx2View::OnStop()//停止对应的消息{//TODO:AddyourcommandhandlercodehereKillTimer(1);}4.使用计算数据画直方图1.使用AppWixard生成单文档工程Plot2.在PlotDoc.h中类CPlotDoc的定义之前加入类Max的定义:classMax{改为OnMovedoublex1,x2,x3,x4;doubleMax2(double,double);public:Max(double,double,double,double);doubleMax4();};3.在PlotDoc.cpp的文件尾部加入类Max的实现部分://CPlotDoccommandsdoubleMax::Max2(doublea,doubleb){if(a=b)returna;elsereturnb;}Max::Max(doublea,doubleb,doublec,doubled){x1=a;x2=b;x3=c;x4=d;}doubleMax::Max4(){returnMax2(Max2(x1,x2),Max2(x3,x4));}4.为CPlotDoc类增加一个公有的void类型的成员函数Find和一个公有int数据成员数组m_num[5],使用类产生对象并计算它们的最大值,将它们转换成int,存入整数数组m_num。voidCPlotDoc::Find(){Maxa(110.5,120.8,110,68);Maxb(130,256.5,90,200);Maxc(125,406.8,350,330);Maxd(120,356.8,300,280.5);Maxe(102,256.8,120,105);m_num[0]=(int)a.Max4();m_num[1]=(int)b.Max4();m_num[2]=(int)c.Max4();m_num[3]=(int)d.Max4();m_num[4]=(int)e.Max4();}5.为CPlotDoc类的成员函数OnNewDocument增加调用语句准备数据。BOOLCPlotDoc::OnNewDocument(){if(!CDocument::OnNewDocument())returnFALSE;//TODO:addreinitializationcodehere//(SDIdocumentswillreusethisdocument)Find();//增加的语句returnTRUE;}6.在视类中画图写字voidCPlotView::OnDraw(CDC*pDC){CPlotDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedataherepDC-SetMapMode(MM_ISOTROPIC);pDC-SetViewportOrg(50,250
本文标题:MFC单文档应用程序练2
链接地址:https://www.777doc.com/doc-2888488 .html