2012-12-01 72 views
1

我想設置一個簡單的窗口使用C++,但我的電話CreateWindowEx返回NULL。我使用的大多數代碼都來自MSDN網站上的example。沒有我已經嘗試過的工作,任何幫助將不勝感激。C++ CreateWindowEx返回NULL

下面是代碼:

//Include the windows header 
#include <Windows.h> 

//Forward declaration of the WndProc function 
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); 

//Main entry point 
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { 
    //Window class name 
    const wchar_t windowName[] = L"Window Class"; 

    //Set up window class 
    WNDCLASS wnd; 
    wnd.lpfnWndProc = WndProc; 
    wnd.hInstance = hInstance; 
    wnd.lpszClassName = windowName; 

    //Register window class 
    RegisterClass(&wnd); 

    //Create window 
    //! This returns NULL 
    HWND hWnd = CreateWindowEx(
     0, 
     windowName, 
     L"Windows Programming", 
     WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, 
     CW_USEDEFAULT, 
     CW_USEDEFAULT, 
     CW_USEDEFAULT, 
     NULL, 
     NULL, 
     hInstance, 
     NULL 
     ); 

    //Simple check to see if window creation failed 
    if(hWnd == NULL) { 
      //Pause 
     system("PAUSE"); 
     return -1; 
    } 

    //Show the window 
    ShowWindow(hWnd, nCmdShow); 

    //Main message loop 
    MSG msg; 
    while(GetMessage(&msg, NULL, 0, 0)) { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
} 

//WndProc function 
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { 
    switch(msg) { 
    case WM_PAINT: 
     { 
      PAINTSTRUCT ps; 
      HDC hDc = BeginPaint(hWnd, &ps); 

      FillRect(hDc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 1)); 

      EndPaint(hWnd, &ps); 

      return 0; 
     } 
    case WM_DESTROY: 
     { 
      PostQuitMessage(0); 
      return 0; 
     } 
    } 

    return DefWindowProc(hWnd, msg, wParam, lParam); 
} 
+0

我在代碼中看不到'GetLastError',但你在這裏。不過,我注意到填寫你的'WNDCLASS'的幾個方面都失蹤了。 – chris

+0

使用'GetLastError'獲取錯誤代碼併發布。 – Nawaz

+0

RegisterClass成功了嗎?它返回了什麼。 –

回答

6

請注意,來自MSDN的樣本在設置它關心的那些之前將WNDCLASS的所有字段設置爲零。

WNDCLASS wnd = { }; // from MSDN example 

空括號是一個C和C++速記用於初始化整個結構爲0。這也是常見寫這爲{ 0 },這在技術上是略有不同,但具有相同的淨效應。

在代碼中,你放棄了初始化:

WNDCLASS wnd; // your code 

因此,你很可能在其他重要領域之一得到一些垃圾值,像cbClsExtracbWndExtra的,作出一流無法註冊。由於課程未註冊,因此您無法創建該課程的窗口。

2

我使你的代碼工作。基本上我在使用WNDCLASS(或WNDCLASSEX)結構時所做的就是使用所有參數確保不會錯過任何東西。

//Include the windows header 
#include <Windows.h> 

//Forward declaration of the WndProc function 
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); 

//Main entry point 
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { 
    //Window class name 
    const wchar_t windowName[] = L"Window Class"; 

    //Set up window class 
    WNDCLASS wnd; 
    wnd.cbClsExtra = 0; 
    wnd.cbWndExtra = 0; 
    wnd.hCursor = LoadCursor(0, IDC_ARROW); 
    wnd.hIcon = LoadIcon(0, IDI_WINLOGO); 
    wnd.lpszMenuName = 0; 
    wnd.style = 0; 
    wnd.hbrBackground = 0; 
    wnd.lpfnWndProc = WndProc; 
    wnd.hInstance = hInstance; 
    wnd.lpszClassName = windowName; 

    //Register window class 
    RegisterClass(&wnd); 

    //Create window 
    //! This returns NULL 
    HWND hWnd = CreateWindowEx(
     0, 
     windowName, 
     L"Windows Programming", 
     WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, 
     CW_USEDEFAULT, 
     CW_USEDEFAULT, 
     CW_USEDEFAULT, 
     NULL, 
     NULL, 
     hInstance, 
     NULL 
     ); 

    //Simple check to see if window creation failed 
    if(hWnd == NULL) { 
      //Pause 
     system("PAUSE"); 
     return -1; 
    } 

    //Show the window 
    ShowWindow(hWnd, nCmdShow); 

    //Main message loop 
    MSG msg; 
    while(GetMessage(&msg, NULL, 0, 0)) { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
} 

//WndProc function 
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { 
    switch(msg) { 
    case WM_PAINT: 
     { 
      PAINTSTRUCT ps; 
      HDC hDc = BeginPaint(hWnd, &ps); 

      FillRect(hDc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 1)); 

      EndPaint(hWnd, &ps); 

      return 0; 
     } 
    case WM_DESTROY: 
     { 
      PostQuitMessage(0); 
      return 0; 
     } 
    } 

    return DefWindowProc(hWnd, msg, wParam, lParam); 
} 
+0

如果switch語句返回,則不需要中斷。 – johnathon

+0

確實。不要以爲它值得-1。答案還是對的。我會解決這個問題。 – ApplePie

+0

你當然沒有。 –