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

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

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

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

>_<: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 + ), // Background brush(每次::BeginPaint时用它清空客户区);COLOR_3DFACE+1是指定窗口具有与按钮对话框一致的背景色和其他一些3D属性;默认为灰亮色
AfxGetApp ()->LoadStandardIcon (IDI_WINLOGO) // Class icon(加载系统图标,也可自己定义)
); //调用CWnd::CreateEx()创建主窗口
//第一个参数表示0个或是多个WS_EX标志组合;2:AfxRegisterWndClass()返回的WNDCLASS名称;
//3、标题;4、窗口样式
CreateEx (, 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 (, , , ); //理想客户区窗口矩形形状
CalcWindowRect (&rect); //根据分辨率、菜单...计算窗口矩形大小(必须在窗口创建后调用) SetWindowPos (NULL, , , 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) == -)
return -; if (!SetTimer (ID_TIMER_ELLIPSE, , NULL)||
!SetTimer (ID_TIMER_RECTANGLE, , NULL)) {
MessageBox (_T ("Error: SetTimer failed"));
return -;
}
return ;
} 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 () % , rand () % , rand () % ));
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);
}
上一篇:[游戏学习22] MFC 井字棋 双人对战


下一篇:[游戏学习25] MFC 橡皮筋画线效果