简体   繁体   English

MFC确定/取消对话框按钮覆盖?

[英]MFC OK/Cancel Dialog Button Override?

Language: C++ 语言: C ++

Development Environment: Microsoft Visual C++ 开发环境: Microsoft Visual C ++

Libraries Used: MFC 使用的库: MFC

Pretty new to MFC, so bear with me. MFC很新,所以请耐心等待。 I have a dialog that is launched via DoModal(). 我有一个通过DoModal()启动的对话框。 I'm attempting to add buttons to this dialog that will replace the default "OK" and "Cancel" buttons. 我正在尝试向此对话框添加按钮,以取代默认的“确定”和“取消”按钮。 Right now, I can't quite figure out how to do this. 现在,我无法弄清楚如何做到这一点。 I deleted the OK and Cancel buttons and added new ones with new IDs, added event handlers, and just some simple code for them to execute upon being pressed, but I couldn't get it to work. 我删除了“确定”和“取消”按钮,并添加了新的ID,添加了事件处理程序,以及一些简单的代码,以便在按下时执行,但我无法使其工作。

I suspect it has something to do with the fact that DoModal() expects responses from OK or Cancel, but nothing else. 我怀疑它与DoModal()期望OK或Cancel的响应这一事实有关,但没有别的。 I'm not really sure though. 我不是很确定。 Any help would be greatly appreciated! 任何帮助将不胜感激!

EDIT: Stripped down code added for reference. 编辑:剥离代码添加供参考。

void CPrefsDlg::Launch() {

[ ... ]

  CSAPrefsDialog dlg;

  INT_PTR nRet = -1;
  nRet = dlg.DoModal();

  // Handle the return value from DoModal
  switch ( nRet )
  {
  case -1: 
     AfxMessageBox("Dialog box could not be created!");
     break;
  case IDABORT:
     // Do something
     break;
  case IDOK: // This works just fine.
     exit(0);
     break;
  case IDSAVEONE: // This does not work.
     MessageBox("Save One");
     break;
  default:
     break;
  };
}

void CPrefsDlg::SaveOne()
{
// I tried adding in my own handler for 'Save One'...this does not work.
MessageBox("Save one");
}

To wire up your dialog to terminate and return IDSAVEONE, you need to add a click handler to the Save One button and have it call EndDialog: 要连接对话框以终止并返回IDSAVEONE,您需要在Save One按钮上添加一个单击处理程序并让它调用EndDialog:

void CSAPrefsDialog::OnBnClickedSaveone()
{
    EndDialog(IDSAVEONE);
}

If you add the click handler through the dialog editor (eg by double-clicking on your button) then the necessary framework code will be generated for you to wire this up; 如果通过对话框编辑器添加单击处理程序(例如,双击按钮),则会生成必要的框架代码以供您连接; otherwise you'll need to add the following line into your BEGIN_MESSAGE_MAP section in your dialog class: 否则,您需要将以下行添加到对话框类的BEGIN_MESSAGE_MAP部分:

ON_BN_CLICKED(IDSAVEONE, &CSAPrefsDialog::OnBnClickedSaveone)

but (as AJG85's just beaten me to posting) depending on what the operation is, how fast it is and whether you want to report errors in the preferences dialog or not, you may want to just carry out the extra function in your on-clicked handler instead. 但是(因为AJG85只是打败我发布)取决于操作是什么,速度有多快以及是否要在首选项对话框中报告错误,您可能只想在点击中执行额外功能处理程序。

MFC has built in ids for the ok and cancel buttons. MFC内置了ok和cancel按钮的ID。 Those being IDOK and IDCANCEL. 那些是IDOK和IDCANCEL。 You can either handle these in a switch via the return of DoModal() or probably better would be to override OnOK() and OnCancel() methods in your dialog class to do what you want. 您可以通过返回DoModal()在交换机中处理这些,也可以更好地覆盖对话框类中的OnOK()OnCancel()方法以执行您想要的操作。

You can do this by adding a line to the message map to call your handler: 您可以通过在消息映射中添加一行来调用处理程序来执行此操作:

Edit: The same thing works for buttons you add to the dialog which I added to my example code below: 编辑:同样的事情适用于您添加到对话框中的按钮,我在下面的示例代码中添加了该对话框:

BEGIN_MESSAGE_MAP(MyDialog, CDialog)
    ON_BN_CLICKED(IDOK, &OnBnClickedOk)
    ON_BN_CLICKED(IDSAVEONE, &OnBnClickedSave)
END_MESSAGE_MAP()

void MyDialog::OnBnClickedOk()
{
   // do extra stuff when they click OK

   CDialog::OnOK(); // call base class version to complete normal behavior
}

void MyDialog::OnBnClickedSave()
{
   // this would be called for your save button with custom id IDSAVEONE

   // note: no base class call here as it's specific to your dialog
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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