2012-12-01 70 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

你当然没有。 –