首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

孙鑫VC 第一课 WINDOWS消息 无法结束进程解决方案

2012-06-03 
孙鑫VC 第一课 WINDOWS消息 无法结束进程就是窗口关闭后进程还在,还蛮占内存。。。看到的路过的有空的说说呵,

孙鑫VC 第一课 WINDOWS消息 无法结束进程
就是窗口关闭后进程还在,还蛮占内存。。。
看到的路过的有空的说说呵,谢了哈。

#include <windows.h>
#include <iostream>

LRESULT CALLBACK Winsunproc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc = Winsunproc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0; 
wndclass.hInstance = hInstance; 
wndclass.hIcon = LoadIcon(NULL,IDI_ERROR);  
wndclass.hCursor = LoadCursor(NULL,IDC_CROSS);
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL; 
wndclass.lpszClassName = "类名称"; 

RegisterClass(&wndclass);

HWND hwnd;
hwnd = CreateWindow(
"类名称", // registered class name
"窗口名称", // window name
WS_OVERLAPPEDWINDOW, // window style
0, // horizontal position of window
0, // vertical position of window
600, // window width
400, // window height
NULL, // handle to parent or owner window
NULL, // menu handle or child identifier
hInstance, // handle to application instance
NULL // window-creation data

);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

tagMSG msg;
while (GetMessage(&msg,hwnd,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GetMessage(&msg,NULL,0,0);

return 0;
}




LRESULT CALLBACK Winsunproc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR://取键值
char szChar[20];
sprintf(szChar,"char code is %d",wParam);
MessageBox(hwnd,szChar,"char",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"砍死你丫的","message",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"我砍!",strlen("我砍!"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"我老牛你也敢砍?",strlen("我老牛你也敢砍?"));
EndPaint(hwnd,&ps);
ReleaseDC(hwnd,hDC);
break;
case WM_CLOSE:
if (IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
// DWORD dwProcessId ;
// dwProcessId = GetWindowThreadProcessId(hwnd,NULL);
// HANDLE handle;
// handle = OpenProcess(PROCESS_ALL_ACCESS,NULL,dwProcessId);
// TerminateProcess(handle,0);
//
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);

}
return 0;
}



[解决办法]
Winsunproc函数中没有写捕获关闭的函数
方法1:
case WM_DESTROY:
postquitmessage(0);
break;

方法2:
case WM_SYSCOMMAND
if (wparam == IDCLOSE) {
postquitmessage (0);
}

------解决方案--------------------


C/C++ code
GetMessage(&msg,[color=#FF0000]hwnd[/color],0,0)
[解决办法]
WM_DESTROY消息响应中用postquitmessage(0);
[解决办法]
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

热点排行