最近一直在看Andre LaMothe的 <<Windows 游戏编程大师技巧>> ,使我学到了很多VC++开发Windows程序的基本常识。感谢作者写了这么一本好书。
昨天写书中的一个例子程序,一个弹球在窗口中运动,代码如下:
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
#define WINDOW_WIDTH 400;
#define WINDOW_HEIGHTS 300;
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]="Ball2";
HDC hdc;
HWND hWnd;
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Main message loop:
int ball_x = WINDOW_WIDTH/2;
int ball_y = WINDOW_HEIGHTS/2;
int xv = -4+rand()%8;
int yv = -4+rand()%8;
HPEN white_pen = CreatePen(PS_SOLID, 1, RGB(255,255,255));
HPEN black_pen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
HBRUSH green_brush = CreateSolidBrush(RGB(0,255,0));
HBRUSH black_brush = CreateSolidBrush(RGB(0,0,0));
hdc=GetDC(hWnd);
srand(GetTickCount());
while(true)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
SelectObject(hdc,black_pen);
SelectObject(hdc,black_brush);
Ellipse(hdc, ball_x, ball_y, ball_x + 32, ball_y + 32);
ball_x+=xv;
ball_y+=yv;
// test for collisions, first x-axis
if (ball_x <0 ¦ ¦ball_x >WINDOW_WIDTH-32)
{
xv=-xv;
ball_x+=xv;
}
else
if (ball_y < 0 ¦ ¦ ball_y > WINDOW_HEIGHTS - 32)
{
yv=-yv;
ball_y+=yv;
}
SelectObject(hdc, white_pen);
SelectObject(hdc, green_brush);
Ellipse(hdc, ball_x, ball_y, ball_x + 32, ball_y + 32);
if (KEYDOWN(VK_ESCAPE))
SendMessage(hwnd, WM_CLOSE, 0,0);
Sleep(10);
}
// delete all the objects
DeleteObject(white_pen);
DeleteObject(black_pen);
DeleteObject(green_brush);
DeleteObject(black_brush);
// release the device context
ReleaseDC(hWnd,hdc);
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW ¦ CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_BALL2);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, WINDOW_WIDTH, WINDOW_HEIGHTS, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
结果程序一编译,报了十几个错误,后来在论坛上问了问,才知道
#define WINDOW_WIDTH 400;
#define WINDOW_HEIGHTS 300; 应该把分号去掉。。。(严重无语,应认真检讨)
#define,#include不是C++语句,后面不能有分号,这本来知道,可是一使用就忘了,还是实践太少啊。
现在突然想起<<Windows 游戏编程大师技巧>> 的序中,署名为Richard Benson(后面标注:3D程序员 梦工厂 电子艺界),他写道:Andre LaMothe请他开发一个简单的程序,可是程序总是无法运行,后来Andre LaMothe看了他的程序说:“Richard ,你可真要命,你必须在每行语句的结尾打上分号。。。”
呵呵。^_^