#include #include int running; char szClassName[] = "My template class name"; char szWinName[] = "My template class name"; void fatal(const char * dammit); void create_window(HINSTANCE hInstance, int nCmdShow); void register_window(HINSTANCE hInstance); LRESULT CALLBACK WndProc (HWND hWnd,UINT msg , WPARAM wParam, LPARAM lParam); void fatal(const char * dammit) { LPVOID lpMsgBuf; if (!FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL )) { // Handle the error. return; } MessageBox( NULL, (LPCTSTR)lpMsgBuf, dammit, MB_OK | MB_ICONINFORMATION ); LocalFree( lpMsgBuf ); } void create_window(HINSTANCE hInstance, int nCmdShow) { HWND hwnd; register_window(hInstance); hwnd = CreateWindow(szClassName, szWinName, WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, HWND_DESKTOP, NULL, hInstance, NULL); if(hwnd == NULL) { fatal("Could not create window"); } ShowWindow(hwnd, nCmdShow); } void register_window(HINSTANCE hInstance) { WNDCLASSEX wndclassex; ZeroMemory(&wndclassex, sizeof(WNDCLASSEX)); wndclassex.cbSize = sizeof(WNDCLASSEX); wndclassex.hInstance = hInstance; wndclassex.lpszClassName = szClassName; wndclassex.lpfnWndProc = &WndProc; wndclassex.style = 0; // ? wndclassex.lpszMenuName = NULL; wndclassex.cbClsExtra = 0; wndclassex.cbWndExtra = 0; wndclassex.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclassex.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wndclassex.hCursor = LoadCursor(NULL, IDC_ARROW); wndclassex.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH); if(RegisterClassEx(&wndclassex) == 0) { fatal("Could not register window"); } } LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_DESTROY: PostQuitMessage(0); return 0; default: return DefWindowProc (hWnd, msg, wParam, lParam); break; } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; printf("Hello, world!\n"); create_window(hInstance, nCmdShow); running = 1; while(running) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) running = 0; TranslateMessage(&msg); DispatchMessage(&msg); } else { } } return 0; }