⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3장 기본소스.cpp

📁 this is a simple code, this tutorial explains how to make a grid-based game.
💻 CPP
字号:
#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE	hInstance,
				   HINSTANCE	hPrevInstance,
				   PSTR			szCmdLine,
				   int			iCmdShow)
{
	static TCHAR	szAppname[] = TEXT("HelloWin");
	HWND			hwnd;
	MSG				msg;
	WNDCLASS		wndclass;
	
	wndclass.style				= CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
	wndclass.lpfnWndProc		= WndProc; //磊悼栏肺 龋免且 窃荐狼 捞抚(林家)
	wndclass.cbClsExtra			= 0;
	wndclass.cbWndExtra			= 0;
	wndclass.hInstance			= hInstance; //WinMain狼 霉锅掳 颇扼皋磐肺 逞绢吭带...
	wndclass.hIcon				= LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground		= (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName		= NULL;
	wndclass.lpszClassName		= szAppname;
	
	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("This Program Requires Windwos NT !"), szAppname, MB_ICONERROR);
		return 0;
	}
	
	hwnd = CreateWindow(szAppname,	//window class name
		"The Hello Program",		//window caption
		WS_OVERLAPPEDWINDOW,		//window style
		CW_USEDEFAULT,				//initial x position
		CW_USEDEFAULT,				//initial y position
		300,						//initial x size
		300,						//initial y size
		NULL,						//parent window handle
		NULL,						//window menu handle
		hInstance,					//program instance handle
		NULL);						//creation parameters
	
	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);
	
	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	
	return msg.wParam;	
}

#define MAXROW 25
#define MAXCOL 25

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{	
	static int Board[MAXROW][MAXCOL];

	static int cxBlock;
	static int cyBlock;

	switch(message)
	{
	case WM_CREATE:
		{
			RECT rect;
			SetRect(&rect, 0, 0, 30*MAXCOL, 30*MAXROW);
			AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);

			MoveWindow(hwnd, 100, 100, rect.right - rect.left, rect.bottom - rect.top, TRUE);
		}

		return 0;

	case WM_SIZE:
		cxBlock = LOWORD(lParam) / MAXCOL;
		cyBlock = HIWORD(lParam) / MAXROW;
		return 0;

	case WM_LBUTTONDOWN:
		{
			int x = LOWORD(lParam);
			int y = HIWORD(lParam);
			
			int xPos = x / cxBlock;
			int yPos = y / cyBlock;
			
			Board[yPos][xPos] = 1;
			
			RECT rect;
			SetRect(&rect, xPos*cxBlock, yPos*cyBlock, (xPos+1)*cxBlock, (yPos+1)*cyBlock);
			InvalidateRect(hwnd, &rect, TRUE);
		}
		return 0;

	case WM_RBUTTONDOWN:
		{
			int x = LOWORD(lParam);
			int y = HIWORD(lParam);
			
			int xPos = x / cxBlock;
			int yPos = y / cyBlock;
			
			Board[yPos][xPos] = 2;
			
			RECT rect;
			SetRect(&rect, xPos*cxBlock, yPos*cyBlock, (xPos+1)*cxBlock, (yPos+1)*cyBlock);
			InvalidateRect(hwnd, &rect, TRUE);
		}
		return 0;

	case WM_KEYDOWN:
		switch(wParam)
		{
		case VK_RETURN:
			{
				POINT pt;
				GetCursorPos(&pt);
				ScreenToClient(hwnd, &pt);

				SendMessage(hwnd, WM_LBUTTONDOWN, NULL, MAKELPARAM(pt.x, pt.y));
			}
			
			return 0;

		case VK_SPACE:
			{
				POINT pt;
				GetCursorPos(&pt);
				ScreenToClient(hwnd, &pt);
				
				SendMessage(hwnd, WM_RBUTTONDOWN, NULL, MAKELPARAM(pt.x, pt.y));
			}
			
			return 0;


		case VK_LEFT:
			{
				POINT pt;
				GetCursorPos(&pt);
				ScreenToClient(hwnd, &pt);

				int xPos = pt.x / cxBlock;
				int yPos = pt.y / cyBlock;

				xPos = ((xPos - 1) + MAXCOL) % MAXCOL;
				pt.x = xPos * cxBlock + cxBlock/2;
				pt.y = yPos * cyBlock + cyBlock/2;
				ClientToScreen(hwnd, &pt);

				SetCursorPos(pt.x, pt.y);
			}
			break;
		
		case VK_RIGHT:
			{
				POINT pt;
				GetCursorPos(&pt);
				ScreenToClient(hwnd, &pt);
				
				int xPos = pt.x / cxBlock;
				int yPos = pt.y / cyBlock;
				
				xPos = ((xPos + 1) + MAXCOL) % MAXCOL;
				pt.x = xPos * cxBlock + cxBlock/2;
				pt.y = yPos * cyBlock + cyBlock/2;
				ClientToScreen(hwnd, &pt);
				
				SetCursorPos(pt.x, pt.y);
			}
			break;

		case VK_UP:
			{
				POINT pt;
				GetCursorPos(&pt);
				ScreenToClient(hwnd, &pt);
				
				int xPos = pt.x / cxBlock;
				int yPos = pt.y / cyBlock;
				
				yPos = ((yPos - 1) + MAXROW) % MAXROW;
				pt.x = xPos * cxBlock + cxBlock/2;
				pt.y = yPos * cyBlock + cyBlock/2;
				ClientToScreen(hwnd, &pt);
				
				SetCursorPos(pt.x, pt.y);
			}
			break;

		case VK_DOWN:
			{
				POINT pt;
				GetCursorPos(&pt);
				ScreenToClient(hwnd, &pt);
				
				int xPos = pt.x / cxBlock;
				int yPos = pt.y / cyBlock;
				
				yPos = ((yPos + 1) + MAXROW) % MAXROW;
				pt.x = xPos * cxBlock + cxBlock/2;
				pt.y = yPos * cyBlock + cyBlock/2;
				ClientToScreen(hwnd, &pt);
				
				SetCursorPos(pt.x, pt.y);
			}
			break;

		}
	return 0;

	case WM_PAINT:

		{
		HDC			hdc;
		PAINTSTRUCT	ps;
				
		hdc = BeginPaint(hwnd, &ps);
				
		for (int y = 0; y < MAXROW-1; ++y)
		{
			for (int x = 0; x < MAXCOL-1; ++x)
			{
				Rectangle(hdc, x*cxBlock+cxBlock/2, y*cyBlock+cyBlock/2, (x+1)*cxBlock+cxBlock/2, (y+1)*cyBlock+cyBlock/2);
			}
		}
		
		for (y = 0; y < MAXROW; ++y)
		{
			for (int x = 0; x < MAXCOL; ++x)
			{
				if (Board[y][x] == 1)
				{
					HBRUSH hBrush = CreateSolidBrush(RGB(255,0,0));
					HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
					Ellipse(hdc, x*cxBlock, y*cyBlock, (x+1)*cxBlock, (y+1)*cyBlock);
					SelectObject(hdc, hOldBrush);
					DeleteObject(hBrush);
				}
				else if(Board[y][x] == 2)
				{
					HBRUSH hBrush = CreateSolidBrush(RGB(0,0,255));
					HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
					Ellipse(hdc, x*cxBlock, y*cyBlock, (x+1)*cxBlock, (y+1)*cyBlock);
					SelectObject(hdc, hOldBrush);
					DeleteObject(hBrush);
				}
			}
		}
		EndPaint(hwnd, &ps);
		}

		return 0;
	
	case WM_DESTROY:

		PostQuitMessage(0);
		return 0;
		
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -