Win32代码异常

时间:2012-04-14 16:24:27

标签: c++ c winapi user-interface

我面临一个非常奇怪的问题。任何人都可以告诉我以下代码有什么问题 - :

#include <Windows.h>

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

char szWinName[]="MyWin";

int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
                   LPSTR lpszArgs, int nWinMode)
{
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wndclass;

    wndclass.cbSize=sizeof(WNDCLASSEX);

    wndclass.hInstance=hThisInst;
    wndclass.lpszClassName=szWinName;
    wndclass.lpfnWndProc=WindowFunc;
    wndclass.style=0;

    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION)
    wndclass.hIconSm=NULL;
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);

    wndclass.lpszMenuName=NULL;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;

    wndclass.hbrBackground=(HBRUSH) GetStockObject(LTGRAY_BRUSH);

    if(!RegisterClassEx(&wndclass)) return 0;

    hwnd=CreateWindow(
        szWinName,
        "Hello World",
        WS_OVERLAPPED,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        500,
        500,
        NULL,
        NULL,
        hThisInst,
        NULL
        );

    ShowWindow(hwnd, nWinMode);
    UpdateWindow(hwnd);

    while(GetMessage(&msg, NULL, 0, 0)>0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;

}

LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wparam, 
                            LPARAM lparam)
{
    switch(message){
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd,message,wparam,lparam);
    }
    return 0;
}

我得到以下窗口 - : Output of the above code

如您所见,没有系统菜单。我不知道为什么会这样。但是,如果我用以下代码替换上面的代码,它似乎工作正常 - :

#include<windows.h>

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

char szWinName[]="Main Window";

int WINAPI WinMain(HINSTANCE thisInst,HINSTANCE prevInst,
                             LPSTR lpCmdArgs, int nMode){

  HWND hwnd;
  MSG msg;
  WNDCLASSEX wndclass;

  wndclass.cbSize=sizeof(WNDCLASSEX);

  wndclass.hInstance=thisInst;
  wndclass.lpszClassName=szWinName;
  wndclass.lpfnWndProc=WinProc;
  wndclass.style=0;

  wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION)
  wndclass.hIconSm=NULL;
  wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);

  wndclass.lpszMenuName=NULL;
  wndclass.cbClsExtra=0;
  wndclass.cbWndExtra=0;

  wndclass.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH);

  if(!RegisterClassEx(&wndclass)) return 0;

  hwnd=CreateWindow( szWinName,
                     "Hello World",
                     WS_OVERLAPPEDWINDOW,
                     CW_USEDEFAULT,
                     CW_USEDEFAULT,
                     500,
                     500,
                     NULL,
                     NULL,
                     thisInst,
                     NULL
  );

  ShowWindow(hwnd,nMode);
  UpdateWindow(hwnd);

  while(GetMessage(&msg,NULL,0,0))
  {
    TranslateMessage(&msg);                                  
    DispatchMessage(&msg);  
}
    return msg.wParam;

}

    LRESULT CALLBACK WinProc(HWND hWnd, UINT message,
                              WPARAM wparam, LPARAM lparam){

    switch(message){
                case WM_DESTROY:
                     PostQuitMessage(0);
                     break;
                default:
                return DefWindowProc(hWnd, message, wparam, lparam);
                }
    return 0;
}                    

请有人告诉我在第一个代码段中我做错了什么我已经尝试了所有内容而无法找到它的错误。我在Visual Studio 2008专业版中使用正常的Win32项目。如果有人想要我可以将项目邮寄给他们自己测试一下。快速回复将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:4)

在底部代码段中,您使用WS_OVERLAPPEDWINDOW作为窗口样式,这就是为您提供系统菜单的原因。第一个代码段只有WS_OVERLAPPED,它只给你标题栏和边框。