c++ - Win32代码异常

标签 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 项目。如果有人想要,我可以将项目邮寄给他们自己进行测试。快速回复将不胜感激。谢谢。

最佳答案

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

关于c++ - Win32代码异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10155220/

相关文章:

c++ - 如何更正警告 : cast to pointer from integer of different size [-Wint-to-pointer-cast]

c++ - 如何将 PNG 文件加载到我的自定义组件中?无法获得正确的实例

c++ - GLSL和c有什么区别?

c - 为什么在减去指向结构的指针时会得到奇怪的结果?

c - 从一个 header 到另一个 header 中的结构的结构

c++ - 我可以删除在另一个进程中创建的 HBITMAP 对象吗?

c++ - 在异常处理中避免复制

C 编程 - 分解金钱

excel - 如何引用所有正在运行的 Excel 应用程序实例的 COM 对象,包括隐藏和没有工作簿?

Petzold 的编程窗口中的代码(制作窗口错误)