【问题标题】:CreateWindow Fails as Unable to Find Window Class - C++CreateWindow 因无法找到窗口类而失败 - C++
【发布时间】:2011-05-17 10:30:21
【问题描述】:

在我的应用程序中,函数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 );

我做错了什么?

【问题讨论】:

    标签: c++ visual-studio-2008 createwindow


    【解决方案1】:

    您必须先调用RegisterClassEx,然后才能在 CreateWindow 上使用窗口类。

    示例代码here

    每个进程都必须注册自己的 窗口类。注册一个 应用程序本地类,使用 RegisterClassEx 函数。你必须 定义窗口过程,填写 WNDCLASSEX 结构的成员, 然后将指针传递给 RegisterClassEx 的结构 功能。

    【讨论】:

    • 确保在 RegisterClassEx 和 WindowsCreateEx 中也使用相同的 hinstance
    猜你喜欢
    • 2021-08-24
    • 1970-01-01
    • 2023-03-24
    • 2013-01-18
    • 2023-03-07
    • 2012-07-15
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多