【问题标题】:ASSERT(::IsWindow(m_hWnd)) issue, not sure what's wrongASSERT(::IsWindow(m_hWnd)) 问题,不知道出了什么问题
【发布时间】:2011-10-13 15:05:34
【问题描述】:

我的代码中有一个让我大吃一惊的错误,因此经过多次尝试调试后,我最终决定看看是否有其他人知道我的问题是什么。

我正在尝试将网格对象添加到我拥有的对话框中,但我一直点击标题中提到的assert,我不知道为什么。

LONG myDialog::OnInitDialog(UINT wParam, LONG lParam)
{
    BOOL bRet = super::OnInitDialog();

    InitGridControl();
    InitLayout();

    myApp.ActiveDocChangeEvent->Attach(
    RefMemberDelegate1(*this, &myDialog::OnNewDoc), this); // attach to event so I know when document is created

    return bRet;
}

void myDialog::OnNewDoc(CDerivedDocument* pNewDoc)
{
    pNewDoc->SetMyDialog(this); // when new document is created, set pointer to dialog
}

void myDialog::InitGridControl()
{
    CRect rect;
    // Get the grid area rectangle and set it up.
    GetDlgItem(IDC_GRID)->GetClientRect(rect);
    GetDlgItem(IDC_GRID)->MapWindowPoints(this, &rect); // replacing dummy image with the grid

    m_Grid = new myGridCtrl;
    bool result = m_Grid->Create(WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP, rect, this, IDC_GRID);


    // Set the appropriate options 
    //...options...

    m_Grid->InsertColumn(0, _T("Name"), 100); // doesn't seem to crash here, which means grid is created okay?
}


void myDialog::PopulateGridControl(BOOL bRedraw, CDerivedDocument * pDoc)
{
    if (GetSafeHwnd() == NULL)
        return;

    // get handles to document and stuff

    m_Grid->SetRedraw(FALSE); // ** ASSERT() CALL IS HERE **
    m_Grid->RemoveAll();

    // other stuff..
}


/////////////////////


// In CDocument, once it is created...

CDerivedDocument::SetMyDoc(myDialog * pDlg)
{
    pDlg->PopulateGridControl(true,this);
}

知道发生了什么吗?我的意思是,我只在一切都初始化后才创建对话框,所以那里不应该有问题。 m_Grid.Create() 返回true,所以创建成功。为什么SetRedraw() 击中assertm_hWnd 不是窗口句柄? m_hWnd 在哪里设置?

感谢您提供的任何帮助。

干杯

【问题讨论】:

    标签: c++ mfc window assert


    【解决方案1】:

    您确定在您拨打电话时创建了对话框
    CDerivedDocument::SetMyDoc(myDialog * pDlg)

    我看到的是您正在从文档加载网格(& 对话框),您应该使用文档从视图加载对话框和网格。

    这可能不是您的断言问题的直接原因,但仍然是一种改进。它可能只是将事情按正确的顺序排列并解决此问题。

    【讨论】:

    • 看起来是线程问题。我在非主线程中做一些事情
    • 一种解决方法是将相同的调用 MFC 问题(例如 ::IsWindow(m_hWnd))实际包装在 if 中,如下所示:forums.codeguru.com/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多