简体   繁体   中英

MFC: How to get default button of child dialog to work?

I have a child dialog which I created as a new dialog in the resource editor. Then I used a static control on the parent dialog to act as a placeholder. The child control is displayed where the place holder is using the following code:

CRect rect;
m_optionPanelPlaceholder.GetWindowRect(&rect); // In screen coordinates
ScreenToClient(&rect);
m_optionPanelPlaceholder.ShowWindow(SW_HIDE);

optionsDialogPanel_ = new OptionsDialogPanel(settings_);
// Call Create() explicitly to ensure the HWND is created.
optionsDialogPanel_->Create(OptionsDialogPanel::IDD, this);

// Set the window position to be where the placeholder was.
optionsDialogPanel_->SetWindowPos
    (
    NULL,
    rect.left,
    rect.top,
    rect.Width(),
    rect.Height(),
    SWP_SHOWWINDOW
    );

This all works fine. There is a button on my child dialog which is set as the default button. Clicking the button with the mouse takes the desired action. However I want to just press the Enter key while in any of the edit text boxes on the child dialog and have the default button's action taken. However it's not working; how can I do this?

Make sure your button has its ID set to IDOK and not some IDC_ * . MFC takes care of the rest!

When hitting the enter button in a dialog, the Parent::OnOK method is called. So you can probably call the Child::OnOK inside Parent::OnOK method.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM