[游戏学习26] MFC 时间函数 画图形
2014-05-18 17:27
525 查看
>_<:这里第一次介绍MFC的时间函数,功能和Win32里的计时器类似。
>_<:这里还介绍了MFC的图形绘制函数,和Win32有一点区别
>_<:ABC.h
>_<:ABC.cpp
>_<:这里还介绍了MFC的图形绘制函数,和Win32有一点区别
>_<:ABC.h
#define EX 1 //该点左鼠标 #define OH 2 //该点右鼠标 class CMyApp : public CWinApp { public: virtual BOOL InitInstance (); }; class CMainWindow : public CFrameWnd //不是继承CFrameWnd 因此需要在CMainWindow()自己定义窗口类了 { protected: public: CMainWindow (); protected: virtual void PostNcDestroy ();//在程序终止之前销毁CMainWindow对象 afx_msg int OnCreate (LPCREATESTRUCT lpcs); afx_msg void OnTimer (UINT nTimerID); DECLARE_MESSAGE_MAP () };
>_<:ABC.cpp
#include <afxwin.h> #include "ABC.h" #define ID_TIMER_ELLIPSE 1 #define ID_TIMER_RECTANGLE 2 CMyApp myApp; ///////////////////////////////////////////////////////////////////////// // CMyApp member functions BOOL CMyApp::InitInstance () { m_pMainWnd = new CMainWindow; m_pMainWnd->ShowWindow (m_nCmdShow); m_pMainWnd->UpdateWindow (); return TRUE; } ///////////////////////////////////////////////////////////////////////// // CMainWindow message map and member functions BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) ON_WM_CREATE () ON_WM_TIMER () END_MESSAGE_MAP () CMainWindow::CMainWindow () { //初始化游戏 //InitGame(); //注册一个 WNDCLASS 窗口类. CString strWndClass = AfxRegisterWndClass ( CS_DBLCLKS, // Class style(有双击时间发生的窗口类型) AfxGetApp ()->LoadStandardCursor (IDC_ARROW), // Class cursor(加载一个系统光标,也可自己定义) (HBRUSH) (COLOR_3DFACE + 1), // Background brush(每次::BeginPaint时用它清空客户区);COLOR_3DFACE+1是指定窗口具有与按钮对话框一致的背景色和其他一些3D属性;默认为灰亮色 AfxGetApp ()->LoadStandardIcon (IDI_WINLOGO) // Class icon(加载系统图标,也可自己定义) ); //调用CWnd::CreateEx()创建主窗口 //第一个参数表示0个或是多个WS_EX标志组合;2:AfxRegisterWndClass()返回的WNDCLASS名称; //3、标题;4、窗口样式 CreateEx (0, strWndClass, _T ("Timer"), WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX, //WS_THICKFRAME窗口可调大小属性(这里不用) CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, //初始位置和大小,这里用CW_USEDEFAULT让Windows拾取窗口和大小 NULL, NULL); //处理窗口位置和尺寸 CRect rect (0, 0, 352, 352); //理想客户区窗口矩形形状 CalcWindowRect (&rect); //根据分辨率、菜单...计算窗口矩形大小(必须在窗口创建后调用) SetWindowPos (NULL, 0, 0, rect.Width (), rect.Height (), SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW); } //在程序结束之前销毁创建的CMainWindow对象 void CMainWindow::PostNcDestroy () { delete this; } int CMainWindow::OnCreate (LPCREATESTRUCT lpcs) { if (CFrameWnd::OnCreate (lpcs) == -1) return -1; if (!SetTimer (ID_TIMER_ELLIPSE, 100, NULL)|| !SetTimer (ID_TIMER_RECTANGLE, 100, NULL)) { MessageBox (_T ("Error: SetTimer failed")); return -1; } return 0; } void CMainWindow::OnTimer (UINT nTimerID) { CRect rect; GetClientRect (&rect); int x1 = rand () % rect.right; int x2 = rand () % rect.right; int y1 = rand () % rect.bottom; int y2 = rand () % rect.bottom; CClientDC dc (this); CBrush brush (RGB (rand () % 255, rand () % 255, rand () % 255)); CBrush* pOldBrush = dc.SelectObject (&brush); if (nTimerID == ID_TIMER_ELLIPSE) dc.Ellipse (min (x1, x2), min (y1, y2), max (x1, x2), max (y1, y2)); else dc.Rectangle (min (x1, x2), min (y1, y2), max (x1, x2), max (y1, y2)); dc.SelectObject (pOldBrush); }
相关文章推荐
- [学习日记]vb.net里的两个重要的时间函数
- MFC下的图形设别接口CDC类的一些有用的函数
- MFC 函数学习 2011-4-3
- MFC 的几个常用函数,用来计算文件大小,下载速度,转换时间的
- DXUT 框架函数介绍 学习开发游戏UI
- MFC学习笔记之图形保存与重绘
- VC学习资料收集(17):实战VC时间控制函数
- C/C++、VC/MFC、嵌入式、Java/XML、数据库/多线程/网络/图形/数据结构/数学”系列学习精粹
- 最近在学习MFC,常用的一些函数【陆续更新中……】
- linux C 时间函数学习积累
- MFC学习中之图形绘制
- 回归游戏的世界,简单图形编程学习的开始
- Oracle中关于时间函数的学习1
- MFC学习笔记之:MFC最基本动作(如创建窗口,点击取消等)函数的执行顺序
- 什么时间调用 UpdateData() --MFC函数
- 来预览一下我业余时间正在写的一个游戏(HGE+DX9+3DS+CN及输入+网络)[+MFC]
- linux下时间函数的学习
- Sql server 学习笔记---日期和时间函数
- 每天学习一算法系列(3)(设计包含min函数的栈,要求函数min、push以及pop的时间复杂度都是O(1))
- MFC图形函数