用C++编的一个小程序,运行后只有进程,没有窗口,这是为什么?程序如下

2024-12-14 21:40:23
推荐回答(1个)
回答1:

#include
#include
LRESULT CALLBACK yxgProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam );
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd )
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0; /// 这里重复设置了, 并且漏设置了 wndcls.cbWndExtra = 0;
wndcls.cbClsExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_HAND);
wndcls.hIcon=LoadIcon(NULL,IDI_INFORMATION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=yxgProc;
wndcls.lpszClassName="yxg";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd=CreateWindow("yxg","严祥光",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,hwnd,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK yxgProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam )
{
switch (uMsg)
{
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,0,0,"我们都是好孩子",strlen("我们都是好孩子"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}