读书人

为什么小弟我这个win32程序关闭后进程

发布时间: 2012-02-09 18:22:27 作者: rapoo

为什么我这个win32程序关闭后进程中还有?

#include <windows.h>

LRESULT CALLBACK WinProc( HWND, UINT,WPARAM,LPARAM );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int iShowCmd )
{
static TCHAR szAppname[] = TEXT("WinMain");
//初始化wndclass类
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );
wndclass.hCursor = LoadCursor( NULL,IDC_CROSS );
wndclass.hIcon = LoadIcon( NULL, IDI_HAND );
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WinProc;
wndclass.lpszClassName = szAppname;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;

//注册窗口类
RegisterClass( &wndclass );

//创建窗口
HWND hwnd;
hwnd = CreateWindow( szAppname,TEXT("WinMain"),WS_OVERLAPPEDWINDOW,0,0,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

//显示窗口
ShowWindow( hwnd,iShowCmd );
UpdateWindow( hwnd );

//消息循环
MSG msg;
while( GetMessage( &msg,hwnd,0,0 ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

//窗口过程
LRESULT CALLBACK WinProc( HWND hwnd, UINT uMessage,WPARAM wParam,LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hdc;

switch( uMessage )
{
case WM_PAINT:
hdc = BeginPaint( hwnd,&ps );
EndPaint( hwnd,&ps );
return 0;
case WM_CLOSE:
if(MessageBox(hwnd,TEXT("是否真的结束?"),TEXT("WinMain"),MB_YESNO)==IDYES)
{
DestroyWindow(hwnd);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc( hwnd, uMessage, wParam, lParam );
}
}

主要是WM_CLOSE消息那!点否程序没问题,但点是后没退出程序,只是将窗口销毁了,原因何在啊?

[解决办法]
GetMessage( &msg,hwnd,0,0 )

-->

GetMessage( &msg,0,0,0 )

PostQuitMessage(0) 是发给线程的

lz 只处理了 hwnd 的消息

其它消息都没法处理,自然是无法退出线程,介绍程序运行的
[解决办法]
消息循环中的问题
改:while( GetMessage( &msg,hwnd,0,0 ) ) //只是本窗口的消息

while( GetMessage( &msg,NULL,0,0 ) ) //所有消息
就可以了


[解决办法]
哦,我正在学这个,自己写的时候前面部分代码多数是复制示例代码再修改的,没深入分析
[解决办法]
对,按2楼说的改吧。
while( GetMessage( &msg,NULL,0,0 ) ) //所有消息

读书人网 >C++

热点推荐