#include <windows.h>LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
HWND hwnd;
static TCHAR szAppName[] = TEXT("Hello");
MSG msg; WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (WNDPROC)WndProc;
wndclass.hInstance = hInstance;
wndclass.cbWndExtra = 0;
wndclass.cbClsExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hIcon = LoadIcon(hInstance,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
wndclass.hIconSm = LoadIcon(hInstance,IDI_APPLICATION); if(!RegisterClassEx(&wndclass))
{
MessageBox(NULL,"ERROR","ERROR",0);
return 0;
} if(!(hwnd = CreateWindowEx(NULL,
szAppName,
"Hello world",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL)))
return 0; ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd); while(GetMessage(&msg,hwnd,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return msg.wParam;
}LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps; switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
TextOut(hdc,0,0,TEXT("你好"),4);
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
} return 0;
}

解决方案 »

  1.   

    看下GetMessage函数里第二个参数的解释吧:
    hWnd 
    [in] Handle to the window whose messages are to be retrieved. The window must belong to the calling thread. The following value has a special meaning. Value Meaning NULL GetMessage retrieves messages for any window that belongs to the calling thread and thread messages posted to the calling thread using the PostThreadMessage function. 
    所以你应该将你的代码中的GetMessage函数的第二个参数写成NULL才行。
      

  2.   

    MSDN中的警告:避免这样做
    Warning  Because the return value can be nonzero, zero, or -1, 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. Instead, use code like this:BOOL bRet;while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)

        if (bRet == -1)
        {
            // handle the error and possibly exit
        }
        else
        {
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        }
    }