[英]Child Dialog - SetWindowTextA or SendMessageA crashes program - MFC
ERROR: afxwin2.inl line 165 错误:afxwin2.inl第165行
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:
我尝试像这样重载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 is a handle to a static text control. m_hBMI是静态文本控件的句柄。 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.
我知道它可能与尚未创建的控件有关,但我尝试了我所知道的每一种方式。
Using breakpoints, I confirmed that OnInitDialog does not get called at all unless I put it in the overloaded DoModal function. 使用断点,我确认OnInitDialog根本没有被调用,除非我把它放在重载的DoModal函数中。 SetWindowText/SendMessage still crashes in OnInitDialog with the same ASSERT error.
SetWindowText / SendMessage仍然在OnInitDialog中崩溃并出现相同的ASSERT错误。
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. 如果我删除所有SetWindowText / SendMessage,那么子窗口确实会出现模态,但“结果”静态文本控件与我在对话框编辑器属性窗格中设置的文本相同。
Thanks !!!!! 谢谢 !!!!!
*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);
}
The usual sequence when you start a dialog is: 启动对话框时的常用顺序是:
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.
你之前尝试使用它们,所以你会崩溃。
Store the value you need for initialization in a member variable and take care of it in DoDataExchange. 将初始化所需的值存储在成员变量中,并在DoDataExchange中处理它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.