Exemple #1
0
        public static MessageBoxResult Show(this Window window, string text, string caption,
                                            MessageBoxButton button = MessageBoxButton.OK,
                                            MessageBoxImage image   = MessageBoxImage.None,
                                            ImageSource imageSource = null)
        {
            var dlg  = new MessageBoxWindow();
            var icon = (StockIconId)0;

            switch (image)
            {
            case MessageBoxImage.Error:
                icon = StockIconId.ERROR;
                break;

            case MessageBoxImage.Information:
                icon = StockIconId.INFO;
                break;

            case MessageBoxImage.Exclamation:
                icon = StockIconId.WARNING;
                break;

            case MessageBoxImage.Question:
                icon = StockIconId.HELP;
                break;
            }

            if (icon != 0 || imageSource != null)
            {
                var img = new Image();
                img.VerticalAlignment = VerticalAlignment.Top;
                img.Source            = imageSource ?? StockIcon.GetStockBitmap(icon, StockIcon.SHGSI.SHGSI_LARGEICON);
                img.Width             = 32;
                img.Margin            = new Thickness(0, 0, 10, 0);
                dlg.ContentPanel.Children.Add(img);
            }

            var tb = new TextBlock();

            tb.Text         = text;
            tb.MaxWidth     = System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(window).Handle).WorkingArea.Width / 3;
            tb.TextWrapping = TextWrapping.Wrap;
            dlg.ContentPanel.Children.Add(tb);
            dlg.Title = caption ?? UIUtilities.GetProduct();
            dlg.Owner = window ?? UIUtilities.GetActiveWindow();
            switch (button)
            {
            case MessageBoxButton.OK:
                dlg.Button1.Content  = "OK";
                dlg.ResultForButton1 = MessageBoxResult.OK;
                break;

            case MessageBoxButton.OKCancel:
                dlg.Button1.Content    = "OK";
                dlg.ResultForButton1   = MessageBoxResult.OK;
                dlg.Button2.Content    = "Cancel";
                dlg.Button1.IsDefault  = false;
                dlg.Button2.IsDefault  = true;
                dlg.ResultForButton1   = MessageBoxResult.Cancel;
                dlg.Button2.Visibility = Visibility.Visible;
                dlg.Sep1.Visibility    = Visibility.Visible;
                break;

            case MessageBoxButton.YesNo:
                dlg.Button1.Content    = "Yes";
                dlg.ResultForButton1   = MessageBoxResult.Yes;
                dlg.Button2.Content    = "No";
                dlg.Button1.IsDefault  = false;
                dlg.Button2.IsDefault  = true;
                dlg.ResultForButton2   = MessageBoxResult.No;
                dlg.Button2.Visibility = Visibility.Visible;
                dlg.Sep1.Visibility    = Visibility.Visible;
                break;

            case MessageBoxButton.YesNoCancel:
                dlg.Button1.Content    = "Yes";
                dlg.ResultForButton1   = MessageBoxResult.Yes;
                dlg.Button2.Content    = "No";
                dlg.ResultForButton2   = MessageBoxResult.No;
                dlg.Button3.Content    = "Cancel";
                dlg.Button1.IsDefault  = false;
                dlg.Button2.IsDefault  = false;
                dlg.Button3.IsDefault  = true;
                dlg.ResultForButton3   = MessageBoxResult.Cancel;
                dlg.Button2.Visibility = Visibility.Visible;
                dlg.Button3.Visibility = Visibility.Visible;
                dlg.Sep1.Visibility    = Visibility.Visible;
                dlg.Sep2.Visibility    = Visibility.Visible;
                break;
            }

            _ = dlg.ShowDialog();
            return(dlg.Result);
        }