2012-04-14 89 views
1

我面臨一個非常奇怪的問題。任何一個可以告訴我,什麼是錯的下列代碼 - :Win32代碼異常

#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項目。如果有人願意,我可以郵寄這個項目給他們自己測試一下。快速回復將不勝感激。謝謝。

回答

4

在底部的代碼段中,您使用WS_OVERLAPPEDWINDOW作爲窗口樣式,這就是您的系統菜單。第一個代碼段只有WS_OVERLAPPED,它只給你標題欄和邊框。

+0

非常感謝你,你爲我節省了很多。我欠你。 – Sreyan 2012-04-14 17:44:28

+0

@Sreyan,我認爲你所能做的最好的就是接受ansewr ['this way'](http://meta.stackexchange.com/a/5235/179541);-) – TLama 2012-04-15 18:39:56

+0

感謝你提供的信息:-) – Sreyan 2012-04-16 14:12:33