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

 

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

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

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

>_<: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 }

 

你可能感兴趣的:(时间函数)