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

上传人:b****3 文档编号:18476085 上传时间:2022-12-17 格式:DOCX 页数:25 大小:56.64KB
下载 相关 举报
VC++Net案例教程和习题解答Word格式.docx_第1页
第1页 / 共25页
VC++Net案例教程和习题解答Word格式.docx_第2页
第2页 / 共25页
VC++Net案例教程和习题解答Word格式.docx_第3页
第3页 / 共25页
VC++Net案例教程和习题解答Word格式.docx_第4页
第4页 / 共25页
VC++Net案例教程和习题解答Word格式.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

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

《VC++Net案例教程和习题解答Word格式.docx》由会员分享,可在线阅读,更多相关《VC++Net案例教程和习题解答Word格式.docx(25页珍藏版)》请在冰豆网上搜索。

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

OnRButtonDown(nFlags,point);

修改OnDraw函数如下:

OnDraw(CDC*pDC)

CMy2_1Doc*pDoc=GetDocument();

ASSERT_VALID(pDoc);

adddrawcodefornativedatahere

if(flag=='

)pDC->

TextOut(10,10,"

鼠标左键按下"

);

鼠标右键按下"

2.在窗口上绘制一个正方形,当鼠标单击它时,可以在客户区中任意拖动。

CRectrect;

boolcapture;

CPointold;

capture=false;

rect=CRect(0,0,100,100);

添加鼠标左键按下、左键释放以及鼠标移动消息处理函数,并添加处理代码,函数如下:

voidCMy2_2View:

if(rect.PtInRect(point))

{

capture=true;

old=point;

}

OnMouseMove(UINTnFlags,CPointpoint)

if(capture)

CSizesz=point-old;

rect=rect+sz;

this->

OnMouseMove(nFlags,point);

OnLButtonUp(UINTnFlags,CPointpoint)

capture=false;

OnLButtonUp(nFlags,point);

在OnDraw函数中添加一行代码:

pDC->

Rectangle(rect);

3.编写一个程序,在窗口显示一个实心圆,圆自动从窗口左端移动到窗口左端。

intx,y;

x=0;

y=150;

添加视图对象创建消息处理函数,并添加处理代码,函数如下:

intCMy2_3View:

OnCreate(LPCREATESTRUCTlpCreateStruct)

if(CView:

OnCreate(lpCreateStruct)==-1)

return-1;

Addyourspecializedcreationcodehere

SetTimer(1,200,NULL);

//设置定时器

return0;

添加定时器消息处理函数,并添加处理代码,函数如下:

voidCMy2_3View:

OnTimer(UINTnIDEvent)

x=x+5;

OnTimer(nIDEvent);

修改OnDraw函数,添加代码如下:

CMy2_3Doc*pDoc=GetDocument();

Ellipse(x,y,x+135,y+135);

CRectr;

GetClientRect(&

r);

if(r.right<

=x+135)this->

KillTimer

(1);

4.编写一个程序,要求鼠标的关标始终指向一个字符串的起始位置,随着鼠标的移动,字符串也跟随移动。

intlocx,locy;

在View类中添加鼠标移动消息处理函数,函数内容如下:

voidCMy2_4View:

locx=point.x;

locy=point.y;

Invalidate();

在OnDraw函数添加代码,函数如下:

CMy2_4Doc*pDoc=GetDocument();

TextOut(locx,locy,"

Thisisastring!

"

第3章 图形设备接口

1.编写一个程序,在窗口客户区绘制一幅包括太阳、蓝天、草地和房子的彩色图画。

在OnDraw函数添加代码,函数最终如下所示:

voidCMy3_1View:

CMy3_1Doc*pDoc=GetDocument();

CBrush*pOldBrush,brushSky,brushGrass,brushHouse,brushSun;

GetClientRect(&

rect);

brushSky.CreateSolidBrush(RGB(127,200,255));

//画天空

pOldBrush=pDC->

SelectObject(&

brushSky);

brushGrass.CreateSolidBrush(RGB(0,255,0));

//画草地

brushGrass);

rect.top=300;

brushSun.CreateSolidBrush(RGB(255,0,0));

//画太阳

brushSun);

Ellipse(30,30,100,100);

brushHouse.CreateSolidBrush(RGB(125,50,0));

//画房子

brushHouse);

CPointm_pointMountain[3];

//roof

m_pointMountain[0]=CPoint(400,200);

m_pointMountain[1]=CPoint(500,150);

m_pointMountain[2]=CPoint(600,200);

Polygon(m_pointMountain,3);

SelectObject(pOldBrush);

Rectangle(415,200,585,300);

//door

Rectangle(435,230,470,300);

//window

Rectangle(505,230,525,250);

Rectangle(525,230,545,250);

Rectangle(505,250,525,270);

Rectangle(525,250,545,270);

2.在一个窗口中央加载一个位图,当单击鼠标左键时位图向上运动,当单击鼠标右键时位图向下运动。

在资源中添加bitmap资源IDB_BITMAP1。

CBitmapbmp;

intwidth,height;

bmp.LoadBitmap(IDB_BITMAP1);

BITMAPBM;

bmp.GetBitmap(&

BM);

width=BM.bmWidth;

height=BM.bmHeight;

locx=200;

locy=150;

在View类中添加鼠标左键按下、鼠标右键按下以及定时器消息处理函数,三个函数如下:

voidCMy3_2View:

KillTimer

(1);

SetTimer(2,200,NULL);

KillTimer

(2);

if(nIDEvent==1)

locy=locy-5;

if(nIDEvent==2)

locy=locy+5;

CMy3_2Doc*pDoc=GetDocument();

CDCMemDC;

MemDC.CreateCompatibleDC(NULL);

MemDC.SelectObject(&

bmp);

BitBlt(locx,locy,width,height,&

MemDC,0,0,SRCCOPY);

3.编一“猫捉老鼠”游戏程序。

用户通过键盘上的上、下、左、右方向键控制猫,5只老鼠由定时器控制在窗口中自由活动,可能随时改变行走方向,遇窗口边界则折回。

猫碰到老鼠后则该鼠被吃,不再显示。

吃掉所有老鼠后游戏结束并显示所用时间。

可为游戏增加难度菜单,控制老鼠的移动速度。

在资源中添加bitmap资源IDB_BITMAP1、IDB_BITMAP2、IDB_BITMAP3、IDB_BITMAP4分别代表老鼠向右、下、左、上运动的图像;

添加代表猫的bitmap资源IDB_BITMAPCAT。

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()

addconstructioncodehere

bmp1.LoadBitmap(IDB_BITMAP1);

bmp2.LoadBitmap(IDB_BITMAP2);

bmp3.LoadBitmap(IDB_BITMAP3);

bmp4.LoadBitmap(IDB_BITMAP4);

bmp1.GetBitmap(&

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(&

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()

Addyourcommandhandlercodehere

SetTimer(1,100,NULL);

playStatus=true;

在View类中添加定时器消息处理函数,添加代码后函数如下:

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)

r[i].top=r[i].top+step[i];

r[i].bottom=r[i].bottom+step[i];

case2:

if(r[i].left-step[i]<

=0||step[i]==0)

r[i].left=r[i].left-step[i];

r[i].right=r[i].right-step[i];

case3:

if(r[i].top-step[i]<

r[i].top=r[i].top-step[i];

r[i].bottom=r[i].bottom-step[i];

}

if(r[i].PtInRect(cat.CenterPoint()))catchit[i]=true;

}

timeUsed=timeUsed+0.1;

if(catchit[0]&

&

catchit[1]&

catchit[2]&

catchit[3]&

catchit[4])

KillTimer

(1);

playStatus=false;

在View类中添加老鼠转向函数,该函数生成新方向以及沿该方向前进步数:

intCMy3_3View:

newDirection(inti,intd)

{

intnd=d;

while(nd==d)nd=rand()%4;

step[i]=8+rand()%5;

returnnd;

在View类中添加键盘按下消息处理函数,添加代码后函数如下:

OnKeyDown(UINTnChar,UINTnRepCnt,UINTnFlags)

switch(nChar)

caseVK_UP:

if(cat.top>

=18)

cat.top=cat.top-8;

cat.bottom=cat.bottom-8;

break;

caseVK_DOW

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工作范文 > 行政公文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1