因为项目需要,要做一个互相关联的连动数字编辑框。闹了我两天时间,终于做好了。主要的错误出在没有理解MFC的WM_EN_CHANGE这个消息发送的时机,所以每次再影射这个消息并使用时总会出现一个ASSERT,提示::IsWindow(m_hWnd),为false。错误的查找是很辛苦的,一度怀疑是使用DDX影射的缘故,最终把很简单的代码全部修改为GetDlgItem()类的东东,还是由问题。
俺终于忍不住在这个消息里加上了如下代码,虽然看起来不那么顺眼,但总算解决了这个问题。
代码如下:
CButton* pButton=(CButton*)GetDlgItem(m_edt[0]);
CEdit* pStartB=(CEdit*)GetDlgItem(m_edt[6]);
CEdit* pEndB=(CEdit*)GetDlgItem(m_edt[7]);
CEdit* pStartA=(CEdit*)GetDlgItem(m_edt[4]);
CEdit* pEndA=(CEdit*)GetDlgItem(m_edt[5]);
CEdit* pLines=(CEdit*)GetDlgItem(m_edt[10]);
CString strText;
int nLines=1;
int nValue=1;
//必须进行判断,否则会进入非窗口时事机
if(pLines!=NULL && ::IsWindow(pLines->GetSafeHwnd())
&& ::IsWindow(pStartA->GetSafeHwnd()) && ::IsWindow(pStartB->GetSafeHwnd())
&& ::IsWindow(pEndA->GetSafeHwnd()) && ::IsWindow(pEndB->GetSafeHwnd()))
{
pLines->GetWindowText(strText);
nLines=atoi(strText);
pStartA->GetWindowText(strText);
nValue=atoi(strText);
if(nValue>m_nMaxLineNumber-nLines+1)
{
nValue=m_nMaxLineNumber-nLines+1;
strText.Format("%d",nValue);
pStartA->SetWindowText(strText);
}
strText.Format("%d",nValue+nLines-1);
pEndA->SetWindowText(strText);
if(pButton->GetCheck()==BST_CHECKED)
{
pStartA->GetWindowText(strText);
pStartB->SetWindowText(strText);
pEndA->GetWindowText(strText);
pEndB->SetWindowText(strText);
}
else
{
pStartB->GetWindowText(strText);
nValue=atoi(strText);
if(nValue>m_nMaxLineNumber-nLines+1)
{
nValue=m_nMaxLineNumber-nLines+1;
strText.Format("%d",nValue);
pStartB->SetWindowText(strText);
}
strText.Format("%d",nValue+nLines-1);
pEndB->SetWindowText(strText);
}
}