创建窗口失败,因为无法找到窗口类 - C++

3

在我的应用程序中,函数CreateWindow因某种原因而失败。GetLastError指示错误代码为1407,根据MSDN文档,这意味着“找不到窗口类”。以下代码显示了如何调用CreateWindow以及调用时的相应变量名称:

m_hInstance = ::GetModuleHandle( NULL );

if ( m_hInstance == NULL )
{
    TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to retrieve the module handle.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
    THROW(::GetLastError());
}

m_hWnd = ::CreateWindow(
    _pwcWindowClass,    // L"USBEventNotificationWindowClass"
    _pwcWindowName,     // L"USBEventNotificationWindow"
    WS_ICONIC,
    0,
    0,
    CW_USEDEFAULT,
    0,
    NULL,
    NULL,
    m_hInstance,        // 0x00400000
    NULL
    );

if ( m_hWnd == NULL )   // m_hWnd is returned as NULL and exception is thrown.
{
    TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to create window.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
    THROW(::GetLastError());
}

::ShowWindow( m_hWnd, SW_HIDE );

我做错了什么?
1个回答

8

在使用CreateWindow之前,您必须调用RegisterClassEx

示例代码在这里

每个进程都必须注册自己的窗口类。要注册应用程序本地类,请使用RegisterClassEx函数。您必须定义窗口过程,填充WNDCLASSEX结构的成员,然后将指向该结构的指针传递给RegisterClassEx函数。


确保在 RegisterClassEx 和 WindowsCreateEx 中也使用相同的 hinstance。 - Angus Connell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接