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

程序不能退出,窗口销毁但是在进程里还在运行解决办法

2012-04-05 
程序不能退出,窗口销毁但是在进程里还在运行#includewindows.h#includestdio.hLRESULT CALLBACK WinSu

程序不能退出,窗口销毁但是在进程里还在运行
#include<windows.h>
#include<stdio.h>

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 winclass;

winclass.cbClsExtra=0;
winclass.cbWndExtra=0;
winclass.hbrBackground=(HBRUSH)GetStockObject(DKGRAY_BRUSH);
winclass.hCursor=LoadCursor(NULL,IDC_CROSS);
winclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
winclass.hInstance=hInstance;
winclass.lpfnWndProc=WinSunProc;
winclass.lpszClassName="weixin";
winclass.lpszMenuName=NULL;
winclass.style=CS_HREDRAW|CS_VREDRAW;

RegisterClass(&winclass);

HWND hwnd;
hwnd=CreateWindow("weixin","课程1",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 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 a[20];
sprintf(a,"char is %d",wParam);
MessageBox(hwnd,a,"1111",MB_OK);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"Mouse is check","weixin",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"abcd",strlen("abcd"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);

TextOut(hDC,0,0,"1234",strlen("1234"));

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;
}

为什么程序运行起来后,点关闭按钮退出不了,在进程还有winmain.exe。

[解决办法]
case WM_CLOSE: 
DestroyWindow(hwnd);
exit(0);//加上这一句

break;

热点排行