#include <windows.h>
const char g_szClassName[] = "myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//register
wc.cbSize=sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.style=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName=NULL;
wc.lpszClassName=g_szClassName;
wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL,"Window Register Failed","Error",MB_ICONEXCLAMATION | MB_OK);
return 0;
}
//Creat window
hwnd=CreateWindowEx(WS_EX_CLIENTEDGE,g_szClassName,"title of my window",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,240,120,NULL,NULL,hInstance,NULL);
if(hwnd=NULL)
{
MessageBox(NULL,"Window Creating Failed","Error",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg,NULL,0,0)>0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}代码如上,我看2小时都晕了,觉得我的没什么错啊

解决方案 »

  1.   

    cbWndExtra
    Specifies the number of extra bytes to allocate following the window instance. The system initializes the bytes to zero. If an application uses WNDCLASSEX to register a dialog box created by using the CLASS directive in the resource file, it must set this member to DLGWINDOWEXTRA. 
      

  2.   

    楼上正解
    wcex.cbSize=sizeof(WNDCLASSEX);
    wcex.style=0;
    wcex.lpfnWndProc = (WNDPROC)WndProc;
    wcex.cbWndExtra=0; wcex.cbClsExtra = 0;wcex.hInstance=hInstance;
    wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
    wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName=NULL;
    wcex.lpszClassName=szWindowClass;
    wcex.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
      

  3.   

    加上
    wc.cbClsExtra = 0;
    这个值没有初始化,当然结果不正确了。一般像这样写:
    WNDCLASSEX wc = { sizeof(WNDCLASSEX) };
    会把WNDCLASSEX成员的值初始化为0