>_<:理解上一个时间函数的概念和用法,本节的实现也比较简单
>_<:就是简单的绘图+时间函数
>_<:TicTac.h
1 #define EX 1 //该点左鼠标 2 #define OH 2 //该点右鼠标 3 4 class CMyApp : public CWinApp 5 { 6 public: 7 virtual BOOL InitInstance (); 8 }; 9 10 class CMainWindow : public CFrameWnd //不是继承CFrameWnd 因此需要在CMainWindow()自己定义窗口类了 11 { 12 protected: 13 CPoint pos,killpos; 14 int vx,vy,ax,ay; 15 int killvx,killvy,killax,killay; 16 int num,Length; 17 18 public: 19 CMainWindow (); 20 21 protected: 22 virtual void PostNcDestroy ();//在程序终止之前销毁CMainWindow对象 23 afx_msg int OnCreate (LPCREATESTRUCT lpcs); 24 afx_msg void OnTimer (UINT nTimerID); 25 DECLARE_MESSAGE_MAP () 26 };
>_<:TicTac.cpp
1 #include <afxwin.h> 2 #include "TicTac.h" 3 #define ID_TIMER_ELLIPSE 1 4 #define ID_TIMER_RECTANGLE 2 5 6 CMyApp myApp; 7 8 ///////////////////////////////////////////////////////////////////////// 9 // CMyApp member functions 10 11 BOOL CMyApp::InitInstance () 12 { 13 m_pMainWnd = new CMainWindow; 14 m_pMainWnd->ShowWindow (m_nCmdShow); 15 m_pMainWnd->UpdateWindow (); 16 return TRUE; 17 } 18 19 ///////////////////////////////////////////////////////////////////////// 20 // CMainWindow message map and member functions 21 22 BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) 23 ON_WM_CREATE () 24 ON_WM_TIMER () 25 END_MESSAGE_MAP () 26 27 CMainWindow::CMainWindow () 28 { 29 //初始化游戏 30 //InitGame(); 31 pos=CPoint(0,0); 32 vx=killvx=20; 33 vy=killvy=20 ; 34 ax=ay=killax=killay=0; 35 Length=6; 36 num=0; 37 38 39 40 //注册一个 WNDCLASS 窗口类. 41 CString strWndClass = AfxRegisterWndClass ( 42 CS_DBLCLKS, // Class style(有双击时间发生的窗口类型) 43 AfxGetApp ()->LoadStandardCursor (IDC_ARROW), // Class cursor(加载一个系统光标,也可自己定义) 44 (HBRUSH) (COLOR_3DFACE + 1), // Background brush(每次::BeginPaint时用它清空客户区);COLOR_3DFACE+1是指定窗口具有与按钮对话框一致的背景色和其他一些3D属性;默认为灰亮色 45 AfxGetApp ()->LoadStandardIcon (IDI_WINLOGO) // Class icon(加载系统图标,也可自己定义) 46 ); 47 48 //调用CWnd::CreateEx()创建主窗口 49 //第一个参数表示0个或是多个WS_EX标志组合;2:AfxRegisterWndClass()返回的WNDCLASS名称; 50 //3、标题;4、窗口样式 51 CreateEx (0, strWndClass, _T ("Timer"), 52 WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX, //WS_THICKFRAME窗口可调大小属性(这里不用) 53 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, //初始位置和大小,这里用CW_USEDEFAULT让Windows拾取窗口和大小 54 NULL, NULL); 55 56 //处理窗口位置和尺寸 57 CRect rect (0, 0, 840, 400); //理想客户区窗口矩形形状 58 CalcWindowRect (&rect); //根据分辨率、菜单...计算窗口矩形大小(必须在窗口创建后调用) 59 60 SetWindowPos (NULL, 0, 0, rect.Width (), rect.Height (), 61 SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW); 62 } 63 64 //在程序结束之前销毁创建的CMainWindow对象 65 void CMainWindow::PostNcDestroy () 66 { 67 delete this; 68 } 69 70 71 72 int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)//回掉函数 73 { 74 if (CFrameWnd::OnCreate (lpcs) == -1) 75 return -1; 76 77 if (!SetTimer (ID_TIMER_ELLIPSE, 100, NULL)) { 78 MessageBox (_T ("Error: SetTimer failed")); 79 return -1; 80 } 81 return 0; 82 } 83 84 void CMainWindow::OnTimer (UINT nTimerID) 85 { 86 CRect rect; 87 GetClientRect (&rect); 88 89 90 pos.x+=vx; 91 if(pos.x>rect.right-30){ 92 pos.x=rect.right-30; 93 vx=-vx; 94 } 95 if(pos.x<0){ 96 pos.x=0; 97 vx=-vx; 98 } 99 pos.y+=vy; 100 if(pos.y>rect.bottom-30){ 101 pos.y=rect.bottom-30; 102 vy=-vy; 103 } 104 if(pos.y<0){ 105 pos.y=0; 106 vy=-vy; 107 } 108 109 110 if(num++==0)killpos=pos; 111 if(num>=Length){ 112 killpos.x+=killvx; 113 if(killpos.x>rect.right-30){ 114 killpos.x=rect.right-30; 115 killvx=-killvx; 116 } 117 if(killpos.x<0){ 118 killpos.x=0; 119 killvx=-killvx; 120 } 121 killpos.y+=killvy; 122 if(killpos.y>rect.bottom-30){ 123 killpos.y=rect.bottom-30; 124 killvy=-killvy; 125 } 126 if(killpos.y<0){ 127 killpos.y=0; 128 killvy=-killvy; 129 }} 130 131 CClientDC dc (this); 132 133 134 CPen pen(PS_NULL,1,RGB(0,0,0)); 135 dc.SelectObject(&pen); 136 if(num==1)dc.Rectangle(rect); 137 dc.Ellipse(killpos.x,killpos.y,killpos.x+30,killpos.y+30); 138 ::DeleteObject(&pen); 139 140 CBrush brush(RGB(10,10,240)); 141 dc.SelectObject (&brush); 142 dc.Ellipse(pos.x,pos.y,pos.x+30,pos.y+30); 143 ::DeleteObject(&brush); 144 145 CBrush brush1(RGB(255,0,0)); 146 dc.SelectObject (&brush1); 147 dc.Ellipse(pos.x+5,pos.y+5,pos.x+25,pos.y+25); 148 ::DeleteObject(&brush1); 149 }