Enter key does not trigger IDOK Default Push Button action

匆匆过客 提交于 2019-12-20 04:57:07

问题


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?


回答1:


The solution was right in front of my eyes in a comment, and I was completely blind, not seeing it!

After seeing the end of the "The WM_INITDIALOG Message" section on Dialog Box Programming Considerations, I understood I had to return FALSE.

I eliminated all the trashy lines I added, changed the return value to FALSE, and everything began to work fine as expected :)



来源:https://stackoverflow.com/questions/35143211/enter-key-does-not-trigger-idok-default-push-button-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!