子对话框 - SetWindowTextA或SendMessageA崩溃程序 - MFC [英] Child Dialog - SetWindowTextA or SendMessageA crashes program - MFC
问题描述
错误:afxwin2.inl第165行
我的应用程序是一个带有几个编辑框的对话框。一旦我点击按钮评估输入的信息,我想打开一个子对话框来显示结果。我尝试重载DoModal()像这样:
My app is a dialog box with a few edit boxes. Once I click the button to evaluate the information entered I want to open a child dialog to display the results. I tried overloading DoModal() like this:
//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是静态文本控件的句柄。我测试了一个编辑框,但它仍然崩溃。
我知道它可能与控件没有创建有关,但我尝试了我所知道的每一种方式。
m_hBMI is a handle to a static text control. I tested an edit box but it still crashed. I understand that it probably has something to do with the controls not being created yet but I tried every way I know.
使用断点,我确认OnInitDialog不会被调用,除非我把它放在重载的DoModal函数中。 SetWindowText / SendMessage仍然在OnInitDialog中崩溃,具有相同的ASSERT错误。
Using breakpoints, I confirmed that OnInitDialog does not get called at all unless I put it in the overloaded DoModal function. SetWindowText/SendMessage still crashes in OnInitDialog with the same ASSERT error.
如果我删除所有SetWindowText / SendMessage然后子窗口确实出现模态像它应该,但'结果'静态文本控制是相同的文本,我设置到对话框编辑器属性窗格中。
If I remove all SetWindowText/SendMessage then the child window does come up modal like it should but the 'result' static text control is the same as the text I set it to in the dialog editor properties pane.
感谢!!!!!
-----------
*MORE DETAILS*-----------
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
- You call CDialog::DoModal.
- The dialog window gets created.
- The child controls of the dialog get created.
- OnInitDialog gets called.
- CDialog::OnInitDialog calls DoDataExchange.
- You have a DDX_Control call in your DoDataExchange method to map the child control to a member variable.
请注意,成员变量只在该序列的末尾初始化。
Notice that the member variables only get initialized at the end of that sequence. You're trying to use them way before, so you get a crash.
在成员变量中存储初始化所需的值,并在DoDataExchange中处理它。
Store the value you need for initialization in a member variable and take care of it in DoDataExchange.
这篇关于子对话框 - SetWindowTextA或SendMessageA崩溃程序 - MFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!