我在以下代码中调用RegisterClassEx
时出现类已存在错误。此代码位于类构造函数中:
this->m_wcx.cbSize = sizeof(WNDCLASSEX); // size of structure
this->m_wcx.style = CS_HREDRAW | CS_VREDRAW; // initially minimized
this->m_wcx.lpfnWndProc = &WndProc; // points to window procedure
this->m_wcx.cbClsExtra = 0; // no extra class memory
this->m_wcx.cbWndExtra = 0; // no extra window memory
this->m_wcx.hInstance = m_hInstance; // handle to instance
this->m_wcx.hIcon = ::LoadIcon( NULL, IDI_APPLICATION ); // default app icon
this->m_wcx.hCursor = ::LoadCursor( NULL, IDC_ARROW ); // standard arrow cursor
this->m_wcx.hbrBackground = NULL; // no background to paint
this->m_wcx.lpszMenuName = NULL; // no menu resource
this->m_wcx.lpszClassName = s_pwcWindowClass; // name of window class
this->m_wcx.hIconSm = NULL; // search system resources for sm icon
// Register window class.
if ( (this->m_atom = ::RegisterClassEx( &m_wcx )) == 0 )
{
dwError = ::GetLastError();
TRACE(_T("Failed to register window class.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
THROW(dwError);
}
这个代码第一次执行时,它没有任何问题。当调用类析构函数时,它会取消注册类:
::UnregisterClass( s_pwcWindowClass, this->m_hInstance );
这一切都很好,第一次通过。对构造函数的后续调用会导致RegisterClassEx
失败并显示ERROR_CLASS_ALREADY_EXISTS
。我做错了什么?
答案 0 :(得分:8)
UnregisterClass()
将失败(不会删除该类)。因此,对于使用该类创建的所有窗口,您将需要::DestroyWindow()
。
答案 1 :(得分:4)
我不会在以后需要时取消注册课程。我会像这样测试ERROR_CLASS_ALREADY_EXISTS:
ATOM reg=RegisterClassEx(&m_wcx);
DWORD err=GetLastError();
if(!reg && !(err==ERROR_CLASS_ALREADY_EXISTS)){
//throw only if not successful and not already registered
}
答案 2 :(得分:0)
我有同样的问题,我确信我销毁了使用指定类创建的唯一窗口,然后调用UnregisterClass并返回TRUE(1)但似乎它不会从系统中删除该类而我从下一次调用RegisterClassEx
获取ERROR_ALREADY_EXIST