可能有什么不对? [英] What could be wrong?
问题描述
int以下代码:
BOOL Personal :: OnInitDialog()
{
CPropertyPage: :OnInitDialog中();
CComboBox * pCombo2 =(CComboBox *)GetDlgItem(IDC_COMBO4);
CComboBox * pCombo3 =(CComboBox *)GetDlgItem(IDC_COMBO5);
int i = 0 ;
while (* szSex [i])
{
int iPosition = pCombo2-> AddString(szSex [i]); // 此时抛出异常。
pCombo2-> SetItemData(iPosition,i);
i ++;
}
pCombo2-> SetCurSel( 0 );
i = 0 ;
while (* szStudentKind [i])
{
int iPosition = pCombo3-> AddString(szStudentKind [i]);
pCombo2-> SetItemData(iPosition,i);
i ++;
}
pCombo3-> SetCurSel( 0 );
if (!LoadClassStudents(pStudEdit-> m_iClassID))
{
返回 0 ;
}
INT_PTR iStudentID = pStudEdit-> m_Personal.GetStudentID();
if (!Load(iStudentID))
{
return 0 ;
}
CString stTitle;
stTitle.LoadString(m_hInstance,IDS_PERSONAL,(WORD)LANGUAGE);
SetWindowText(stTitle);
return TRUE; // 除非您将焦点设置为控件
// EXCEPTION:OCX Property Pages应返回FALSE
}
每次运行程序时,我都会得到一个异常,解释为pCombo2的窗口句柄无效,但如果我用IDC_COMBO1替换IDC_COMBO4,程序将按预期运行。
sttrange的一点是,IDC_COMBO1,IDC_COMBO4和IDC_COMBO5都是同一模板上的组合框,可能出错?
< blockquote>在MFC中,GetDlgItem返回一个CWnd指针。
你勇敢地将它转换为CComboBox指针。
试试这个:
将CComboBox成员变量添加到对话框类中。
在你的OnInitDialog中处理程序,将每个CComboBox附加到它的相应HWN D(使用:: GetDlgitem(m_hWnd,IDC_XXXXX)。
您可能需要在对话框类的OnDestroy中分离这些控件。
>
int the following code:
BOOL Personal::OnInitDialog()
{
CPropertyPage::OnInitDialog();
CComboBox *pCombo2 = (CComboBox *)GetDlgItem(IDC_COMBO4);
CComboBox *pCombo3 = (CComboBox *)GetDlgItem(IDC_COMBO5);
int i = 0;
while(*szSex[i])
{
int iPosition = pCombo2->AddString(szSex[i]);//throws an exception at this point.
pCombo2->SetItemData(iPosition,i);
i++;
}
pCombo2->SetCurSel(0);
i = 0;
while(*szStudentKind[i])
{
int iPosition = pCombo3->AddString(szStudentKind[i]);
pCombo2->SetItemData(iPosition,i);
i++;
}
pCombo3->SetCurSel(0);
if(!LoadClassStudents(pStudEdit->m_iClassID))
{
return 0;
}
INT_PTR iStudentID = pStudEdit->m_Personal.GetStudentID();
if(!Load(iStudentID))
{
return 0;
}
CString stTitle;
stTitle.LoadString(m_hInstance,IDS_PERSONAL,(WORD)LANGUAGE);
SetWindowText(stTitle);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
Each time the program is run, I get an exception that interprets to mean pCombo2 has an invalid window handle, But if I replace IDC_COMBO4 with IDC_COMBO1 the program runs as expected.
The sttrange thing is that, IDC_COMBO1, IDC_COMBO4 and IDC_COMBO5 are all comboboxes on the same template, what could be wrong?
In MFC, GetDlgItem returns a CWnd pointer.
You have bravely cast that to a CComboBox pointer.
Try this:
Add CComboBox member variables to your dialog class.
In your OnInitDialog handler, attach each CComboBox to it's corresponding HWND (use ::GetDlgitem(m_hWnd, IDC_XXXXX).
You may need to Detach each of these controls in the dialog class's OnDestroy.
这篇关于可能有什么不对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!