#include<windows.h>
LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
//填写窗口类
WNDCLASS wndobj; wndobj.hInstance=(HINSTANCE)1;
wndobj.lpszClassName="NO_1";
wndobj.lpszMenuName =NULL;
wndobj.cbClsExtra =NULL;
wndobj.cbWndExtra =NULL;
wndobj.lpfnWndProc=WindowProc;
wndobj.style =CS_HREDRAW|CS_VREDRAW;
wndobj.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndobj.hCursor = LoadCursor(NULL,IDC_CROSS);
wndobj.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);//申请注册窗口类
if (!RegisterClass(&wndobj))
{
MessageBox(NULL,"Register Error!","information",MB_ICONSTOP);
return 0;
}
//实例化窗口类 HWND hwnd;
hwnd=0;
hwnd=CreateWindow(
"NO_1",
"First Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
200,200,
NULL,NULL,
(HINSTANCE)1,NULL);
    if (!hwnd)
{
MessageBox(NULL,"CreateWindow Error!","information",MB_ICONSTOP);
return 0;
}
//显示窗体
ShowWindow(hwnd,SW_SHOWDEFAULT);
UpdateWindow(hwnd);//处理Windows消息
MSG msg;
while(GetMessage(&msg,hwnd,0,0))
{ TranslateMessage (&msg);
        DispatchMessage (&msg); }
return msg.wParam ;
}//======================回调函数定义==============================
LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
     HDC         hdc ;//设备句柄
     PAINTSTRUCT ps ;//数据结构
     RECT        rect ;//数据结构
switch  (uMsg)
{
 case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          GetClientRect (hwnd, &rect) ;
          DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
          EndPaint (hwnd, &ps) ;
          return 0 ;
    case WM_DESTROY:
 PostQuitMessage (0) ;
         return 0 ;
}

return DefWindowProc(hwnd,uMsg,wParam,lParam);

}

解决方案 »

  1.   

    hwnd=CreateWindow(
    "NO_1",
    "First Window",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    200,200,
    NULL,NULL,
    (HINSTANCE)1,NULL);除了这句中(HINSTANCE)1这个参数我不知老兄是何道理外,没发现什么问题呀。应该不会如你所说。
      

  2.   

    GetMessage(&msg,hwnd,0,0) -->GetMessage(&msg,NULL,0,0)
      

  3.   

    iamwjp(iamwjp) ( ) 
    为什么会这样呀
      

  4.   

    否则不响应PostQuitMessage,看MSDN
      

  5.   

    或者改成
    while(GetMessage(&msg,hwnd,0,0) > 0)
    {
    }也可
      

  6.   

    噢,sorry!iamwjp(iamwjp)说得对。程序收到WM_DESTROY时,窗口就已经不存在了,hwnd无效,GetMessage(&msg,hwnd,0,0)是收不到消息的,while成了死循环
      

  7.   

    If there is an error, the return value is -1. For example, the function fails if hWnd is an invalid window handle or lpMsg is an invalid pointer. To get extended error information, callGetLastError.........Note that the function return value can be nonzero, zero, or -1. Thus, you should avoid code like this:while (GetMessage( lpMsg, hWnd, 0, 0)) ... 
     
    The possibility of a -1 return value means that such code can lead to fatal application errors.