none
在Win7系统(64位)下使用Visual Stdio 2005的编译问题 RRS feed

  • 问题

  • 大家好,我刚刚学习使用Windows程序设计,但是当我参考书上编写了一个Hello World程序,编译通过后,运行的时候,确发现RegisterClass()返回值为0,这是什么问题呢?

    我所用的系统是Window7,64位机,机器上安装了Visual Stdio 2005,代码如下:

    #include <Windows.h>


    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


    int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
    {
     static TCHAR szAppName[] = TEXT("HelloWin");
     HWND hWnd;
     MSG msg;
     WNDCLASS wndclass;

     wndclass.lpfnWndProc = WndProc;
     wndclass.cbClsExtra = 0;
     wndclass.cbWndExtra = 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 = szAppName;

     if(!RegisterClassEx(&wndclass))
     {
      MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
      return 0;
     }
     hWnd = CreateWindow(szAppName,TEXT("The Hello Program"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
      CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

     ShowWindow(hWnd,SW_SHOWMAXIMIZED);
     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)
    {
     HDC hDc;
     PAINTSTRUCT ps;
     RECT  rect;
     switch(message)
     {
     case WM_CREATE:

      return 0;
     case WM_PAINT:
      hDc = BeginPaint(hwnd,&ps);
      GetClientRect(hwnd,&rect);
      DrawText(hDc,TEXT("Hello Windows!"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
      EndPaint(hwnd,&ps);
      return 0;
     case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;

    }

    2010年10月23日 13:39

答案

  • styles

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2010年10月24日 2:13
    版主

全部回复

  • 用GetLastError检查原因。

    PS:WNDCLASS的部分成员没有初始化。



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2010年10月23日 14:27
    版主
  • 用GetLastError检查原因。

    PS:WNDCLASS的部分成员没有初始化。



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP

    谢谢您的回复,我用GetLastError(),得到的返回值是87(0x57),我上网查了一下,是“参数错误”,我在这个程序中wndclass的什么成员还没有赋值呢?请明示
    2010年10月24日 2:12
  • styles

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2010年10月24日 2:13
    版主
  • 嗯~,是的,我把style赋值后,就正常了,谢谢~

    2010年10月24日 2:25