这只是一个框架,请期待下一篇文章
(没错我就是牙膏厂厂长)
#include#include #include #include #include typedef unsigned short ushort; typedef unsigned int uint; struct CellData { ushort width = 0; ushort height = 0; std::vector data; }; const uint WINDOW_WIDTH = 1000; const uint WINDOW_HEIGHT = 500; const int OpenButton = 10001; const int SaveButton = 10002; const int StatusButton = 10003; const int QuitButton = 10004; CellData cells; uint getNeighbourCount(ushort x, ushort y); CellData calcNextFrame(); void drawCells(); void saveToFile(); void readFromFile(); HWND createWindow(); inline HINSTANCE getInstance(); LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp); inline void redraw(HWND hwnd); int WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) { HWND window = createWindow(); MSG msg; bool should_quit = false; while (!should_quit) { if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { should_quit = true; } else { TranslateMessage(&msg); DispatchMessage(&msg); } } } DestroyWindow(window); return msg.wParam; } HWND createWindow() { WNDCLASSEX wc; memset(&wc, 0, sizeof(WNDCLASSEX)); wc.cbSize = sizeof(WNDCLASSEX); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.hInstance = getInstance(); wc.lpfnWndProc = WndProc; wc.lpszClassName = "WindowClass"; wc.style = CS_HREDRAW | CS_VREDRAW; RegisterClassEx(&wc); return CreateWindowEx(WS_EX_WINDOWEDGE, "WindowClass", "Window", WS_VISIBLE | WS_OVERLAPPED, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT + GetSystemMetrics(SM_CYCAPTION), nullptr, nullptr, getInstance(), nullptr); } inline HINSTANCE getInstance() { return (HINSTANCE)GetModuleHandle(nullptr); } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { if (msg == WM_CREATE) { CREATESTRUCT* cs = (CREATESTRUCT*)lp; CreateWindowEx(WS_EX_WINDOWEDGE, "button", "Open", WS_VISIBLE | WS_CHILD | WS_BORDER | BS_CENTER | BS_VCENTER, 0, 0, 300, 50, hwnd, (HMENU)OpenButton, cs->hInstance, nullptr); CreateWindowEx(WS_EX_WINDOWEDGE, "button", "Save", WS_VISIBLE | WS_CHILD | WS_BORDER | BS_CENTER | BS_VCENTER, 300, 0, 300, 50, hwnd, (HMENU)SaveButton, cs->hInstance, nullptr); CreateWindowEx(WS_EX_WINDOWEDGE, "button", "Start", WS_VISIBLE | WS_CHILD | WS_BORDER | BS_CENTER | BS_VCENTER, 600, 0, 300, 50, hwnd, (HMENU)StatusButton, cs->hInstance, nullptr); CreateWindowEx(WS_EX_WINDOWEDGE, "button", "Quit", WS_VISIBLE | WS_CHILD | WS_BORDER | BS_CENTER | BS_VCENTER, 900, 0, 100, 50, hwnd, (HMENU)QuitButton, cs->hInstance, nullptr); } else if (msg == WM_PAINT) { drawCells(); } else if (msg == WM_COMMAND) { int id = LOWORd(wp); int event = HIWORd(wp); if(id == QuitButton&&event == BN_CLICKED) { exit(0); } } else if (msg == WM_DESTROY) { PostQuitMessage(0); } else { return DefWindowProc(hwnd, msg, wp, lp); } return 0; } inline void redraw(HWND hwnd) { RECT rect; GetClientRect(hwnd, &rect); InvalidateRect(hwnd, &rect, true); } uint getNeighbourCount(ushort x, ushort y) { uint count = 0; return count; } CellData calcNextFrame() { CellData next; return next; } void drawCells() { } void saveToFile() { } void readFromFile() { }


![[C++] [Win32] 生命游戏-01 [C++] [Win32] 生命游戏-01](http://www.mshxw.com/aiimages/31/869932.png)
