1、第一章实例 1 Hello, Visual C+/*/ program1.cpp/*#include “iostream.h”#include “stdio.h”int main()printf(“Hello, World!n”);coutstr;couta;coutb;coutc;if (ab)max=a;else max=b;if (cmax)max=c;cutkey;if (key=1234)coutkey;if (key=0)coutyear;f1=(int)(year/4)*4=year;f2=(int)(year/100)*100=year;f3=(int)(year/400)*4
2、00=year;if (f1if (key=123)coutkey;switch(key)case 123:coutcj;switch(cj)case 90:cout=a if (k100)coutk;if (9999=k)break;kmax=max(kmax,k);kmin=min(kmin,k);while(1);coutb?a:b);int min(int a,int b)return(an;N=fac(n);Coutn;for (I=1;Ik;if (k=9999)break;if (k%2=0)ax=k;x+;elseby=k;y+;while (1);for (i=0; i(si
3、gn=n)n=-n;do*str+=n%10+0; while (n/=10)0;if (signn;itostr(str,n);coutsi.name;coutsi.chin;coutsi.eng;coutsi.math;coutf;i+;for (j=0;jc.cels;coutf();delete p;实例 36 不同类型数据的比较/*/ program36.cpp/*#include “iostream.h“int comp(int a,int b)coutb?a:b);float comp(float a,float b)coutb?a:b);double comp(double a
4、, double b)coutb?a:b);void main()int i1,i2;float f1,f2;double d1,d2;cini1;cini2;coutf1;cinf2;coutd1;cind2;coutcy.r;coutcy.h;coutcy1.r;coutcy1.h;coutcy2.h;coutemployee:num;cinname;cindepartment:num;cindesign;void print()coutTextOut(x,y, “在窗口上画一个圆形 ”);x=r.right/2;y=r.bottom/2;pDC-Ellipse(x-100,y-100,x
5、+100,y+100);实例 49 应用程序的创建与撤销 (P162163)int CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)/添加下面这一行语句,显示对话框MessageBox(“准备建立应用程序”, “创建程序”,MB_OK|MB_ICONEXCLAMATION);If (CFrameWnd:OnCreate(lpCreateStruct)= -1)return 1;void CMainFrame:OnClose( )/TODO:Add your message handler code here and/or call defau
6、ltMessageBox(“准备销毁程序窗口”, “撤销窗口”,MB_OK|MB_ICONEXCLAMATION);CFrameWnd:OnClose( );实例 50 键盘字符的输入与显示 (P164166)CProgram50Doc:CProgram50Doc( )/TODO: add one-time construction code hereText=”;void CProgram50View:OnChar(UNIT nChar,UINT nRepCnt, UINT nFlags)/TODO:Add your message handler code here and/or call
7、 defaultCProgram50Doc *pDoc=GetDocument( );ASSERT_VALID(pDoc);if (nChar=b)pDoc-Text.Delete(pDoc-Text.GetLength( )-1,1);elsepDoc-Text+=nChar;Invalidate( );Cview:OnChar(nChar, nRepCnt,nFlags);void CProgram50View:OnDraw(CDC *pDC)CProgram50Doc *pDoc=GetDocument( );ASSERT_VALID(pDoc);/TODO: add draw code
8、 for native data hereCRect r;GetClientRect(pDC-DrawText(pDoc-Text,r,DT_WORDBREAK|DT_CENTER);实例 51 区域探测 (P168171)void CProgram51View:OnLButtonUp(UINT nFlags,CPoint point)/TODO: Add your message handler code here and/or call defaultCRect r;GetClientRect(char *s;if (point.xr.right/2)if (point.yr.bottom
9、/2)s=”你现在位于第四区”;elses=”你现在位于第一区”;elseif (point.yr.bottom/2)s=”你现在位于第三区”;elses=”你现在位于第二区”;MessageBox(s,”位置提示”,MB_OK|MB_ICONEXCLAMATION);CView:OnLButtonUp(nFlags,point);void CProgram51View:OnDraw(CDC *pDC)CProgram51Doc *pDoc=GetDocument( );ASSERT_VALID(pDoc);/TODO: add draw code for native data herepD
10、C-TextOut(mPoint.x,mPoint.y,pDoc-Text);void CProgram51View:OnMouseMove(UINT nFlags,CPoint point)/TODO: Add your message handler code here and/or call defaultCProgram51Doc *pDoc=GetDocument( );ASSERT_VALID(pDoc);pDoc-Text=IntToCStr(point.x)+”,”+IntToCStr(point.y);mPoint.x=point.x+20;mPoint.y=point.y;
11、Invalidate( );CView:OnMouseMove(nFlags,point);Cstring CProgram51View:IntToCStr(int Num)int i;CString strnum=”;char c;while (Num0)i=Num%10;c=0;c=c+i;strnum+=(char)c;Num=Num/10;strnum.MakeReverse( );return strnum;实例 52 滚动字幕 (P172173)CProgram52View:CProgram52View()/TO: add construction code herems=0;x=
12、0;CProgram52View:OnCreate(LPCREATESTRUCT lpCreateStruct)if (CView:OnCreate(lpCreateStruct)=-1)/TODO: Add your specialized creation code hereSetTimer(1,10,NULL);return 0;void CProgram52View:OnTimer(UNIT nIDEvent)/TODO: Add your message handler code and/or call default/InvalidateRect(NULL,ture);/Updat
13、eWindow();ms+=10;x=x+1;Invalidate();CView:OnTimer(10);void CProgram52View:OnDraw(CDC *pDC)/TODO: add draw code for native data hereCRect r;GetClientRect(CString s;s.Format(“字幕已运行 %d 毫秒”,ms);if (xr.right-r.left)x=0;pDC-TextOut(x,r.bottom/2,”Visual C+ 6.0 “);pDC-TextOut(x,r.bottom/2+20,s);实例 53 园的移动轨迹
14、 (P175179)void CProgram53View:OnDraw(CDC *pDC)CProgram53Doc*pDoc=GetDocument();ASSERT VALID(pDoc);/TODO: add draw code for native data hereDrawEps();void CPprogram53View:OnInitialUpdate()CScrollView:OnInitialUpdate();CSize sizeTotal;/TODO: calculate the total size of this viewsizeTotal.cx=1000;sizeT
15、otal.cy=800;SetScrollSizes(MM_TEXT,sizeTotal);void CProgram53View:DrawEps()CDC *pDC=GetDC();CRect r;GetClientRect(int x=0,y=0;x=r.right/2;y=r.bottom/2;pDC-Ellipse(x-50,y-50,x+50,y+50);void CProgram53View:OnHScroll(UNIT nSBCode,UNIT nPos,CScrollBar *pScrollBar)/TODO: Add your message handler code her
16、e and/or call defaultDrawEps();CScrollView:OnHScroll(nSBCode,nPos,pScrollBar);void CProgram53View:OnVScroll(UNIT nSBCode,UNIT nPos,CScrollBar *pScrollBar)/TODO: Add your message handler code here and/or call defaultDrawEps();CScrollView:OnVScroll(nSBCode,nPos,pScrollBar);void CProgram53View:OnKeyDow
17、n(UNIT nChar, UINT nRepCnt,UINT nFlags)/TODO: Add your message handler code and/or call defaultswitch(nChar)case VK_NUMPAD7:OnVScroll(SB_LINEUP,0,NULL);OnHScroll(SB_LINELEFT,0,NULL);break;case VK_NUMPAD1:OnVScroll(SB_LINEDOWN,0,NULL);OnHScroll(SB_LINELEFT,0,NULL);break;case VK_NUMPAD8:OnVScroll(SB_L
18、INEUP,0,NULL);break;case VK_NUMPAD2:OnVScroll(SB_LINEDOWN,0,NULL);break;case VK_NUMPAD6:OnHScroll(SB_LINERIGHT,0,NULL);break;case VK_NUMPAD4:OnHScroll(SB_LINELEFT,0,NULL);break;case VK_NUMPAD9:OnVScroll(SB_LINEUP,0,NULL);OnHScroll(SB_LINERIGHT,0,NULL);break;case VK_NUMPAD3:OnVScroll(SB_LINEDOWN,0,NU
19、LL);OnHScroll(SB_LINERIGHT,0,NULL);break;case VK_NUMPAD5:Invalidate();break;CScrollView:OnKeyDown(nChar,nRepCnt,nFlags);实例 54 应用程序的生存周期 (P180182)int CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)MessageBox(“收到 WM_CREATE 消息! “,”提示信息”,MB_OK);If (CFrameWnd:OnCreate(lpCreateStruct)=-1)return 1;void
20、CMainFrame:OnClose()/TODO:Add your message handler code here and/or call defaultMessageBox(“收到 WM_CLOSE 消息! “,”提示信息”,MB_OK);CFrameWnd:OnClose();void CMainFrame:OnDestroy()CFrameWnd:OnDestroy();/TODO: Add your message handler code hereMessageBox(“收到 WM_DESTROY 消息! “,”提示信息”,MB_OK);BOOL CMainFrame:OnQu
21、eryEndSession()if (!CFrameWnd:OnQueryEndSession()return FALSE;/TODO: Add your specialized query end session code hereMessageBox(“收到 WM_QUERYENDSESSION 消息! “,”提示信息 ”,MB_OK);return TURE;void CMainFrame:PostNcDestroy()/TODO:Add your spcialized code here and/or call the base classMessageBox(“正在执行 PostNc
22、Destroy 函数! “,”提示信息”,MB_OK);CFrameWnd:PostNcDestroy();BOOL CMainFrame:DestroyWindow()/TODO:Add your spcialized code here and/or call the base classMessageBox(“正在执行 DestroyWindow 函数! “,”提示信息”,MB_OK);return CFrameWnd:DestroyWindow();实例 56 电子秒表 (P192198)void CProgram56Dlg:OnClose()/TODO:Add your contro
23、l notification handler code hereCDialog:OnCancel();void CProgram56Dlg:OnButstart()/TODO: Add your control notification handler code herem_iMSEL=0;SetTimer(1,10,NULL);m_tStar=Ctime:GetCurentTime();void CProgram56Dlg:OnButend()/TODO:Add your control notification handler code hereKillTimer(1);void CPro
24、gram56Dlg:OnTimer(UNIT nIDEvent)/TODO: Add your message handler code and/or call defaultCTimeSpan tInterval;CTime tCurrentTime;int ih,im,is;tCurrentTime=CTime:GetCurentTime();ih=tCurrentTime.GetHour();im=tCurrentTime.GetMinute();is=tCurrentTime.GetSecond();m_strCurr.Format(“现在时间是 %d 点%d 分%d 秒”ih,im,is);m_iMSEL+=1;tInterval=tCurrentTime-m_tStar;ih=tInterval.GetHours();im=tInterval.GetMinutes();is=tInterval.GetSeconds();if (m_iMSEL99)m_iMSEL=0;m_strClock.Format(“%d:%d:%d.%d”,ih,im,is,m_iMSEL);UpdateData(FALSE);CDialog:OnTimer(nIDEvent);全部结束