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

更新时间:2023-10-19 23:31:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第1章 用MFC开发Windows应用程序

习题

1.安装Visual C++.Net集成开发环境。

2.启动Visual C++ .Net集成开发环境,熟悉其菜单、工具栏和窗口的操作过程。 3.模仿书中的例子,编写“Hello,Visual C++ .Net”程序。

4.编写程序,在窗口中输出两行由字符“*”组成的字符串,中间是“爱国爱校、追求真理、勤奋踏实、艰苦朴素”。

第2章 窗口类和消息处理机制

习题

1.设计一个应用程序,当单击鼠标左键时,窗口中显示“鼠标左键按下”;当单击鼠标右键时,窗口中显示“鼠标右键按下”。

解答:

在View类中添加下列变量:

char flag;

在View类构造函数添加初始化代码: flag=' ';

添加鼠标左键按下和右键按下消息处理函数,并添加下列消息处理代码,函数如下: void CMy2_1View::OnLButtonDown(UINT nFlags, CPoint point) { }

void CMy2_1View::OnRButtonDown(UINT nFlags, CPoint point) {

// TODO: Add your message handler code here and/or call default flag='R';

// TODO: Add your message handler code here and/or call default flag='L'; Invalidate();

CView::OnLButtonDown(nFlags, point);

}

Invalidate();

CView::OnRButtonDown(nFlags, point);

修改OnDraw函数如下:

void CMy2_1View::OnDraw(CDC* pDC) { }

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

在View类中添加下列变量: CRect rect;

bool capture; CPoint old;

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')pDC->TextOut(10,10,_T(\鼠标右键按下\

在View类构造函数添加初始化代码: capture=false;

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

添加鼠标左键按下、左键释放以及鼠标移动消息处理函数,并添加处理代码,函数如下: void CMy2_2View::OnLButtonDown(UINT nFlags, CPoint point) { }

void CMy2_2View::OnMouseMove(UINT nFlags, CPoint point) {

// TODO: Add your message handler code here and/or call default if(capture) {

// TODO: Add your message handler code here and/or call default if(rect.PtInRect(point)) { }

CView::OnLButtonDown(nFlags, point);

capture =true; old=point;

}

}

CSize sz=point-old; rect=rect+sz; old=point; this->Invalidate();

CView::OnMouseMove(nFlags, point);

void CMy2_2View::OnLButtonUp(UINT nFlags, CPoint point) { }

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

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

在View类中添加下列变量:

int x,y; x=0; y=150;

在View类构造函数添加初始化代码:

pDC->Rectangle(rect);

// TODO: Add your message handler code here and/or call default capture =false;

CView::OnLButtonUp(nFlags, point);

添加视图对象创建消息处理函数,并添加处理代码,函数如下: int CMy2_3View::OnCreate(LPCREATESTRUCT lpCreateStruct) { }

添加定时器消息处理函数,并添加处理代码,函数如下: void CMy2_3View::OnTimer(UINT nIDEvent) {

// TODO: Add your message handler code here and/or call default x=x+5; CRect r;

if (CView::OnCreate(lpCreateStruct) == -1)

return -1;

// TODO: Add your specialized creation code here SetTimer(1,200,NULL); //设置定时器 return 0;

}

this->GetClientRect(&r);

if(r.right<=x+135) this->KillTimer(1); this->Invalidate();

CView::OnTimer(nIDEvent);

修改OnDraw函数,添加代码如下: void CMy2_3View::OnDraw(CDC* pDC) { }

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

解答:

在View类中添加下列变量:

int locx,locy;

在View类中添加鼠标移动消息处理函数,函数内容如下: void CMy2_4View::OnMouseMove(UINT nFlags, CPoint 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(\

// TODO: Add your message handler code here and/or call default locx=point.x; locy=point.y; Invalidate();

CView::OnMouseMove(nFlags, point); CMy2_3Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc);

// TODO: add draw code for native data here pDC->Ellipse(x,y,x+135,y+135);

第3章 图形设备接口

习题

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

在OnDraw函数添加代码,函数最终如下所示: void CMy3_1View::OnDraw(CDC* pDC) {

brushHouse.CreateSolidBrush(RGB(125, 50, 0)); // 画房子 pDC->SelectObject(&brushHouse); CPoint m_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

brushSun.CreateSolidBrush(RGB(255, 0, 0)); pDC->SelectObject(&brushSun); pDC->SelectObject(&brushSun); pDC->Ellipse(30,30,100,100);

// 画太阳

brushGrass.CreateSolidBrush(RGB(0, 255, 0)); // 画草地 pDC->SelectObject(&brushGrass); rect.top = 300; pDC->Rectangle(rect);

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, 200, 255)); pOldBrush = pDC->SelectObject(&brushSky); pDC->Rectangle(rect);

// 画天空

本文来源:https://www.bwwdw.com/article/v3wf.html

Top