VC6.0下新建空项目Win32应用程序,新建一个源文件,功能就是创建一个基本窗口,编译通过,但是运行不出来结果,代码如下:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpseCmdLine,int nCmdShow)
{
HWND hwnd;
MSG  Msg;
WNDCLASS wndclass;
char lpszClassName[]="Window";
char lpszTitle[]="My_Windows"; wndclass.style=0;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=lpszClassName; if(!RegisterClass(&wndclass))
{
printf("Error");
MessageBeep(0);

return false;
} hwnd=CreateWindow(
lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL,
);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}

解决方案 »

  1.   

    wndclass.cbWndExtra = 0;
    加上,或者
    WNDCLASS wndclass = {0};
    初始化一下
      

  2.   

    给你我的一个例子吧:
    #include <windows.h>
    #include <stdio.h>LRESULT CALLBACK WinSunProc(
      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(BLACK_BRUSH);
    wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
    wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
    wndcls.hInstance=hInstance;
    wndcls.lpfnWndProc=WinSunProc;
    wndcls.lpszClassName="AG";
    wndcls.lpszMenuName=NULL;
    wndcls.style=CS_HREDRAW | CS_VREDRAW;
    //注册窗口类
    RegisterClass(&wndcls);
        //创建窗口类
    HWND hwnd;
    hwnd=CreateWindow("AG","AG主框架",WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,CW_USEDEFAULT,600,400,NULL,NULL,hInstance,NULL); ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd); MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return 0;
    }LRESULT CALLBACK WinSunProc(
      HWND hwnd,      // handle to window
      UINT uMsg,      // message identifier
      WPARAM wParam,  // first message parameter
      LPARAM lParam   // second message parameter
    )
    {
    switch(uMsg)
    {
    case WM_CHAR:
    char szChar[20];
    sprintf(szChar,"char is %d",wParam);
    MessageBox(hwnd,szChar,"weixin",0);
    break;
    case WM_LBUTTONDOWN:
    MessageBox(hwnd,"mouse clicked","weixin",0);
    HDC hdc;
    hdc=GetDC(hwnd);
    TextOut(hdc,0,50,"计算机编程语言",strlen("计算机编程语言"));
    ReleaseDC(hwnd,hdc);
    break;
    case WM_PAINT:
    HDC hDC;
    PAINTSTRUCT ps;
    hDC=BeginPaint(hwnd,&ps);
    TextOut(hDC,0,0,"AG",strlen("AG"));
    EndPaint(hwnd,&ps);
    break;
    case WM_CLOSE:
    if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))
    {
    DestroyWindow(hwnd);
    }
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
    }