1
votes


I've come across CDialog::SetDefId and while it is pretty easy and clear that it is for "pushbuttons" I wanted to use this functionality with a non button control.I understand that you have to press Enter or Return to make the dialog use that ID

If I set nID to 0 in the CDialog::OnInitDialog and have no default button setted the dialog will default to CDialog::OnOk, If I do have a default button setted, the dialog will push that button as expected.

The thing is that I want to make this work for a non button control, so if I set nID to a control that is not a pushbutton, the dialog will do nothing even If I set the message handler for the keydown event or NM_RETURN, it doesn't matter if the control has focus or not, the dialog will still do nothing if I press Enter or Return.

How can I make the control be the default control without using stuff like PreTranslateMessage? and which message is sent to the control?

thanks in advance

1
There is no such thing as a "default" for non button controls. For example what should a "default" radio button or a "default" edit control do ? What are you trying to achieve ? This looks like an XY problem to me.Jabberwocky
I was looking at this question stackoverflow.com/questions/30127282/… and was wondering if there could be a default non button control, so any input would be passed to it as default. It is not a problem that I need to solve but rather I'm using it to study about mfcRobson

1 Answers

1
votes

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

BOOL CMyDlg::OnInitDialog()
{
    CDialog::OnInitDialog();
    GetDlgItem(IDC_CHECK1)->SetFocus();
    return 0;  // return TRUE  unless you set the focus to a control
}

In this example the OK button may still be the default button. Enter key will go to default button, probably IDOK. But space key it will change the check box IDC_CHECK1.

There has to be a default button. If you don't want one, then add a fake button, lets say IDC_BUTTON1, 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)