[游戏学习26] MFC 时间函数 画图形

>_<:这里第一次介绍MFC的时间函数,功能和Win32里的计时器类似。

>_<:这里还介绍了MFC的图形绘制函数,和Win32有一点区别

>_<:ABC.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
14 public:
15 CMainWindow ();
16
17 protected:
18 virtual void PostNcDestroy ();//在程序终止之前销毁CMainWindow对象
19
20 afx_msg int OnCreate (LPCREATESTRUCT lpcs);
21 afx_msg void OnTimer (UINT nTimerID);
22 DECLARE_MESSAGE_MAP ()
23 };

>_<:ABC.cpp


 1 #include <afxwin.h>
2 #include "ABC.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
32
33
34 //注册一个 WNDCLASS 窗口类.
35 CString strWndClass = AfxRegisterWndClass (
36 CS_DBLCLKS, // Class style(有双击时间发生的窗口类型)
37 AfxGetApp ()->LoadStandardCursor (IDC_ARROW), // Class cursor(加载一个系统光标,也可自己定义)
38 (HBRUSH) (COLOR_3DFACE + 1), // Background brush(每次::BeginPaint时用它清空客户区);COLOR_3DFACE+1是指定窗口具有与按钮对话框一致的背景色和其他一些3D属性;默认为灰亮色
39 AfxGetApp ()->LoadStandardIcon (IDI_WINLOGO) // Class icon(加载系统图标,也可自己定义)
40 );
41
42 //调用CWnd::CreateEx()创建主窗口
43 //第一个参数表示0个或是多个WS_EX标志组合;2:AfxRegisterWndClass()返回的WNDCLASS名称;
44 //3、标题;4、窗口样式
45 CreateEx (0, strWndClass, _T ("Timer"),
46 WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX, //WS_THICKFRAME窗口可调大小属性(这里不用)
47 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, //初始位置和大小,这里用CW_USEDEFAULT让Windows拾取窗口和大小
48 NULL, NULL);
49
50 //处理窗口位置和尺寸
51 CRect rect (0, 0, 352, 352); //理想客户区窗口矩形形状
52 CalcWindowRect (&rect); //根据分辨率、菜单...计算窗口矩形大小(必须在窗口创建后调用)
53
54 SetWindowPos (NULL, 0, 0, rect.Width (), rect.Height (),
55 SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);
56 }
57
58 //在程序结束之前销毁创建的CMainWindow对象
59 void CMainWindow::PostNcDestroy ()
60 {
61 delete this;
62 }
63
64
65
66 int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
67 {
68 if (CFrameWnd::OnCreate (lpcs) == -1)
69 return -1;
70
71 if (!SetTimer (ID_TIMER_ELLIPSE, 100, NULL)||
72 !SetTimer (ID_TIMER_RECTANGLE, 100, NULL)) {
73 MessageBox (_T ("Error: SetTimer failed"));
74 return -1;
75 }
76 return 0;
77 }
78
79 void CMainWindow::OnTimer (UINT nTimerID)
80 {
81 CRect rect;
82 GetClientRect (&rect);
83
84 int x1 = rand () % rect.right;
85 int x2 = rand () % rect.right;
86 int y1 = rand () % rect.bottom;
87 int y2 = rand () % rect.bottom;
88
89 CClientDC dc (this);
90 CBrush brush (RGB (rand () % 255, rand () % 255, rand () % 255));
91 CBrush* pOldBrush = dc.SelectObject (&brush);
92 if (nTimerID == ID_TIMER_ELLIPSE)
93 dc.Ellipse (min (x1, x2), min (y1, y2), max (x1, x2),
94 max (y1, y2));
95 else
96 dc.Rectangle (min (x1, x2), min (y1, y2), max (x1, x2),
97 max (y1, y2));
98 dc.SelectObject (pOldBrush);
99 }

[游戏学习26] MFC 时间函数 画图形,布布扣,bubuko.com

时间: 2024-08-06 07:55:34

[游戏学习26] MFC 时间函数 画图形的相关文章

[游戏学习25] MFC 橡皮筋画线效果

>_<:这是给出窗口内外不同情况的处理展示的例子. >_<:MouseCap.h 1 #include<afxwin.h> 2 class CMyApp :public CWinApp 3 { 4 public: 5 virtual BOOL InitInstance(); 6 }; 7 class CMainWindow:public CFrameWnd 8 { 9 protected: 10 BOOL m_bTracking; //标志:鼠标按下为真,否则为假 11

[游戏学习23] MFC 画尺子

>_<:这是个简单的MFC程序,有利于了解MFC的框架结构 >_<:Ruler.h 1 #include<afxwin.h> 2 class CMyApp:public CWinApp 3 { 4 public: 5 virtual BOOL InitInstance(); 6 }; 7 class CMainWindow:public CFrameWnd 8 { 9 public: 10 CMainWindow(); 11 protected: 12 afx_msg

[游戏学习22] MFC 井字棋 双人对战

>_<:太多啦,感觉用英语说的太慢啦,没想到一年做的东西竟然这么多.....接下来要加速啦! >_<:注意这里必须用MFC和前面的Win32不一样啦! >_<:这也是第一次出现MFC游戏,其框架和逻辑的写法和Win32有很大的区别,建议先看一下MFC的基础再理解代码: >_<:TicTac.h 1 #define EX 1 //该点左鼠标 2 #define OH 2 //该点右鼠标 3 4 class CMyApp : public CWinApp 5 {

[游戏学习27] MFC 匀速运动

>_<:理解上一个时间函数的概念和用法,本节的实现也比较简单 >_<:就是简单的绘图+时间函数 >_<: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 //不

[游戏学习28] MFC 时钟

>_<:这是一个时钟小程序 >_<:通过调用获得系统时间然后经过计算得出当前时间,然后再以3个圆环表示时分秒. >_<:TAO_CLOCK.h 1 class CMyApp : public CWinApp 2 { 3 public: 4 virtual BOOL InitInstance (); 5 }; 6 7 class CMainWindow : public CFrameWnd 8 { 9 protected: 10 11 12 int m_nPrevSeco

[游戏学习24] MFC 各种绘图 字体学习

>_<:这里包含字体设置及各种绘图,只要稍微看一下代码就能理解,这里不多介绍 >_<:Hello.h 1 #include<afxwin.h> 2 class CMyApp:public CWinApp 3 { 4 public: 5 virtual BOOL InitInstance(); 6 }; 7 class CMainWindow:public CFrameWnd 8 { 9 public: 10 CMainWindow(); 11 protected: 12

matlab画图形函数 semilogx

matlab画图形函数 semilogx loglog 主要是学习semilogx函数,其中常用的是semilogy函数,即后标为x的是在x轴取对数,为y的是y轴坐标取对数.loglog是x y轴都取对数. 例子, clc;clear;close all;x = 0:.1:10;y = 2*x+3;subplot(211);plot(x,y);grid onsubplot(212);semilogy(x,y);grid on 结果 —————————复习各种matlab图形函数—————————

C++学习笔记26,虚函数

在C++里面,虚函数是一类重要的函数!可以通过虚函数定义不同对象同一行为的不同实现. 举一个简单的例子: #include <iostream> #include <string> using namespace std; class Animal{ protected: string name; public: Animal(const string &s):name(s){ } virtual ~Animal(){ } virtual void speak()const

Mysql学习笔记(五)数学与日期时间函数

原文:Mysql学习笔记(五)数学与日期时间函数 学习内容: 1.数学函数 2.日期时间函数 这些函数都是很常用的函数...在这里进行简单的介绍... 数学函数: mysql> SELECT ABS(-32); //取绝对值函数 -> 32 这个函数可安全地使用于 BIGINT 值. mysql> SELECT SIGN(-32);//判断一个数是正数,负数,还是0..根据实际情况返回指定的数值.. -> -1 mysql> SELECT MOD(234, 10);//取模函