ImageVerifierCode 换一换
格式:DOCX , 页数:31 ,大小:66.85KB ,
资源ID:2592508      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-2592508.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(VC+Net案例教程和习题解答.docx)为本站会员(b****1)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

VC+Net案例教程和习题解答.docx

1、VC+Net案例教程和习题解答第1章用MFC开发Windows应用程序习题1安装Visual C+.Net集成开发环境。2启动Visual C+ .Net集成开发环境,熟悉其菜单、工具栏和窗口的操作过程。3模仿书中的例子,编写“Hello,Visual C+ .Net”程序。4编写程序,在窗口中输出两行由字符“*”组成的字符串,中间是“爱国爱校、追求真理、勤奋踏实、艰苦朴素”。第2章窗口类和消息处理机制习题1设计一个应用程序,当单击鼠标左键时,窗口中显示“鼠标左键按下”;当单击鼠标右键时,窗口中显示“鼠标右键按下”。解答:在View类中添加下列变量: char flag;在View类构造函数添

2、加初始化代码: flag= ;添加鼠标左键按下和右键按下消息处理函数,并添加下列消息处理代码,函数如下:void CMy2_1View:OnLButtonDown(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call default flag=L; Invalidate(); CView:OnLButtonDown(nFlags, point);void CMy2_1View:OnRButtonDown(UINT nFlags, CPoint point) / TODO: Add yo

3、ur message handler code here and/or call default flag=R; Invalidate(); CView:OnRButtonDown(nFlags, point);修改OnDraw函数如下:void CMy2_1View:OnDraw(CDC* pDC) CMy2_1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); / TODO: add draw code for native data here if(flag=L)pDC-TextOut(10,10,_T(鼠标左键按下); if(flag=R)pD

4、C-TextOut(10,10,_T(鼠标右键按下); 2在窗口上绘制一个正方形,当鼠标单击它时,可以在客户区中任意拖动。解答:在View类中添加下列变量: CRect rect; bool capture; CPoint old; 在View类构造函数添加初始化代码: capture=false; rect=CRect(0,0,100,100); 添加鼠标左键按下、左键释放以及鼠标移动消息处理函数,并添加处理代码,函数如下:void CMy2_2View:OnLButtonDown(UINT nFlags, CPoint point) / TODO: Add your message han

5、dler code here and/or call default if(rect.PtInRect(point) capture =true; old=point; CView:OnLButtonDown(nFlags, point);void CMy2_2View:OnMouseMove(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call default if(capture) CSize sz=point-old; rect=rect+sz; old=point; this-

6、Invalidate(); CView:OnMouseMove(nFlags, point);void CMy2_2View:OnLButtonUp(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call default capture =false; CView:OnLButtonUp(nFlags, point);在OnDraw函数中添加一行代码: pDC-Rectangle(rect);3编写一个程序,在窗口显示一个实心圆,圆自动从窗口左端移动到窗口左端。解答:在View类中添加下

7、列变量: int x,y; 在View类构造函数添加初始化代码: x=0; y=150; 添加视图对象创建消息处理函数,并添加处理代码,函数如下:int CMy2_3View:OnCreate(LPCREATESTRUCT lpCreateStruct) if (CView:OnCreate(lpCreateStruct) = -1) return -1; / TODO: Add your specialized creation code here SetTimer(1,200,NULL); /设置定时器 return 0;添加定时器消息处理函数,并添加处理代码,函数如下:void CMy2

8、_3View:OnTimer(UINT nIDEvent) / TODO: Add your message handler code here and/or call default x=x+5; CRect r; this-GetClientRect(&r); if(r.rightKillTimer(1); this-Invalidate(); CView:OnTimer(nIDEvent);修改OnDraw函数,添加代码如下:void CMy2_3View:OnDraw(CDC* pDC) CMy2_3Doc* pDoc = GetDocument(); ASSERT_VALID(pDo

9、c); / TODO: add draw code for native data here pDC-Ellipse(x,y,x+135,y+135);4编写一个程序,要求鼠标的关标始终指向一个字符串的起始位置,随着鼠标的移动,字符串也跟随移动。解答:在View类中添加下列变量: int locx,locy;在View类中添加鼠标移动消息处理函数,函数内容如下:void CMy2_4View:OnMouseMove(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call default

10、locx=point.x; locy=point.y; Invalidate(); CView:OnMouseMove(nFlags, point);在OnDraw函数添加代码,函数如下:void CMy2_4View:OnDraw(CDC* pDC) CMy2_4Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); / TODO: add draw code for native data here pDC-TextOut(locx,locy,_T(This is a string!);第3章图形设备接口习题1编写一个程序,在窗口客户区绘制一幅包括太阳

11、、蓝天、草地和房子的彩色图画。解答:在OnDraw函数添加代码,函数最终如下所示:void CMy3_1View:OnDraw(CDC* pDC) CMy3_1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); / TODO: add draw code for native data here CBrush *pOldBrush, brushSky, brushGrass, brushHouse,brushSun; CRect rect; GetClientRect(&rect); brushSky.CreateSolidBrush(RGB(127,

12、 200, 255); / 画天空 pOldBrush = pDC-SelectObject(&brushSky); pDC-Rectangle(rect); brushGrass.CreateSolidBrush(RGB(0, 255, 0); / 画草地 pDC-SelectObject(&brushGrass); rect.top = 300; pDC-Rectangle(rect); brushSun.CreateSolidBrush(RGB(255, 0, 0); / 画太阳 pDC-SelectObject(&brushSun); pDC-SelectObject(&brushSu

13、n); pDC-Ellipse(30,30,100,100); brushHouse.CreateSolidBrush(RGB(125, 50, 0); / 画房子 pDC-SelectObject(&brushHouse); CPoint m_pointMountain3; / roof m_pointMountain0 = CPoint(400, 200); m_pointMountain1 = CPoint(500, 150); m_pointMountain2 = CPoint(600, 200); pDC-Polygon(m_pointMountain, 3); pDC-Select

14、Object(pOldBrush); pDC-Rectangle(415,200,585,300); /door pDC-Rectangle(435,230,470,300); /window pDC-Rectangle(505,230,525,250); pDC-Rectangle(525,230,545,250); pDC-Rectangle(505,250,525,270); pDC-Rectangle(525,250,545,270);2在一个窗口中央加载一个位图,当单击鼠标左键时位图向上运动,当单击鼠标右键时位图向下运动。解答:在资源中添加bitmap资源IDB_BITMAP1。在V

15、iew类中添加下列变量: CBitmap bmp; int locx,locy; int width,height;在View类构造函数添加初始化代码: bmp.LoadBitmap(IDB_BITMAP1); BITMAP BM; bmp.GetBitmap(&BM); width=BM.bmWidth; height=BM.bmHeight; locx=200; locy=150;在View类中添加鼠标左键按下、鼠标右键按下以及定时器消息处理函数,三个函数如下:void CMy3_2View:OnRButtonDown(UINT nFlags, CPoint point) / TODO:

16、Add your message handler code here and/or call default KillTimer(1); SetTimer(2,200,NULL); CView:OnRButtonDown(nFlags, point);void CMy3_2View:OnLButtonDown(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call default KillTimer(2); SetTimer(1,200,NULL); CView:OnLButtonDow

17、n(nFlags, point);void CMy3_2View:OnTimer(UINT nIDEvent) / TODO: Add your message handler code here and/or call default if(nIDEvent=1) locy=locy-5; if(nIDEvent=2) locy=locy+5; Invalidate(); CView:OnTimer(nIDEvent);在OnDraw函数添加代码,函数最终如下所示:void CMy3_2View:OnDraw(CDC* pDC) CMy3_2Doc* pDoc = GetDocument()

18、; ASSERT_VALID(pDoc); / TODO: add draw code for native data here CDC MemDC; MemDC.CreateCompatibleDC(NULL); MemDC.SelectObject(&bmp); pDC-BitBlt(locx,locy,width,height,&MemDC,0,0,SRCCOPY);3编一“猫捉老鼠”游戏程序。用户通过键盘上的上、下、左、右方向键控制猫,5只老鼠由定时器控制在窗口中自由活动,可能随时改变行走方向,遇窗口边界则折回。猫碰到老鼠后则该鼠被吃,不再显示。吃掉所有老鼠后游戏结束并显示所用时间。可

19、为游戏增加难度菜单,控制老鼠的移动速度。解答:在资源中添加bitmap资源IDB_BITMAP1、IDB_BITMAP2、IDB_BITMAP3、IDB_BITMAP4分别代表老鼠向右、下、左、上运动的图像;添加代表猫的bitmap资源IDB_BITMAPCAT。在View类中添加下列变量: int status5; /0-right 1-down 2-left 3-up CRect r5; / the rect of mouse int speed5,step5; / the speed of mouse, how many step the mouse will go / before t

20、urning bool catchit5; /a mouse has been catched or not CBitmap bmp1,bmp2,bmp3,bmp4,bmpCat; /mouse bitmap(4 direction) and cat bitmap CRect cat; / the rect of cat double timeUsed; / have used time bool playStatus; /game status在View类构造函数添加初始化代码,函数如下:CMy3_3View:CMy3_3View() / TODO: add construction cod

21、e here bmp1.LoadBitmap(IDB_BITMAP1); bmp2.LoadBitmap(IDB_BITMAP2); bmp3.LoadBitmap(IDB_BITMAP3); bmp4.LoadBitmap(IDB_BITMAP4); BITMAP BM; bmp1.GetBitmap(&BM); r0.left=20; r0.top=20; r0.bottom=r0.top+BM.bmHeight; r0.right=r0.left+BM.bmWidth; status0=0; speed0=10; step0=6; catchit0=false; r1.left=420;

22、 r1.top=20; r1.bottom=r1.top+BM.bmHeight; r1.right=r1.left+BM.bmWidth; status1=1; speed1=6; step1=6; catchit1=false; r2.left=20; r2.top=350; r2.bottom=r2.top+BM.bmHeight; r2.right=r2.left+BM.bmWidth; status2=0; speed2=12; step2=4; catchit2=false; r3.left=220; r3.top=340; r3.bottom=r3.top+BM.bmHeight

23、; r3.right=r3.left+BM.bmWidth; status3=0; speed3=8; step3=6; catchit3=false; r4.left=420; r4.top=330; r4.bottom=r4.top+BM.bmHeight; r4.right=r4.left+BM.bmWidth; status4=3; speed4=10; step4=6; catchit4=false; bmpCat.LoadBitmap(IDB_BITMAPCAT); bmpCat.GetBitmap(&BM); cat.left=225; cat.top=180; cat.bott

24、om=cat.top+BM.bmHeight; cat.right=cat.left+BM.bmWidth; timeUsed=0; playStatus=false;添加“开始”菜单,并为其添加菜单命令处理函数:void CMy3_3View:OnStart() / TODO: Add your command handler code here this-SetTimer(1,100,NULL); playStatus=true;在View类中添加定时器消息处理函数,添加代码后函数如下:void CMy3_3View:OnTimer(UINT nIDEvent) / TODO: Add y

25、our message handler code here and/or call default if(playStatus) for(int i=0;i=500|stepi=0) statusi=newDirection(i,statusi); else ri.left=ri.left+stepi; ri.right=ri.right+stepi; stepi-; break; case 1: if(ri.bottom+stepi=400|stepi=0) statusi=newDirection(i,statusi); else ri.top=ri.top+stepi; ri.botto

26、m=ri.bottom+stepi; stepi-; break; case 2: if(ri.left-stepi=0|stepi=0) statusi=newDirection(i,statusi); else ri.left=ri.left-stepi; ri.right=ri.right-stepi; stepi-; break; case 3: if(ri.top-stepiInvalidate(); if(catchit0&catchit1&catchit2&catchit3&catchit4) KillTimer(1); playStatus=false; CView:OnTim

27、er(nIDEvent);在View类中添加老鼠转向函数,该函数生成新方向以及沿该方向前进步数:int CMy3_3View:newDirection(int i,int d) int nd=d; while(nd=d) nd=rand()%4; stepi=8+rand()%5; return nd;在View类中添加键盘按下消息处理函数,添加代码后函数如下:void CMy3_3View:OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) / TODO: Add your message handler code here and/or call default if(playStatus) switch(nChar) case VK_UP: if(cat.top=18) cat.top=cat.top-8; cat.bottom=cat.bottom-8; break; case VK_DOWN

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2