案例教程习题解答

上传人:bin****86 文档编号:44403945 上传时间:2018-06-09 格式:DOC 页数:22 大小:131.50KB
返回 下载 相关 举报
案例教程习题解答_第1页
第1页 / 共22页
案例教程习题解答_第2页
第2页 / 共22页
案例教程习题解答_第3页
第3页 / 共22页
案例教程习题解答_第4页
第4页 / 共22页
案例教程习题解答_第5页
第5页 / 共22页
点击查看更多>>
资源描述

《案例教程习题解答》由会员分享,可在线阅读,更多相关《案例教程习题解答(22页珍藏版)》请在金锄头文库上搜索。

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

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

3、 CPoint point) / TODO: Add your message handler code here and/or call defaultflag=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 hereif(flag=L)pDC-TextOut(10,10,

4、_T(“鼠标左键按下“); if(flag=R)pDC-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

5、) / TODO: Add your message handler code here and/or call defaultif(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 defaultif(capture)CSize sz=point-old;rect=re

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

7、到窗口左端。解答:解答:在 View 类中添加下列变量: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 hereSetTimer(1,200,NULL); /设置定时器return 0;添加定时器消息处理函数,并添加

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

9、VALID(pDoc);/ TODO: add draw code for native data herepDC-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

10、call defaultlocx=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 herepDC-TextOut(locx,locy,_T(“This is a string!“);第第 3 章章 图形设备接口图形设备接口习题习题

11、1编写一个程序,在窗口客户区绘制一幅包括太阳、蓝天、草地和房子的彩色图画。解答:解答:在 OnDraw 函数添加代码,函数最终如下所示:void CMy3_1View:OnDraw(CDC* pDC)CMy3_1Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data hereCBrush *pOldBrush, brushSky, brushGrass, brushHouse,brushSun;CRect rect;GetClientRect(brushSky.CreateSolidB

12、rush(RGB(127, 200, 255);/ 画天空pOldBrush = pDC-SelectObject(pDC-Rectangle(rect);brushGrass.CreateSolidBrush(RGB(0, 255, 0);/ 画草地pDC-SelectObject(rect.top = 300;pDC-Rectangle(rect);brushSun.CreateSolidBrush(RGB(255, 0, 0);/ 画太阳pDC-SelectObject(pDC-SelectObject(pDC-Ellipse(30,30,100,100);brushHouse.Crea

13、teSolidBrush(RGB(125, 50, 0); / 画房子pDC-SelectObject(CPoint m_pointMountain3;/ roofm_pointMountain0 = CPoint(400, 200);m_pointMountain1 = CPoint(500, 150);m_pointMountain2 = CPoint(600, 200);pDC-Polygon(m_pointMountain, 3);pDC-SelectObject(pOldBrush);pDC-Rectangle(415,200,585,300);/doorpDC-Rectangle(

14、435,230,470,300);/windowpDC-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。在 View 类中添加下列变量:CBitmap bmp;int locx,locy;int width,height;在 View 类构造函数添

15、加初始化代码:bmp.LoadBitmap(IDB_BITMAP1);BITMAP BM;bmp.GetBitmap(width=BM.bmWidth;height=BM.bmHeight;locx=200;locy=150;在 View 类中添加鼠标左键按下、鼠标右键按下以及定时器消息处理函数,三个函数如下:void CMy3_2View:OnRButtonDown(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call defaultKillTimer(1);SetTimer(2,2

16、00,NULL);CView:OnRButtonDown(nFlags, point);void CMy3_2View:OnLButtonDown(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call defaultKillTimer(2);SetTimer(1,200,NULL);CView:OnLButtonDown(nFlags, point);void CMy3_2View:OnTimer(UINT nIDEvent) / TODO: Add your message handler code here and/or call defaultif(nIDEvent=1)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号