Mfc 在运行时创建派生控件导致断言

Mfc 在运行时创建派生控件导致断言,mfc,runtime,dynamically-generated,derived-class,Mfc,Runtime,Dynamically Generated,Derived Class,我试图在运行时创建一个控件,但它会导致断言,我不知道是什么导致它。我使用的控件是此链接中的Tree ComboBox控件: 我添加了注册类的代码,如下所示: CTreeComboBox::CTreeComboBox() ... { ... RegisterWindowClass(); } CTreeComboBox::~CTreeComboBox() { m_BrushAlert.DeleteObject(); } BOOL CTreeComboBox::

我试图在运行时创建一个控件,但它会导致断言,我不知道是什么导致它。我使用的控件是此链接中的Tree ComboBox控件:

我添加了注册类的代码,如下所示:

CTreeComboBox::CTreeComboBox()
    ...
{
    ...

    RegisterWindowClass();
}

CTreeComboBox::~CTreeComboBox()
{
    m_BrushAlert.DeleteObject();
}


BOOL CTreeComboBox::RegisterWindowClass()
{
    WNDCLASS wndcls;
    HINSTANCE hInst = AfxGetInstanceHandle();

    if (!(::GetClassInfo(hInst, _T("TreeComboBox"), &wndcls)))
    {
        wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
        wndcls.lpfnWndProc = ::DefWindowProc;
        wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
        wndcls.hInstance = hInst;
        wndcls.hIcon = NULL;
        wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
        wndcls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
        wndcls.lpszMenuName = NULL;
        wndcls.lpszClassName = _T("TreeComboBox");

        if (!AfxRegisterClass(&wndcls))
        {
            AfxThrowResourceException();
            return FALSE;
        }
    }

    return TRUE;
}
我尝试在运行时使用测试程序中的以下代码创建控件:

BOOL CTestComboBoxDlg::OnInitDialog()
{
    ...

    m_ComboBox2.CreateEx(WS_EX_CLIENTEDGE, _T("TreeComboBox"), _T(""), WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP,
        CRect(0, 0, 100, 50), this, 100000, NULL);

    return TRUE;  // return TRUE  unless you set the focus to a control
}
我还尝试使用按钮单击事件创建控件,认为应该让GUI完成初始化,但出现了相同的错误:

void CTestComboBoxDlg::OnBnClickedButton1()
{
    m_ComboBox2.CreateEx(WS_EX_CLIENTEDGE, _T("TreeComboBox"), _T(""), WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP,
        CRect(0, 0, 100, 50), this, 100000, NULL);
}
当我运行该程序时,它在文件dbgrptt.cpp的以下行停止:

     __try
            {
                if (_CRT_ASSERT == nRptType && _InterlockedIncrement(&_crtAssertBusy) > 0)
                {
                    /* use only 'safe' functions -- must not assert in here! */

                    _ERRCHECK(_itoa_s(nLine, szLineMessage, DBGRPT_MAX_MSG, 10));

                    OutputDebugStringA("Second Chance Assertion Failed: File ");
                    OutputDebugStringA(szFile ? szFile : "<file unknown>");
                    OutputDebugStringA(", Line ");
                    OutputDebugStringA(szLineMessage);
                    OutputDebugStringA("\n");

  It stop here===>  _CrtDbgBreak();
                    retval=-1;
                    __leave;
                }
如果我使用VisualStudioGUI编辑器手动创建控件,程序运行良好,因此我不确定出了什么问题。你能帮我弄清楚如何在运行时创建这个控件吗


注意:更改语句:TRACE1_TItem selected:%s\n,GetItemTexthItem;要跟踪所选项目:%s\n,GetItemTexthItem;如果要运行代码并使用MFC回答我自己的问题,请在文件ComboTreeCtrlExt.cpp中输入。将以下代码从CTreeComboBox::PreSubclassWindow移动到CTreeComboBox::OnCreate

当我运行程序时,它在dbgrptt.cpp文件处停止-调用堆栈的顶部通常没有那么重要。为了使这个问题更有价值,应该包括完整的调用堆栈。
CRect rect(0, 0, 0, 0);
DWORD dwStyle =  WS_POPUP | WS_BORDER;
CWnd* pWnd = &m_Tree;
pWnd->CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), 0, NULL);
m_Tree.Init(this);

GetClientRect(rect);
SetDroppedWidth(rect.Width());
SetDroppedHeight(m_nDroppedHeight);

dwStyle = CBS_DROPDOWNLIST & GetStyle();
ASSERT(CBS_DROPDOWNLIST == dwStyle);