代码如下
#include <windows.h>
#include <iostream>
LRESULT CALLBACK win7(
  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 wndcls;                       
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);  
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW); 
wndcls.hIcon=LoadIcon(NULL,IDI_WINLOGO);   
wndcls.hInstance=hInstance; 
wndcls.lpfnWndProc=win7;   
wndcls.lpszClassName="window";  
wndcls.lpszMenuName=NULL;   
wndcls.style=CS_HREDRAW || CS_VREDRAW;  
RegisterClass(&wndcls);   HWND hwnd;   //定义窗口句柄
hwnd=CreateWindow("window","三等奖福克斯",WS_OVERLAPPEDWINDOW,
 200,400,400,300,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 win7(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)          
{
switch(uMsg)
{
case WM_CHAR:             
break;
case WM_LBUTTONDOWN:      
break;
case WM_PAINT:         
HDC hDC;
PAINTSTRUCT sp;
hDC=BeginPaint(hwnd,&sp);
TextOut(hDC,0,0,"酒店客房及阿克",sizeof("酒店客房及阿克"));
EndPaint(hwnd,&sp);
break;
case WM_CLOSE:           
DestroyWindow(hwnd);
break;
case WM_DESTROY:          
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
PostQuitMessage(0)加入消息队列后
while循环不终止,任务管理器里进程还在,CPU占用很高,求解求分析求优化

解决方案 »

  1.   

    while(GetMessage(&msg,hwnd,0,0))   
    =============
    常见错误,应该是while(GetMessage(&msg,NULL,0,0))   
      

  2.   


    也可以这样修改:BOOL myFlag;
    while((myFlag=GetMessage(&msg,hwnd,0,0))!=0)
    {
       if (myFlag==-1)
       {
          return -1;
       }
       else
       {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
       }
    }
      

  3.   

    GetMessage()函数中,如果hWnd参数是无效的时候,将返回-1,你关闭窗口时,调用Destroy来销毁窗口,句柄自然就无效了,于是返回-1.非0为真,于是就是死循环了,所以,while循环不终止,任务管理器里进程还在,CPU占用很高