儿童对话框 - setWindowTexta或sendmessagea崩溃程序-MFC

Child Dialog - SetWindowTextA or SendMessageA crashes program - MFC

本文关键字:sendmessagea 崩溃 程序 -MFC setWindowTexta 对话框      更新时间:2023-10-16

错误:afxwin2.inl行165

我的应用程序是一个带有一些编辑框的对话框。单击按钮以评估输入的信息后,我想打开一个子对话框以显示结果。我尝试过以下超载Domodal():

//in the child dialog
//.h    
CResultsDlg::CResultsDlg(CParentDlg *parent); 
virtual INT_PTR DoModal(float bmi); 
//.cpp    
CResultsDlg::CResultsDlg(CParentDlg *parent) : CDialogEx(CResultsDlg::IDD), _parent(parent)
{   //initializations  }
INT_PTR CResultsDlg::DoModal(float bmi)
{
    m_sBMI.Format("%f", bmi);
    m_hBMI.SetWindowTextA(m_sBMI); //crashes !!!!!!!!!! 
    m_hBMI.SendMessageA(WM_SETTEXT, 0, (LPARAM)"15.11"); //crashes !!!!!!!! 
//  OnInitDialog(); //because this wasn't getting called at all
    return CDialogEx::DoModal();
}
BOOL CResultsDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
//  __super::OnInitDialog(); //no difference...
m_hBMI.SetWindowTextA("10.3"); //crashes !!!
return true;  // return TRUE unless you set the focus to a control
}

//in the parent dialog 
//.cpp
void CParentDlg::OnBnClickedCalculate()
{
    CResultsDlg childResultsDlg = this;
    childResultsDlg.DoModal(15.7);
}

m_hbmi是静态文本控件的手柄。我测试了一个编辑框,但仍然崩溃了。我了解这可能与尚未创建的控件有关,但我尝试了各种各样的方式。

使用断点,我确认除非我将其放在过载的domodal函数中,否则根本不会被调用。setWindowText/sendmessage仍然在OnInitDialog中崩溃,并具有相同的断言错误。

如果我删除了所有setWindowText/sendmessage,则"子"窗口确实会像应有的那样出现模式,但是"结果"静态文本控件与我在对话框编辑器属性窗格中设置的文本相同。

谢谢!!!!!

*更多详细信息* ----------------------

void CResultsDlg::DoDataExchange(CDataExchange* pDX)    // DDX/DDV support
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_BMI, m_fBMI);
    DDV_MinMaxFloat(pDX, m_fBMI, 0, 100);
    DDX_Control(pDX, IDC_BMI, m_hBMI);
}

启动对话框时的通常序列是:

  • 您称CDIALOG :: DOMODAL。
  • 创建对话框窗口。
  • 创建对话框的孩子控制。
  • oninitdialog被称为。
  • cdialog :: oninitdialog调用dodataexchange。
  • 您在您的dodataexchange方法中有一个ddx_control调用,可以将子控制映射到成员变量。

请注意,成员变量仅在该序列的末尾初始化。您以前尝试使用它们,因此您会崩溃。

将初始化所需的值存储在成员变量中,并在Dodataexchange中照顾它。