112 lines
5.0 KiB
C#
112 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Shapes;
|
|
using IconImage = System.Drawing.Icon;
|
|
using System.IO;
|
|
|
|
namespace DBToolControls {
|
|
/// <summary>
|
|
/// Interaktionslogik für Window1.xaml
|
|
/// </summary>
|
|
public partial class WpfMsgBox : Window {
|
|
|
|
public MessageBoxResult Result { get; private set; }
|
|
|
|
private WpfMsgBox(string message, MessageBoxButton buttons, MessageBoxImage icon, MessageBoxResult defaultResult) {
|
|
InitializeComponent();
|
|
|
|
// Message
|
|
MessageText.Text = message;
|
|
|
|
// Buttons
|
|
if (buttons == MessageBoxButton.OK || buttons == MessageBoxButton.OKCancel) {
|
|
OkButton.Visibility = Visibility.Visible;
|
|
OkButton.Focus();
|
|
if (buttons == MessageBoxButton.OK) OkButton.IsCancel = true;
|
|
}
|
|
if (buttons == MessageBoxButton.YesNoCancel || buttons == MessageBoxButton.OKCancel) {
|
|
CancelButton.Visibility = Visibility.Visible;
|
|
CancelButton.IsCancel = true;
|
|
}
|
|
if (buttons == MessageBoxButton.YesNo || buttons == MessageBoxButton.YesNoCancel) {
|
|
YesButton.Visibility = Visibility.Visible;
|
|
NoButton.Visibility = Visibility.Visible;
|
|
if (buttons == MessageBoxButton.YesNo) NoButton.IsCancel = true;
|
|
YesButton.Focus();
|
|
}
|
|
|
|
// Icon
|
|
if (icon == MessageBoxImage.Asterisk) BoxIcon.Source = GetBitmapImageFromIcon(System.Drawing.SystemIcons.Asterisk);
|
|
else if (icon == MessageBoxImage.Error) BoxIcon.Source = GetBitmapImageFromIcon(System.Drawing.SystemIcons.Error);
|
|
else if (icon == MessageBoxImage.Exclamation) BoxIcon.Source = GetBitmapImageFromIcon(System.Drawing.SystemIcons.Exclamation);
|
|
else if (icon == MessageBoxImage.Hand) BoxIcon.Source = GetBitmapImageFromIcon(System.Drawing.SystemIcons.Hand);
|
|
else if (icon == MessageBoxImage.Information) BoxIcon.Source = GetBitmapImageFromIcon(System.Drawing.SystemIcons.Information);
|
|
else if (icon == MessageBoxImage.Question) BoxIcon.Source = GetBitmapImageFromIcon(System.Drawing.SystemIcons.Question);
|
|
else if (icon == MessageBoxImage.Stop) BoxIcon.Source = GetBitmapImageFromIcon(System.Drawing.SystemIcons.Hand);
|
|
else if (icon == MessageBoxImage.Warning) BoxIcon.Source = GetBitmapImageFromIcon(System.Drawing.SystemIcons.Warning);
|
|
else BoxIcon.Visibility = Visibility.Collapsed;
|
|
|
|
// DefaultResult
|
|
Result = (defaultResult == MessageBoxResult.None) ? MessageBoxResult.OK : defaultResult;
|
|
|
|
OkButton.Tag = MessageBoxResult.OK;
|
|
CancelButton.Tag = MessageBoxResult.Cancel;
|
|
YesButton.Tag = MessageBoxResult.Yes;
|
|
NoButton.Tag = MessageBoxResult.No;
|
|
}
|
|
|
|
private void Button_Click(object sender, RoutedEventArgs e) {
|
|
Result = (MessageBoxResult)(sender as Button).Tag ;
|
|
Close();
|
|
}
|
|
|
|
private static BitmapImage GetBitmapImageFromIcon(IconImage ico) {
|
|
System.Drawing.Bitmap bmp = ico.ToBitmap();
|
|
MemoryStream strm = new MemoryStream();
|
|
bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
|
|
|
|
BitmapImage bmpImage = new BitmapImage();
|
|
bmpImage.BeginInit();
|
|
strm.Seek(0, SeekOrigin.Begin);
|
|
bmpImage.StreamSource = strm;
|
|
bmpImage.EndInit();
|
|
|
|
return bmpImage;
|
|
}
|
|
|
|
private void StartDrag(object sender, MouseButtonEventArgs e) {
|
|
this.DragMove();
|
|
}
|
|
|
|
// static Show-Methods
|
|
public static MessageBoxResult Show(Window owner, string messageBoxText, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult) {
|
|
WpfMsgBox box = new WpfMsgBox(messageBoxText, button, icon, defaultResult);
|
|
box.Owner = owner;
|
|
box.WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
|
box.ShowDialog();
|
|
return box.Result;
|
|
}
|
|
|
|
public static MessageBoxResult Show(Window owner, string messageBoxText) {
|
|
return Show(owner, messageBoxText, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK);
|
|
}
|
|
|
|
public static MessageBoxResult Show(Window owner, string messageBoxText, MessageBoxButton button) {
|
|
return Show(owner, messageBoxText, button, MessageBoxImage.None, MessageBoxResult.OK);
|
|
}
|
|
|
|
public static MessageBoxResult Show(Window owner, string messageBoxText, MessageBoxButton button, MessageBoxImage icon) {
|
|
return Show(owner, messageBoxText, button, icon, MessageBoxResult.OK);
|
|
}
|
|
}
|
|
}
|