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

发布于 2024-11-16 16:20:28 字数 1011 浏览 1 评论 0原文

语言: C++

开发环境: Microsoft Visual C++

使用的库: MFC

对 MFC 还很陌生,所以请耐心等待。我有一个通过 DoModal() 启动的对话框。我正在尝试向此对话框添加按钮,以替换默认的“确定”和“取消”按钮。现在,我不太清楚如何做到这一点。我删除了“确定”和“取消”按钮,并添加了具有新 ID 的新按钮、添加了事件处理程序,以及一些简单的代码供它们在按下时执行,但我无法让它工作。

我怀疑这与 DoModal() 期望来自“确定”或“取消”的响应有关,但与其他无关。但我不太确定。任何帮助将不胜感激!

编辑:添加精简代码以供参考。

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");
}

Language: C++

Development Environment: Microsoft Visual C++

Libraries Used: MFC

Pretty new to MFC, so bear with me. I have a dialog that is launched via 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.

I suspect it has something to do with the fact that DoModal() expects responses from OK or Cancel, but nothing else. 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");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小忆控 2024-11-23 16:20:28

要连接对话框以终止并返回 IDSAVEONE,您需要向 Save One 按钮添加一个单击处理程序并让它调用 EndDialog:

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

如果您通过对话框编辑器添加单击处理程序(例如,通过双击按钮),则将生成必要的框架代码供您进行连接;否则,您需要将以下行添加到对话框类的 BEGIN_MESSAGE_MAP 部分中:

ON_BN_CLICKED(IDSAVEONE, &CSAPrefsDialog::OnBnClickedSaveone)

但是(因为 AJG85 刚刚让我发帖)取决于操作是什么、速度有多快以及您是否无论是否想要在首选项对话框中报告错误,您可能只想在单击处理程序中执行额外的功能。

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:

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

If you add the click handler through the dialog editor (e.g. 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:

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.

暮年 2024-11-23 16:20:28

MFC 已为“确定”和“取消”按钮内置了 id。那些是 IDOK 和 IDCANCEL。您可以通过返回 DoModal() 在开关中处理这些,或者更好的方法是重写 OnOK()OnCancel()对话框类中的方法来执行您想要的操作。

您可以通过在消息映射中添加一行来调用处理程序来完成此操作:

编辑: 对于您添加到对话框中的按钮也适用同样的操作,我将其添加到下面的示例代码中:

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
}

MFC has built in ids for the ok and cancel buttons. Those being IDOK and 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.

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文