I have a CDialog derived class. Its interface definition has several picture boxes and some buttons after, defined in resource file as:
IDD_SELECT_ITEMS DIALOGEX 0, 0, 462, 274
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Select"
FONT 8, "MS Shell Dlg", 0, 0, 0x0
BEGIN
CONTROL "",IDC_ITEM1,"Static",SS_BLACKFRAME,13,18,59,52
//...
CONTROL "",IDC_ITEM18,"Static",SS_BLACKFRAME,373,178,59,52
LTEXT "Select",IDC_STATIC,7,256,40,8
PUSHBUTTON "All",IDC_ALL,47,253,50,14
PUSHBUTTON "None",IDC_NONE,101,253,50,14
PUSHBUTTON "Filter ...",IDC_FILTER,155,253,60,14
DEFPUSHBUTTON "OK",IDOK,353,253,50,14
PUSHBUTTON "Cancel",IDCANCEL,405,253,50,14
END
When I init the form, pressing the Enter Key does not trigger the action associated to the IDOK button, because the IDC_ALL button is focused and Enter does its action, which is undesirable.
In its initialization routine,
BOOL CSelectDialog::OnInitDialog()
{
CDialog::OnInitDialog();
//...
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
I tried everything to put the focus on the IDOK button, but no success. To the point of getting tired of alternating the commenting and uncommenting the lines I added:
BOOL CSelectParts::OnInitDialog()
{
SendDlgItemMessage(IDOK, DM_SETDEFID, 0);
CDialog::OnInitDialog();
// ...
this->SetDefID(IDOK);
GetDlgItem(IDOK)->SendMessage(BN_SETFOCUS, 0, 0);
SendDlgItemMessage(IDOK, WM_SETFOCUS, 0, 0);
SendDlgItemMessage(IDOK, BN_SETFOCUS, 0, 0);
SendDlgItemMessage(IDC_ALL, WM_KILLFOCUS, 0, 0);
SendDlgItemMessage(IDOK, DM_SETDEFID, 0,0);
::SetFocus(GetDlgItem(IDOK)->GetSafeHwnd());
GetDlgItem(IDOK)->SetFocus();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
The closer I got, was by using the line
SendDlgItemMessage(IDOK, WM_SETFOCUS, 0, 0);
which did a very strange thing: it drawed a focus rectangle on two buttons as you can see in the image:
but the Enter key was not yet triggering the Default Push Button!
I even tried to add a OnOK method
void CSelectDialog::OnOK()
{
__super::OnOK();
}
No success yet!
One more experiment: I added an entry to the class's message map:
ON_COMMAND(IDOK, OnOK)
Still unsuccessful!
How can I manage to put the Enter key to do the IDOK button's action?