【问题标题】:How to prevent an application from not displaying multiple cancel messagebox's?如何防止应用程序不显示多个取消消息框?
【发布时间】:2014-12-20 05:57:06
【问题描述】:

我有一个propertysheet,它分别有三个页面(page1、page2、page3)。每当按下取消按钮或单击 [X] 或 Esc 时,我都会将其添加为消息框被按下。

后续步骤:

1.运行应用程序。

  1. 按下取消按钮并弹出消息框。 (没有取消消息​​框)。

  2. 现在转到任务栏并右键单击应用程序图标,然后单击“关闭窗口”。正是在这里出现了问题;即又弹出一个消息框窗口。

实际上这不应该发生,对吧?它应该仅限于一个消息框。

//This is being triggered when close window or cancel button is pressed.

BOOL OnQueryCancel()
{

    if(IDOK == ::MessageBox(m_hWnd, L"Closing the application",
                            L"Warning", MB_OKCANCEL | MB_ICONWARNING))
    {
        return TRUE;
    }
    return FALSE;
}

如何防止不显示多个消息框?我应该将焦点显示在已经打开的消息框上。

【问题讨论】:

标签: mfc messagebox


【解决方案1】:

首先,您应该使用 AfxMessageBox,它在 MFC 中更容易。其次,这是 Windows 中的正常操作——它只是响应关闭消息。我会添加一个变量来指示该框已显示:

//Part of your class
BOOL m_bIsPromptActive;

BOOL OnQueryCancel()
{
  if( !m_bIsPromptActive)
  {
    m_bIsPromptActive = TRUE;

    if(IDOK == ::MessageBox(m_hWnd, L"Closing the application",
                            L"Warning", MB_OKCANCEL | MB_ICONWARNING))
    {
        return TRUE;
    }

    m_bIsPromptActive = FALSE;
  }
  else
  {
     // Message is already displayed. Set the focus to this window
     ::SetFocus( m_hWnd ); // or this->SetFocus();
     // You can also look at ::BringWindowToFront()
  }

  return FALSE;
}

【讨论】:

  • 感谢您的回复。它就像魅力一样。在 else 部分中,您将焦点设置到父窗口。我们可以设置焦点到消息框而不是设置焦点到父窗口吗?
  • 您无法将焦点设置到 MessageBox,因为您没有它的句柄。但是,MB 是表单的子项(您传递了 m_hWnd),并且由于它是模态的,因此父项将为其提供焦点。 (您可以检索 MB 句柄,但不值得麻烦)。
猜你喜欢
  • 2016-01-13
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多