简体   繁体   English

Win32代码异常

[英]Win32 code anomaly

I am facing a very odd problem. 我面临一个非常奇怪的问题。 Can any one tell me what is wrong with the following code-: 任何人都可以告诉我以下代码有什么问题 - :

#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;
}

I am getting the following window-: 我得到以下窗口 - : 输出上面的代码

As you can see there is no system menu. 如您所见,没有系统菜单。 I do not know why this is happening. 我不知道为什么会这样。 But if I replace the above code with the following code it seems to work just fine-: 但是,如果我用以下代码替换上面的代码,它似乎工作得很好 - :

#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;
}                    

Please can someone tell me what I doing wrong in the first code segment I have tried everything and not been able to find what is wrong with it. 请有人告诉我在第一个代码段中我做错了什么我已经尝试了所有内容而无法找到它的错误。 I am using a normal Win32 Project in Visual Studio 2008 Professional Edition. 我在Visual Studio 2008专业版中使用正常的Win32项目。 If anyone wants I can mail the project to them to test it out for themselves. 如果有人想要我可以将项目邮寄给他们自己测试一下。 A quick reply would be appreciated. 快速回复将不胜感激。 Thank You. 谢谢。

In the bottom code segment you use WS_OVERLAPPEDWINDOW as a window style, which is what gives you the system menu. 在底部代码段中,您使用WS_OVERLAPPEDWINDOW作为窗口样式,这将为您提供系统菜单。 The first code segment only has WS_OVERLAPPED, which only gives you the title bar and border. 第一个代码段只有WS_OVERLAPPED,它只为您提供标题栏和边框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM