为什么窗口销毁了,进程还在#include<windows.h>LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASSEX wndclass;
wndclass.cbClsExtra=0;
wndclass.cbSize=sizeof(wndclass);
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_APPSTARTING);
wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
wndclass.hIconSm=NULL;
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=MainWndProc;
wndclass.lpszClassName="zhao";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW; RegisterClassEx(&wndclass); HWND hwnd=::CreateWindowEx(0,"zhao","love",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); if(!hwnd)
{
MessageBox(NULL,"Error","Error",MB_OK);
exit(-1);
} ::ShowWindow(hwnd,nCmdShow);
::UpdateWindow(hwnd); MSG msg; while(GetMessage(&msg,hwnd,NULL,NULL))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
} return msg.wParam;}LRESULT CALLBACK MainWndProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch(message)
{
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc=::BeginPaint(hwnd,&ps); ::EndPaint(hwnd,&ps);
break;
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否退出?","退出",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
::PostQuitMessage(0);
break;
default:
return ::DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASSEX wndclass;
wndclass.cbClsExtra=0;
wndclass.cbSize=sizeof(wndclass);
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_APPSTARTING);
wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
wndclass.hIconSm=NULL;
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=MainWndProc;
wndclass.lpszClassName="zhao";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW; RegisterClassEx(&wndclass); HWND hwnd=::CreateWindowEx(0,"zhao","love",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); if(!hwnd)
{
MessageBox(NULL,"Error","Error",MB_OK);
exit(-1);
} ::ShowWindow(hwnd,nCmdShow);
::UpdateWindow(hwnd); MSG msg; while(GetMessage(&msg,hwnd,NULL,NULL))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
} return msg.wParam;}LRESULT CALLBACK MainWndProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch(message)
{
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc=::BeginPaint(hwnd,&ps); ::EndPaint(hwnd,&ps);
break;
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否退出?","退出",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
::PostQuitMessage(0);
break;
default:
return ::DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
#include <windows.h>
#include<windows.h>LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(
HINSTANCE hInstance, // 现在实例句柄
HINSTANCE hPrevInstance, // 先前实例句柄
LPSTR lpCmdLine, // 参数
int nCmdShow // 显示状态
)
{
WNDCLASSEX wc;
wc.cbClsExtra=0; //没有额外的类内存
wc.cbWndExtra=0; //没有额外的窗口内存
wc.cbSize=sizeof(wc); //结构的大小
wc.hbrBackground=(HBRUSH)::GetStockObject(WHITE_BRUSH); //背景
wc.hCursor=LoadCursor(NULL,IDC_CROSS); //光标
wc.hIcon=LoadIcon(NULL,IDI_ASTERISK); //图标
wc.hIconSm=NULL; //没有类的小图标
wc.hInstance=hInstance; //实例句柄名
wc.lpfnWndProc=MainWndProc; //函数窗口指针
wc.lpszClassName="MainWndProc"; //窗口类的名称
wc.lpszMenuName=NULL; //菜单名称
wc.style=CS_HREDRAW | CS_VREDRAW; //窗口样式 RegisterClassEx(&wc); //注册窗口类 //创建主窗口
HWND hwnd=::CreateWindowEx(
0, //dwExStyle,扩展样式
"MainWndProc", //lpClassName,类名
"My first Window", //lpMenuName,标题
WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX, //dwStyle,窗口风格
CW_USEDEFAULT, //X,初始X的坐标
CW_USEDEFAULT, //Y,初始Y的坐标
CW_USEDEFAULT, //nWidth,宽度
CW_USEDEFAULT, //nHeight,高度
NULL, //hWndParent,父窗口句柄
NULL, //hMenu,菜单句柄
hInstance, //hInstance,程序实例句柄
NULL); //lpParam,用户数据 if(!hwnd)
{
::MessageBox(NULL,"创建窗口出错","ERROR",MB_OK);
exit(-1);
} //显示窗口,刷新窗口客户区
::ShowWindow(hwnd,nCmdShow);
::UpdateWindow(hwnd); //从消息队列中取出消息,交给窗口函数处理,直到GetMessage返回FALSE,结束消息循环
MSG msg;
while(::GetMessage(&msg,NULL,0,0))
{
::TranslateMessage(&msg); //转化键盘消息
::DispatchMessage(&msg); //将消息发送到相应的窗口函数
} return msg.wParam; //当GetMessage返回FALSE时程序结束
}LRESULT CALLBACK MainWndProc(
HWND hwnd, //窗口句柄
UINT message, //消息
WPARAM wParam, //低位菜单ID号,高位消息通知码对子窗口控件有效
LPARAM lParam) //发送消息时,低位鼠标x坐标,高位Y坐标
{ switch(message)
{
case WM_PAINT: //窗口客户区重绘
HDC hdc;
PAINTSTRUCT ps;
//使无效的客户区有并行,并取得设备环境句柄
hdc=::BeginPaint(hwnd,&ps);
::EndPaint(hwnd,&ps);
break;
case WM_CLOSE: //关闭
if(IDYES==MessageBox(hwnd,"是否退出?","退出",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
//向消息列队投递一个WM_OUT消息,促使GetMessage函数返回为0,结束循环
::PostQuitMessage(0);
break;
default:
//将不处理的消息交给系统做默认处理
return ::DefWindowProc(hwnd,message,wParam,lParam); }
return 0;
}
=========================
经典错误,应该是 while(GetMessage(&msg,NULL,NULL,NULL)),GetMessage的第二个参数为NULL,否则收不到WM_QUIT消息造成进程无法关闭