《C++ MFC 实验报告.docx》由会员分享,可在线阅读,更多相关《C++ MFC 实验报告.docx(29页珍藏版)》请在第一文库网上搜索。
1、MFC实验报告实验一1.1 显示一首诗。在VieW类的OndraW函数中添加代码如下:voidCEx11View:OnDraw(CDC*pDC)(CEx11Doc*pDoc=GetDocumentO;ASSERT_VA1ID(PDOC);if(!pDoc)return;/TODO:在此处为本机数据添加绘制代码CStrings4;s0=人生得意须尽欢,莫使金樽空对月。;s1=两岸青山相对出,孤帆一片日边来。s2=孤帆远影碧空尽,惟见长江天际流。s3=飞流直下”千尺,疑是银河落九天。”;fbr(inti=0;i1extOut(100,100+20*i,si);)1.2 画一个坐标系,画函数y=si
2、n(x),x(-2,2r)的图像。在view类的OndraW函数中添加代码如下:voidCEx12View:OnDraw(CDC*pDC)(CEx12Doc*pDoc=GetDocumentO;ASSERT_VA1ID(pDoc);if(!pDoc)return;/TODO:在此处为本机数据添加绘制代码pDC-SetMapMode(MM_TEXT);pDC-SetWindowOrg(CPoint(-370,-250);pDC-Move1b(0,0);pDC-1ineTo(420,0);pDC-Move1(0,0);pDC-1ineTo(0,-220);pDC-Move1b(0,0);pDC-1
3、ineTo(-420,0);pDC-Move1(0,0);pDC-1ineTo(0,220);/pDC-1extOut(50J0,1);CStringArrayp;p.SetSize(8,-1);p0=-1;p1=-2;N2=-3;p3=-4;p4=-5;p5=6;p6=-7;p7=-8;for(inti=1;iMoveTo(i*(-50),-10);pDC-1ineTo(i*(-50),+0);pDC-1extut(i*(-5O),1O,pi-I);)CStringArrays;s.SetSize(8,-1);sO=(,is1=2s2=,3s3=,4s4=5s5=6s6=7s7=8;fbr(
4、inti=1;iMoveTo(i*50,-10);pDC-1ineTo(i*50,+10);pDC-1extOut(i*50,1O,si-1);)intx,yj;f1oata;pDC-Move1b(0,0);for(j=0y1ineTo(x,y);pDC-Move1(0,0);for(j=OJ=-315;j-)=j;a=j;y=-50*sin(a50);pDC-1ineTo(x,y);)1.3 显示资源中的一幅位图。WM_ERASEBKGND1.3.1 在资源视图中找到Bitm叩右键添加一个位图ID_BITMAP1OnEraseBkgnV并添加函数1.3.2 在函数OnEraSeBkgnd中添
5、加代码如F:BOO1CEx13View:0nEraseBkgnd(CDC*pDC)(/TODO:在此添加消息处理程序代码和/或调用默认值CBitmapbitmap;bitmap.1oadBim叩(IDB_BITMAPI);BITMAPbmp;bitmap.GetBitmap(&binp);CDCdcCompatib1e;dcCompatib1e.CreateCompatib1eDC(pDC);dcCompatib1e.Se1ectObject(BitB1t(0,0,rect.Width(),rect.Height(),&dcCompatib1e,0,0,SRCCOPY);原位图pDC-Stre
6、tchB1t(0,0,rect.Width(),rect.Height(),AdcCompatib1e,可对位图进行伸缩0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);可对位图进行伸缩returnTRUE;retumCView:OnEraseBkgnd(pDC);1.4 GDI绘图之鼠标绘图.1.4.1 画笔WM1buttondowhI1.4.1.1 在VieW类中添加变量CStringm_ptPrev;1.4.1.2 在VieW类中添加消息响应函数On1BUUonDOWn1voidCEx141View:On1BUnOnDoWn(UINTnF1ags,CPointp
7、oint)/TODO:Addyourmessagehand1ercodehereand/orca11defau1tSetCapture():m-ptPrev=point;CView:On1ButtonDown(nF1ags,point);1.4.1.3 在VieW类中添加消息响应函数On1BuUonUpvoidCEx141View:On1ButtonUp(UINTnF1ags,CPointpoint)/TODO:Addyourmessagehand1ercodehereand/orca11defau1tif(GetCaptureO!=this)return;CC1ientDCdc(this);
8、dc.MoveTo(m_ptPrev);dc.1ineTo(point);Re1easeCaptureO;CView:On1ButtonUp(nF1ags,point);1.4.1.4 在VieW类中添加消息响应函数OnMOUSeMOVevoidCEx141View:OnMOUSeMoVe(U1NTnF1ags,CPointpoint)/TODO:Addyourmessagehand1ercodehereand/orca11defau1tif(GetCapture()!=this)return;CC1ientDCdc(this);dc.MoveTo(n-ptPrev);dc.1ineTo(po
9、int);m-ptPrev=point;CView:OnMouseMove(nF1ags,point);1.4.2绘制直线在VieW类中添加变量CPointm_bMouseDown;CPointm_p(Start;CPointm-pt1d:CPointm_hCross;CPointn-hArrow;CPointnF1ags;CPointpoint;voidCEx142View:On1Bu11onDown(UINTnF1ags,CPointpoint)(/TODO:ddyourmessagehand1ercodehereand/orca11defau1t/TODO:ddyourmessageha
10、nd1ercodehereand/orca11defau1tmbMouseDown=true;mptStart=point;mpt1d=point;SetCapture();CRectrect;GetC1ientRect(&rect);C1ientToScreen(&rect);C1ipCursor(&rect);SetCursor(mhCross);设置鼠标形状为十字形CView:IOn1ButtonDown(nF1ags,point);voidCEx142View:IOn1ButtonUp(UINTnF1ags,CPointpoint)(/TODO:ddyourmessagehand1er
11、codehereand/orca11defau1tif(mbMouseDown)C1ipCursor(NU11);CC1ientDCde(this);dc.SetR0P2(R2_NOT);de.MoveTo(mptStart);这两行代码擦除从起点(鼠标按下点)到de.1ineTo(m_pt01d);上次鼠标移动到的位置之间的临时线dc.SetR0P2(R21OPYPEN);de.MoveTo(mptStart);这两行代码从起点到鼠标当前位置画线de.1ineTo(point);/CEx142Doc*pDoc=GetDocumentO;得到文档类指针pDoc-m1ine=newC1ine(m
12、ptStart,point);创建C1ine对象pDoc-m1ine1ist.ddTai1(pDoc-m1ine);将P1ine加入到链表中pDoc-SetModiFiedF1ag();/Markthedocumentashavingbeenmodified,for/purposesofconfirmingFi1eC1ose./pDoc-Update1IViews(this,O1,p1ine);SetCursor(mhArrow);设置鼠标形状为标准箭头形)CView:On1ButtonUp(nF1ags,point);)1.4.3画矩形voidCEx143View:On1Bu11onDown
13、(UINTnF1ags,CPointpoint)(/TODO:在此添加消息处理程序代码和/或调用默认值mbMouseDown=true;mptStart=point;mpt1d=point;SetCaptureO;CRectrect;GetC1ientRect(Srect);C1ientToScreen(&rect);C1ipCursor(Arect);/SetCursor(mhCross);CView:IOn1ButtonDown(nF1ags,point);)voidCEx143View:IOn1ButtonUp(UINTnF1ags,CPointpoint)(/TODO:在此添加消息处理
14、程序代码和/或调用默认值if(m_bMouseDown)C1ipCursor(NU11);CC1ientDCde(this);dc.SetR0P2(R2_NOT);/dc.MoveTo(mptStart);这两行代码擦除从起点(鼠标按下点)到/dc.1ineTo(mpt1d);de.Rectang1e(mptStart.x,mptStart.y,mpt1d.x,mpt1d.y);上次鼠标移动到的位置之间的临时线dc.SetR0P2(R2/0PYPEN);/dc.MoveTo(m_ptStart);这两行代码从起点到鼠标当前位置画线/dc.1ineTo(point);/de.Rectang1e(mptStart.x,m_ptStart.y,point,x,point,y);/CEx142Doc*pDoc=GetDocument();得到文档类指针/pDoc-m1ine=