int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
WNDCLASSEX  wcex;
wcex.cbSize=sizeof(wcex);
wcex.style=CS_HREDRAW|CS_VREDRAW;
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;
wcex.hInstance=hInstance;
wcex.lpfnWndProc=WinProc;
wcex.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
wcex.hIcon=LoadIcon(hInstance,NULL);
wcex.lpszClassName=("人生");
wcex.lpszMenuName=NULL;
wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
wcex.hIconSm=LoadCursor(NULL,IDC_ARROW);
//注册窗口类
if(!RegisterClassEx(&wcex))return -1;

//创建窗口
g_Hwnd=CreateWindowEx(0,
                  "人生",
                  "人生",
  WS_OVERLAPPEDWINDOW ,
  0,
  0,
  WINDOW_WIDTH,
  WINDOW_HEIGHT,
  NULL,
  NULL,
  hInstance,
  NULL); if(g_Hwnd==NULL)
{
int iiiii=GetLastError();
UnregisterClass("人生",wcex.hInstance);
return -1;
} RECT rect={0,0,WINDOW_WIDTH,WINDOW_HEIGHT}; AdjustWindowRect(&rect,GetWindowLong(g_Hwnd,GWL_STYLE),FALSE); SetWindowPos(g_Hwnd,0,0,0,rect.right-rect.left,rect.bottom-rect.top,SWP_NOMOVE); ShowWindow(g_Hwnd,nShowCmd); UpdateWindow(g_Hwnd); //消息循环
MSG msg;
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
;
} }
UnregisterClass("人生",wcex.hInstance);
return 0;}问题:CreateWindowEx返回值为0,看了参数也没错,即创建窗口失败
有时候RegisterClass也会出现错误,这个应该是传递了无效的参数导致

解决方案 »

  1.   

    if(!RegisterClassEx(&wcex))return -1; 是不是窗口类注册失败导致的
      

  2.   

    GetLastError看看另外MSDN上说This function typically fails for one of the following reasons: an invalid parameter value
    the system class was registered by a different module
    the WH_CBT hook is installed and returns a failure code
    the window procedure fails for WM_CREATE or WM_NCCREATE
      

  3.   

    我现在怎么弄都是创建窗口失败   CreateWindowEx的返回值为0  
      

  4.   

    wcex.hIcon=LoadIcon(hInstance,NULL); 
    =============================
    有这样写的吗?
      

  5.   

    LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
      

  6.   

    .....
    老大,没实现?WM_CREATE没有默认响应的话窗口创建不可能成功-_-b orz 凹点
      

  7.   

    Amuro1987218 :  你没说到点子上去,跟你说吧,这段程序我是从别人那完全给拿来的  
      …………  还有,我说的空壳,是只有函数里面没什么实质性的东西  无语了…………