我在这里做错了,但错误消息没有给我任何线索。对 CreateWindow 的调用总是失败(返回 NULL,GetLastError() 返回 50)。我想要的只是一个简单的空白窗口,但显然我的请求“不支持”。
// gcc basic.c -o basic.exe -mwindows
#include <stdio.h>
#include <windows.h>
// Function prototypes.
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
// Application entry point.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if (!InitApplication(hinstance))
return FALSE;
if (!InitInstance(hinstance, nCmdShow))
return FALSE;
BOOL fGotMessage;
while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
BOOL InitApplication(HINSTANCE hinstance)
{
WNDCLASSEX wcx;
wcx.cbSize = sizeof (wcx); // size of structure
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = MainWndProc; // points to window procedure
wcx.cbClsExtra = 0; // no extra class memory
wcx.cbWndExtra = 0; // no extra window memory
wcx.hInstance = hinstance; // handle to instance
wcx.hIcon = NULL;
wcx.hCursor = NULL;
wcx.hbrBackground = GetStockObject(WHITE_BRUSH); // white background brush
wcx.lpszMenuName = NULL; // name of menu resource
wcx.lpszClassName = "MainWClass"; // name of window class
wcx.hIconSm = NULL; // small class icon
return RegisterClassEx(&wcx);
}
BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
{
HWND hwnd;
// Create the main window.
hwnd = CreateWindow(
"MainWClass", // name of window class
"Sample", // title-bar string
WS_OVERLAPPEDWINDOW, // top-level window
CW_USEDEFAULT, // default horizontal position
CW_USEDEFAULT, // default vertical position
CW_USEDEFAULT, // default width
CW_USEDEFAULT, // default height
(HWND) NULL, // no owner window
(HMENU) NULL,
hinstance, // handle to application instance
(LPVOID) NULL); // no window-creation data
if (!hwnd) {
int error_code = GetLastError();
char caption[256];
snprintf(caption, sizeof caption, "CreateWindow: error %d", error_code);
MessageBox(NULL, caption, "CreateWindow error", MB_OK);
return FALSE;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
LRESULT CALLBACK
MainWndProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam)
{
switch (uMsg) {
case WM_CLOSE:
DestroyWindow(hwnd);
return 1;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
DefWindowProc(hwnd, uMsg, wparam, lparam);
}
return 0;
}