VC++Net案例教程和习题解答.docx
《VC++Net案例教程和习题解答.docx》由会员分享,可在线阅读,更多相关《VC++Net案例教程和习题解答.docx(25页珍藏版)》请在冰豆网上搜索。
VC++Net案例教程和习题解答
第1章 用MFC开发Windows应用程序
习题
1.安装VisualC++.Net集成开发环境。
2.启动VisualC++.Net集成开发环境,熟悉其菜单、工具栏和窗口的操作过程。
3.模仿书中的例子,编写“Hello,VisualC++.Net”程序。
4.编写程序,在窗口中输出两行由字符“*”组成的字符串,中间是“爱国爱校、追求真理、勤奋踏实、艰苦朴素”。
第2章 窗口类和消息处理机制
习题
1.设计一个应用程序,当单击鼠标左键时,窗口中显示“鼠标左键按下”;当单击鼠标右键时,窗口中显示“鼠标右键按下”。
解答:
在View类中添加下列变量:
charflag;
在View类构造函数添加初始化代码:
flag='';
添加鼠标左键按下和右键按下消息处理函数,并添加下列消息处理代码,函数如下:
voidCMy2_1View:
:
OnLButtonDown(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
flag='L';
this->Invalidate();
CView:
:
OnLButtonDown(nFlags,point);
}
voidCMy2_1View:
:
OnRButtonDown(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
flag='R';
this->Invalidate();
CView:
:
OnRButtonDown(nFlags,point);
}
修改OnDraw函数如下:
voidCMy2_1View:
:
OnDraw(CDC*pDC)
{
CMy2_1Doc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
//TODO:
adddrawcodefornativedatahere
if(flag=='L')pDC->TextOut(10,10,"鼠标左键按下");
if(flag=='R')pDC->TextOut(10,10,"鼠标右键按下");
}
2.在窗口上绘制一个正方形,当鼠标单击它时,可以在客户区中任意拖动。
解答:
在View类中添加下列变量:
CRectrect;
boolcapture;
CPointold;
在View类构造函数添加初始化代码:
capture=false;
rect=CRect(0,0,100,100);
添加鼠标左键按下、左键释放以及鼠标移动消息处理函数,并添加处理代码,函数如下:
voidCMy2_2View:
:
OnLButtonDown(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
if(rect.PtInRect(point))
{
capture=true;
old=point;
}
CView:
:
OnLButtonDown(nFlags,point);
}
voidCMy2_2View:
:
OnMouseMove(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
if(capture)
{
CSizesz=point-old;
rect=rect+sz;
old=point;
this->Invalidate();
}
CView:
:
OnMouseMove(nFlags,point);
}
voidCMy2_2View:
:
OnLButtonUp(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
capture=false;
CView:
:
OnLButtonUp(nFlags,point);
}
在OnDraw函数中添加一行代码:
pDC->Rectangle(rect);
3.编写一个程序,在窗口显示一个实心圆,圆自动从窗口左端移动到窗口左端。
解答:
在View类中添加下列变量:
intx,y;
在View类构造函数添加初始化代码:
x=0;
y=150;
添加视图对象创建消息处理函数,并添加处理代码,函数如下:
intCMy2_3View:
:
OnCreate(LPCREATESTRUCTlpCreateStruct)
{
if(CView:
:
OnCreate(lpCreateStruct)==-1)
return-1;
//TODO:
Addyourspecializedcreationcodehere
SetTimer(1,200,NULL);//设置定时器
return0;
}
添加定时器消息处理函数,并添加处理代码,函数如下:
voidCMy2_3View:
:
OnTimer(UINTnIDEvent)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
x=x+5;
this->Invalidate();
CView:
:
OnTimer(nIDEvent);
}
修改OnDraw函数,添加代码如下:
voidCMy2_3View:
:
OnDraw(CDC*pDC)
{
CMy2_3Doc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
//TODO:
adddrawcodefornativedatahere
pDC->Ellipse(x,y,x+135,y+135);
CRectr;
this->GetClientRect(&r);
if(r.right<=x+135)this->KillTimer
(1);
}
4.编写一个程序,要求鼠标的关标始终指向一个字符串的起始位置,随着鼠标的移动,字符串也跟随移动。
解答:
在View类中添加下列变量:
intlocx,locy;
在View类中添加鼠标移动消息处理函数,函数内容如下:
voidCMy2_4View:
:
OnMouseMove(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
locx=point.x;
locy=point.y;
Invalidate();
CView:
:
OnMouseMove(nFlags,point);
}
在OnDraw函数添加代码,函数如下:
voidCMy2_4View:
:
OnDraw(CDC*pDC)
{
CMy2_4Doc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
//TODO:
adddrawcodefornativedatahere
pDC->TextOut(locx,locy,"Thisisastring!
");
}
第3章 图形设备接口
习题
1.编写一个程序,在窗口客户区绘制一幅包括太阳、蓝天、草地和房子的彩色图画。
解答:
在OnDraw函数添加代码,函数最终如下所示:
voidCMy3_1View:
:
OnDraw(CDC*pDC)
{
CMy3_1Doc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
//TODO:
adddrawcodefornativedatahere
CBrush*pOldBrush,brushSky,brushGrass,brushHouse,brushSun;
CRectrect;
GetClientRect(&rect);
brushSky.CreateSolidBrush(RGB(127,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(&brushSun);
pDC->Ellipse(30,30,100,100);
brushHouse.CreateSolidBrush(RGB(125,50,0));//画房子
pDC->SelectObject(&brushHouse);
CPointm_pointMountain[3];
//roof
m_pointMountain[0]=CPoint(400,200);
m_pointMountain[1]=CPoint(500,150);
m_pointMountain[2]=CPoint(600,200);
pDC->Polygon(m_pointMountain,3);
pDC->SelectObject(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。
在View类中添加下列变量:
CBitmapbmp;
intlocx,locy;
intwidth,height;
在View类构造函数添加初始化代码:
bmp.LoadBitmap(IDB_BITMAP1);
BITMAPBM;
bmp.GetBitmap(&BM);
width=BM.bmWidth;
height=BM.bmHeight;
locx=200;
locy=150;
在View类中添加鼠标左键按下、鼠标右键按下以及定时器消息处理函数,三个函数如下:
voidCMy3_2View:
:
OnRButtonDown(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
KillTimer
(1);
SetTimer(2,200,NULL);
CView:
:
OnRButtonDown(nFlags,point);
}
voidCMy3_2View:
:
OnLButtonDown(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
KillTimer
(2);
SetTimer(1,200,NULL);
CView:
:
OnLButtonDown(nFlags,point);
}
voidCMy3_2View:
:
OnTimer(UINTnIDEvent)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
if(nIDEvent==1)
{
locy=locy-5;
}
if(nIDEvent==2)
{
locy=locy+5;
}
Invalidate();
CView:
:
OnTimer(nIDEvent);
}
在OnDraw函数添加代码,函数最终如下所示:
voidCMy3_2View:
:
OnDraw(CDC*pDC)
{
CMy3_2Doc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
//TODO:
adddrawcodefornativedatahere
CDCMemDC;
MemDC.CreateCompatibleDC(NULL);
MemDC.SelectObject(&bmp);
pDC->BitBlt(locx,locy,width,height,&MemDC,0,0,SRCCOPY);
}
3.编一“猫捉老鼠”游戏程序。
用户通过键盘上的上、下、左、右方向键控制猫,5只老鼠由定时器控制在窗口中自由活动,可能随时改变行走方向,遇窗口边界则折回。
猫碰到老鼠后则该鼠被吃,不再显示。
吃掉所有老鼠后游戏结束并显示所用时间。
可为游戏增加难度菜单,控制老鼠的移动速度。
解答:
在资源中添加bitmap资源IDB_BITMAP1、IDB_BITMAP2、IDB_BITMAP3、IDB_BITMAP4分别代表老鼠向右、下、左、上运动的图像;添加代表猫的bitmap资源IDB_BITMAPCAT。
在View类中添加下列变量:
intstatus[5];//0-right1-down2-left3-up
CRectr[5];//therectofmouse
intspeed[5],step[5];//thespeedofmouse,howmanystepthemousewillgo
//beforeturning
boolcatchit[5];//amousehasbeencatchedornot
CBitmapbmp1,bmp2,bmp3,bmp4,bmpCat;//mousebitmap(4direction)andcatbitmap
CRectcat;//therectofcat
doubletimeUsed;//haveusedtime
boolplayStatus;//gamestatus
在View类构造函数添加初始化代码,函数如下:
CMy3_3View:
:
CMy3_3View()
{
//TODO:
addconstructioncodehere
bmp1.LoadBitmap(IDB_BITMAP1);
bmp2.LoadBitmap(IDB_BITMAP2);
bmp3.LoadBitmap(IDB_BITMAP3);
bmp4.LoadBitmap(IDB_BITMAP4);
BITMAPBM;
bmp1.GetBitmap(&BM);
r[0].left=20;
r[0].top=20;
r[0].bottom=r[0].top+BM.bmHeight;
r[0].right=r[0].left+BM.bmWidth;
status[0]=0;
speed[0]=10;
step[0]=6;
catchit[0]=false;
r[1].left=420;
r[1].top=20;
r[1].bottom=r[1].top+BM.bmHeight;
r[1].right=r[1].left+BM.bmWidth;
status[1]=1;
speed[1]=6;
step[1]=6;
catchit[1]=false;
r[2].left=20;
r[2].top=350;
r[2].bottom=r[2].top+BM.bmHeight;
r[2].right=r[2].left+BM.bmWidth;
status[2]=0;
speed[2]=12;
step[2]=4;
catchit[2]=false;
r[3].left=220;
r[3].top=340;
r[3].bottom=r[3].top+BM.bmHeight;
r[3].right=r[3].left+BM.bmWidth;
status[3]=0;
speed[3]=8;
step[3]=6;
catchit[3]=false;
r[4].left=420;
r[4].top=330;
r[4].bottom=r[4].top+BM.bmHeight;
r[4].right=r[4].left+BM.bmWidth;
status[4]=3;
speed[4]=10;
step[4]=6;
catchit[4]=false;
bmpCat.LoadBitmap(IDB_BITMAPCAT);
bmpCat.GetBitmap(&BM);
cat.left=225;
cat.top=180;
cat.bottom=cat.top+BM.bmHeight;
cat.right=cat.left+BM.bmWidth;
timeUsed=0;
playStatus=false;
}
添加“开始”菜单,并为其添加菜单命令处理函数:
voidCMy3_3View:
:
OnStart()
{
//TODO:
Addyourcommandhandlercodehere
this->SetTimer(1,100,NULL);
playStatus=true;
}
在View类中添加定时器消息处理函数,添加代码后函数如下:
voidCMy3_3View:
:
OnTimer(UINTnIDEvent)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
if(playStatus)
{
for(inti=0;i<5;i++)
{
switch(status[i])
{
case0:
if(r[i].right+step[i]>=500||step[i]==0)
{
status[i]=newDirection(i,status[i]);
}else{
r[i].left=r[i].left+step[i];
r[i].right=r[i].right+step[i];
step[i]--;
}
break;
case1:
if(r[i].bottom+step[i]>=400||step[i]==0)
{
status[i]=newDirection(i,status[i]);
}else{
r[i].top=r[i].top+step[i];
r[i].bottom=r[i].bottom+step[i];
step[i]--;
}
break;
case2:
if(r[i].left-step[i]<=0||step[i]==0)
{
status[i]=newDirection(i,status[i]);
}else{
r[i].left=r[i].left-step[i];
r[i].right=r[i].right-step[i];
step[i]--;
}
break;
case3:
if(r[i].top-step[i]<=0||step[i]==0)
{
status[i]=newDirection(i,status[i]);
}else{
r[i].top=r[i].top-step[i];
r[i].bottom=r[i].bottom-step[i];
step[i]--;
}
break;
}
if(r[i].PtInRect(cat.CenterPoint()))catchit[i]=true;
}
timeUsed=timeUsed+0.1;
this->Invalidate();
}
if(catchit[0]&&catchit[1]&&catchit[2]&&catchit[3]&&catchit[4])
{
KillTimer
(1);
playStatus=false;
}
CView:
:
OnTimer(nIDEvent);
}
在View类中添加老鼠转向函数,该函数生成新方向以及沿该方向前进步数:
intCMy3_3View:
:
newDirection(inti,intd)
{
intnd=d;
while(nd==d)nd=rand()%4;
step[i]=8+rand()%5;
returnnd;
}
在View类中添加键盘按下消息处理函数,添加代码后函数如下:
voidCMy3_3View:
:
OnKeyDown(UINTnChar,UINTnRepCnt,UINTnFlags)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
if(playStatus)
{
switch(nChar)
{
caseVK_UP:
if(cat.top>=18)
{
cat.top=cat.top-8;
cat.bottom=cat.bottom-8;
}
break;
caseVK_DOW