【问题标题】:CreateWindowEx returns NULLCreateWindowEx 返回 NULL
【发布时间】:2012-06-19 16:42:57
【问题描述】:

以下是我的代码的一部分。每次运行程序,SPanel 的CreateWindowEx 返回NULL,触发错误。任何人都可以看到这段代码有什么问题吗?

SPanelProcMainWndProc 已经声明,并且是原型 LRESULT CALLBACK SPanelProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

我正在编程的环境是 Visual C++ 2010。

为什么CreateWindowEx 总是返回NULL

#include <Windows.h>
#include <WindowsX.h>

LRESULT CALLBACK SPanelProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_MOUSEMOVE:

        break;
    }

    return 0;
}

void Init ()
{
    HWND child;
    HINSTANCE hInstance = (HINSTANCE)(GetWindowLongPtr (parent, GWLP_HINSTANCE));
    WNDCLASSEX wc;
    const char PanelClassName[] = "SPanel"; //ClassName of the panel. Do not reuse on another window

    //Create the window, initialize GWLP_USERDATA, check for errors, register the window class, and hide the window


    //Create the windowclass for the button
    wc.cbSize        = sizeof (WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = SPanelProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon (hInstance, IDI_APPLICATION);
    wc.hCursor       = LoadCursor (hInstance, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = PanelClassName;
    wc.hIconSm       = LoadIcon (hInstance, IDI_APPLICATION);

    //If could not register window class
    if(!RegisterClassEx (&wc))
    {
        MessageBox (NULL, "Panel registration failed!", "Error!", MB_OK);
        PostQuitMessage (0);
        return;
    }

    //Create window
    child = CreateWindowEx (WS_EX_NOPARENTNOTIFY, PanelClassName, "TITLE_TEXT", WS_CHILD, 0, 0, 400, 200, parent, NULL, hInstance, NULL);

    //Check for error in window creation
    if (child == NULL)
    {
        MessageBox (NULL, "Panel creation failed!", "Error!", MB_OK);
        PostQuitMessage (0);
        return;
    }

    return;
}


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hWnd;
    MSG Msg;
    int length, width;

    //Register the window, create the window, and check for errors
    {
        //Window class
        wc.cbSize        = sizeof (WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = MainWndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance; //Instance used to create the window
        wc.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_CODERICON)); //Change here to change large application icon
        wc.hCursor       = LoadCursor (hInstance, IDC_ARROW); //Change here to change application cursor
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); //Change here to change starting background color (white)
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = Coder::Constants::MAINWNDCLASSNAME;
        wc.hIconSm       = (HICON)LoadImage (hInstance, MAKEINTRESOURCE (IDI_CODERICON), IMAGE_ICON, GetSystemMetrcs (SM_CXSMICON), GetSystemMetrics (SM_CYSMICON), 0); //Change here to change application icon (small, as in taskbar)

        //Register the window class so that the ClassName can be used to create the window and then check for error
        if (!RegisterClassEx (&wc))
        {
            MessageBox (NULL, "Main window registration failed!", "Error!", MB_OK);
            return 0;
        }

        //Get screen dimensions
        length = GetSystemMetrics (SM_CXSCREEN);
        width = GetSystemMetrics (SM_CYSCREEN);

        //Create window
        hWnd = CreateWindowEx (WS_EX_CLIENTEDGE, /*Border*/
            Coder::Constants::MAINWNDCLASSNAME,
            Coder::Constants::MAINWNDTITLETEXT, /*Title of window*/
            WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_THICKFRAME | WS_CLIPCHILDREN, /*Window style*/
            (length - Coder::Constants::XDEFAULT - Coder::Constants::XSHIFT) / 2, (width - Coder::Constants::YDEFAULT - Coder::Constants::YSHIFT) / 2, /*Top-left x and y coordinates*/
            Coder::Constants::XDEFAULT + Coder::Constants::XSHIFT, Coder::Constants::YDEFAULT + Coder::Constants::YSHIFT, /*Lengths and widths of window*/
            NULL, NULL, 
            hInstance, NULL);

        //Check for error in window creation
        if (hWnd == NULL)
        {
            MessageBox (NULL, "Main window creation failed!", "Error!", MB_OK);
            return 0;
        }
    }

    //Call function
            Init (hWnd);

    //Show and redraw the window
    ShowWindow (hWnd, nCmdShow);
    UpdateWindow (hWnd);

    //Put window in message loop
    while (GetMessage (&Msg, NULL, 0, 0) > 0) //While not terminated
    {
        TranslateMessage (&Msg);
        DispatchMessage (&Msg);
    }

    //Return
    return static_cast<int>(Msg.wParam);
}

【问题讨论】:

  • SPanelProc() 如何处理WM_NCCREATEWM_CREATE?处理WM_NCCREATE0 处理WM_CREATE 时是否返回TRUE
  • parent 来自哪里?它属于另一个进程吗?这是不可能的。
  • 你能包含一些 GetLastError 的输出吗?您也许可以使用它来诊断您的问题。 msdn.microsoft.com/en-us/library/windows/desktop/…
  • 尝试GetLastErrorFormatMessage 可能会获取有关它失败原因的一些信息。通常是因为您的参数无效,例如样式冲突等。
  • 我怀疑你的 wnd proc 中有错误。你能发布那个代码吗?

标签: c++ visual-studio-2010 winapi visual-c++-2010 createwindowex


【解决方案1】:

每个窗口过程都必须在每条消息上返回 DefWindowProc(hwnd,msg,wparam,lparam),但在使用自定义绘制过程时,WM_ERASEBKGNDWM_PAINT 等消息除外(您必须返回 0,以便操作系统不会重新绘制窗口使用它的默认程序)

【讨论】:

  • 是的,这就是原因。我以为我可以完成创建然后处理 WndProc,但看起来这不是真的。谢谢你们!我已投票支持所有人并将您标记为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
  • 2012-01-22
  • 1970-01-01
  • 2020-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多