MFC:如何使子对话框的默认按钮起作用?
我有一个子对话框,我在资源编辑器中创建了一个新对话框。然后我在父对话框上使用静态控件作为占位符。子控件显示在占位符使用以下代码的位置:
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
);
这一切都工作正常。我的子对话框上有一个按钮被设置为默认按钮。用鼠标单击按钮即可执行所需的操作。但是,我只想在子对话框上的任何编辑文本框中按 Enter 键,并执行默认按钮的操作。然而它不起作用;我该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保您的按钮的 ID 设置为 IDOK,而不是某些 IDC_*。 MFC 会处理剩下的事情!
Make sure your button has its ID set to IDOK and not some IDC_*. MFC takes care of the rest!
当点击对话框中的回车按钮时,将调用 Parent::OnOK 方法。因此,您可以在 Parent::OnOK 方法中调用 Child::OnOK 。
谢谢。
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.