如何使用非按钮控件的CDialog :: SetDefId?

时间:2015-05-11 10:40:47

标签: c++ mfc dialog


我遇到过CDialog :: SetDefId,虽然它非常容易和清晰,但它适用于"按钮"我想通过非按钮控件使用此功能。我知道您必须按 Enter 返回以使对话框使用该ID

如果我在CDialog :: OnInitDialog中将nID设置为0并且没有设置默认按钮,则对话框将默认为CDialog :: OnOk,如果我确实设置了默认按钮,则对话框将按预期按下该按钮。 / p>

问题是我想让这个工作用于非按钮控件,所以如果我将nID设置为不是按钮的控件,那么对话框什么都不做,即使我为此设置了消息处理程序keydown事件或NM_RETURN,如果控件有焦点并不重要,如果按 Enter Return ,对话框仍然无效。

如何在不使用PreTranslateMessage等内容的情况下将控件设为默认控件?以及哪条消息发送给控件?

提前感谢

1 个答案:

答案 0 :(得分:1)

Yes, it only works with buttons but you can use dy to change focus to any other control:

SetFocus

In this example the OK button may still be the default button. BOOL CMyDlg::OnInitDialog() { CDialog::OnInitDialog(); GetDlgItem(IDC_CHECK1)->SetFocus(); return 0; // return TRUE unless you set the focus to a control } key will go to default button, probably Enter. But space key it will change the check box IDOK.

There has to be a default button. If you don't want one, then add a fake button, lets say IDC_CHECK1, and make it the default button and not visible, then you don't see a default button (you can still add IDC_BUTTON1 to message map and decide what to do with Enter key)