C++(msvc2022)创建窗口编译成功但不显示
无bug,感谢解答
#include
#include
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
In HINSTANCE hInstance,
In_opt HINSTANCE hPrevInstance,
In LPSTR lpCmdLine,
In int nCmdShow
)
{
WNDCLASS wndcls{};
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_PEN);
wndcls.hCursor = LoadCursor(NULL, IDC_HAND);
wndcls.hIcon = LoadIcon(NULL, IDI_SHIELD);
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = WinSunProc;
wndcls.lpszClassName = L"C++1";
wndcls.lpszMenuName = NULL;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd = CreateWindow(L"C++1", TEXT("我的C++之旅"), WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch (uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf_s(szChar, "char is %d", wParam);
MessageBox(hwnd, (LPCWSTR)szChar, L"Jim", 0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd, L"mouse clicked", L"C++11", 0);
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc, 300, 200, L"我最帅", strlen("我最帅"));
ReleaseDC(hwnd, hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd, &ps);
TextOut(hDC, 0, 0, L"C++最强", strlen("C++最强"));
EndPaint(hwnd, &ps);
break;
case WM_CLOSE:
if (IDYES == MessageBox(hwnd, L"您真的要退出吗?", L"牛逼C++", MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
代码没问题,我可以运行起来,你是不是项目建错了?需要选择win32项目即可