简体   繁体   中英

Block all windows with MessageBox.Show()

I'm currently working on a WPF app which has multiple windows. From the "main" window, you should be able to close the entire app. Before the app will be closed, the client wants it to show a dialog box which basically asks "are you sure you want to close the app" and blocks every other window until the user answers.

I'm currently using MessageBox.Show() to create this dialog box, but for some reason it only blocks the main window.

Here's the simplest example of what I'm talking about; if you create a WPF window with two buttons:

private void openChildWindowButton_Click(object sender, RoutedEventArgs e)
{
  var window = new ChildWindow();
  window.Show();
}

private void openDialogButton_Click(object sender, RoutedEventArgs e)
{
  MessageBox.Show(this, "This should freeze all other windows");
} 

Opening a dialog will completely freeze the first window. If you click on it or attempt any sort of interaction, the OS makes a "ding!" sound and flashes the border on the message box. But all of the other windows you've opened can be clicked, moved, resized, etc., and that's what I want to prevent.

As it turns out, there is a way to do this, but it's not pretty. It involves using the WinForms version of MessageBox and passing an undocumented option as the last property.

var result = System.Windows.Forms.MessageBox.Show("Are you sure you want to exit this app?", "Exit", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question, System.Windows.Forms.MessageBoxDefaultButton.Button2, (System.Windows.Forms.MessageBoxOptions)8192 /*MB_TASKMODAL*/);

Source: http://social.msdn.microsoft.com/Forums/vstudio/en-US/8d1bd4a2-455e-4e3f-8c88-7ed49aeabc09/messagebox-is-not-applicationmodal?forum=wpf

Hopefully this is helpful to somebody else in the future!

If this works in WPF like in Windows Forms you could just use:

MessageBox.ShowDialog()

chris

Above not working...

Edit:

But there is a workaround: style a Form like a MessageBox (use a fixed Border-Type) and then Show it using ShowDialog(). Then set the Forms Cacel and Ok Button in Properties to your Buttons and you can get a DialogResult just like in a MessageBox. Hope that Helps but it is also from Windows-Forms ;)

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