簡體   English   中英

子對話框 - SetWindowTextA或SendMessageA崩潰程序 - MFC

[英]Child Dialog - SetWindowTextA or SendMessageA crashes program - MFC

錯誤: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是靜態文本控件的句柄。 我測試了一個編輯框但它仍然崩潰了。 我知道它可能與尚未創建的控件有關,但我嘗試了我所知道的每一種方式。

使用斷點,我確認OnInitDialog根本沒有被調用,除非我把它放在重載的DoModal函數中。 SetWindowText / SendMessage仍然在OnInitDialog中崩潰並出現相同的ASSERT錯誤。

如果我刪除所有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中處理它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM