我用c++代码写了一个窗口,结果在执行时会突然崩掉,报错Process finished with exit code -1073740771 (0xC000041D)
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
switch(msg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
break;
default:
return DefWindowProc(hwnd, msg, wp, lp);
}
}
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASS ClassForWindow = {};
ClassForWindow.lpfnWndProc = WindowProcedure;
ClassForWindow.hInstance = hinstance;
ClassForWindow.lpszClassName = "ClassForWindow";
RegisterClass(&ClassForWindow);
HWND hwnd = CreateWindowEx(0, "ClassForWindow", "window", WS_OVERLAPPEDWINDOW, 0, 0, 800, 608,nullptr, nullptr, hinstance, nullptr);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg = {};
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
解决方案:
是第五和第七行的break碍了事,改一下
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
switch(msg) {
case WM_DESTROY:
PostQuitMessage(0);
return 1; //如果返回非零值,Windows认为消息已经被处理,不会再调用DefWindowProc
case WM_PAINT:
//空着就行(至少现在不用)
default:
return DefWindowProc(hwnd, msg, wp, lp);
}
}
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASS ClassForWindow = {};
ClassForWindow.lpfnWndProc = WindowProcedure;
ClassForWindow.hInstance = hinstance;
ClassForWindow.lpszClassName = "ClassForWindow";
RegisterClass(&ClassForWindow);
HWND hwnd = CreateWindowEx(0, "ClassForWindow", "window", WS_OVERLAPPEDWINDOW, 0, 0, 800, 608,nullptr, nullptr, hinstance, nullptr);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg = {};
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}