TURBO-569:Common增加提示框HexMessageBox
This commit is contained in:
@@ -0,0 +1,114 @@
|
|||||||
|
<Window x:Class="XP.Common.GeneralForm.Views.HexMessageBox"
|
||||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
Height="240" Width="400"
|
||||||
|
MinHeight="240" MinWidth="400"
|
||||||
|
MaxHeight="240" MaxWidth="400"
|
||||||
|
WindowStartupLocation="Manual"
|
||||||
|
Topmost="True"
|
||||||
|
WindowStyle="None"
|
||||||
|
x:Name="csdMessageBox"
|
||||||
|
Background="White">
|
||||||
|
|
||||||
|
<Window.Resources>
|
||||||
|
<Style x:Key="MenuButton" TargetType="Button">
|
||||||
|
<Setter Property="MinWidth" Value="80" />
|
||||||
|
<Setter Property="Height" Value="30" />
|
||||||
|
<Setter Property="Background" Value="#0078D7" />
|
||||||
|
<Setter Property="Foreground" Value="White" />
|
||||||
|
<Setter Property="BorderThickness" Value="0" />
|
||||||
|
<Setter Property="FontSize" Value="14" />
|
||||||
|
<Setter Property="Cursor" Value="Hand" />
|
||||||
|
<Setter Property="Template">
|
||||||
|
<Setter.Value>
|
||||||
|
<ControlTemplate TargetType="Button">
|
||||||
|
<Border Background="{TemplateBinding Background}" CornerRadius="2" Padding="10,5">
|
||||||
|
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
|
||||||
|
</Border>
|
||||||
|
<ControlTemplate.Triggers>
|
||||||
|
<Trigger Property="IsMouseOver" Value="True">
|
||||||
|
<Setter Property="Background" Value="#005A9E" />
|
||||||
|
</Trigger>
|
||||||
|
<Trigger Property="IsPressed" Value="True">
|
||||||
|
<Setter Property="Background" Value="#004275" />
|
||||||
|
</Trigger>
|
||||||
|
</ControlTemplate.Triggers>
|
||||||
|
</ControlTemplate>
|
||||||
|
</Setter.Value>
|
||||||
|
</Setter>
|
||||||
|
</Style>
|
||||||
|
</Window.Resources>
|
||||||
|
|
||||||
|
<Border BorderBrush="#CCCCCC" BorderThickness="1">
|
||||||
|
<Grid>
|
||||||
|
<Grid.RowDefinitions>
|
||||||
|
<RowDefinition Height="*" />
|
||||||
|
<RowDefinition Height="Auto" />
|
||||||
|
<RowDefinition Height="Auto" />
|
||||||
|
</Grid.RowDefinitions>
|
||||||
|
|
||||||
|
<Grid Grid.Row="0" Margin="10">
|
||||||
|
<Grid.ColumnDefinitions>
|
||||||
|
<ColumnDefinition Width="Auto" />
|
||||||
|
<ColumnDefinition Width="*" />
|
||||||
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
|
<Image Grid.Column="0" Source="{Binding Icon, ElementName=csdMessageBox}"
|
||||||
|
Width="34" Height="33" Margin="0,0,10,0" VerticalAlignment="Top" />
|
||||||
|
|
||||||
|
<ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto" MaxHeight="140">
|
||||||
|
<TextBlock Text="{Binding MessageText, ElementName=csdMessageBox}"
|
||||||
|
TextWrapping="Wrap" FontSize="14" VerticalAlignment="Center" />
|
||||||
|
</ScrollViewer>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
<Rectangle Grid.Row="1" Fill="#CCCCCC" Height="1" />
|
||||||
|
|
||||||
|
<Grid Grid.Row="2" Margin="10">
|
||||||
|
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
|
||||||
|
<StackPanel.Style>
|
||||||
|
<Style TargetType="StackPanel">
|
||||||
|
<Setter Property="Visibility" Value="Collapsed" />
|
||||||
|
<Style.Triggers>
|
||||||
|
<DataTrigger Binding="{Binding MessageBoxButton, ElementName=csdMessageBox}" Value="{x:Static MessageBoxButton.YesNo}">
|
||||||
|
<Setter Property="Visibility" Value="Visible" />
|
||||||
|
</DataTrigger>
|
||||||
|
</Style.Triggers>
|
||||||
|
</Style>
|
||||||
|
</StackPanel.Style>
|
||||||
|
<Button Content="Yes" Style="{StaticResource MenuButton}" Margin="0,0,10,0" Click="Yes_Click" />
|
||||||
|
<Button Content="No" Style="{StaticResource MenuButton}" Click="No_Click" />
|
||||||
|
</StackPanel>
|
||||||
|
|
||||||
|
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
|
||||||
|
<StackPanel.Style>
|
||||||
|
<Style TargetType="StackPanel">
|
||||||
|
<Setter Property="Visibility" Value="Collapsed" />
|
||||||
|
<Style.Triggers>
|
||||||
|
<DataTrigger Binding="{Binding MessageBoxButton, ElementName=csdMessageBox}" Value="{x:Static MessageBoxButton.OKCancel}">
|
||||||
|
<Setter Property="Visibility" Value="Visible" />
|
||||||
|
</DataTrigger>
|
||||||
|
</Style.Triggers>
|
||||||
|
</Style>
|
||||||
|
</StackPanel.Style>
|
||||||
|
<Button Content="OK" Style="{StaticResource MenuButton}" Margin="0,0,10,0" Click="OK_Click" />
|
||||||
|
<Button Content="Cancel" Style="{StaticResource MenuButton}" Click="Cancel_Click" />
|
||||||
|
</StackPanel>
|
||||||
|
|
||||||
|
<StackPanel HorizontalAlignment="Right">
|
||||||
|
<StackPanel.Style>
|
||||||
|
<Style TargetType="StackPanel">
|
||||||
|
<Setter Property="Visibility" Value="Collapsed" />
|
||||||
|
<Style.Triggers>
|
||||||
|
<DataTrigger Binding="{Binding MessageBoxButton, ElementName=csdMessageBox}" Value="{x:Static MessageBoxButton.OK}">
|
||||||
|
<Setter Property="Visibility" Value="Visible" />
|
||||||
|
</DataTrigger>
|
||||||
|
</Style.Triggers>
|
||||||
|
</Style>
|
||||||
|
</StackPanel.Style>
|
||||||
|
<Button Content="OK" Style="{StaticResource MenuButton}" Click="OK_Click" />
|
||||||
|
</StackPanel>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</Border>
|
||||||
|
</Window>
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
using System;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Interop;
|
||||||
|
using System.Windows.Media;
|
||||||
|
using System.Windows.Media.Imaging;
|
||||||
|
|
||||||
|
namespace XP.Common.GeneralForm.Views;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 自定义消息框,可替代标准 MessageBox
|
||||||
|
/// </summary>
|
||||||
|
public partial class HexMessageBox : Window
|
||||||
|
{
|
||||||
|
public MessageBoxResult Result { get; set; }
|
||||||
|
|
||||||
|
public string MessageText
|
||||||
|
{
|
||||||
|
get => (string)GetValue(MessageTextProperty);
|
||||||
|
set => SetValue(MessageTextProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly DependencyProperty MessageTextProperty =
|
||||||
|
DependencyProperty.Register("MessageText", typeof(string), typeof(HexMessageBox), new PropertyMetadata(""));
|
||||||
|
|
||||||
|
public new ImageSource Icon
|
||||||
|
{
|
||||||
|
get => (ImageSource)GetValue(IconProperty);
|
||||||
|
set => SetValue(IconProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public new static readonly DependencyProperty IconProperty =
|
||||||
|
DependencyProperty.Register("Icon", typeof(ImageSource), typeof(HexMessageBox), new PropertyMetadata(null));
|
||||||
|
|
||||||
|
public MessageBoxButton MessageBoxButton
|
||||||
|
{
|
||||||
|
get => (MessageBoxButton)GetValue(MessageBoxButtonProperty);
|
||||||
|
set => SetValue(MessageBoxButtonProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly DependencyProperty MessageBoxButtonProperty =
|
||||||
|
DependencyProperty.Register("MessageBoxButton", typeof(MessageBoxButton), typeof(HexMessageBox), new PropertyMetadata(MessageBoxButton.OK));
|
||||||
|
|
||||||
|
public HexMessageBox(string messageText, MessageBoxButton messageBoxButton, MessageBoxImage image)
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
|
||||||
|
Left = (SystemParameters.PrimaryScreenWidth - Width) / 2;
|
||||||
|
Top = (SystemParameters.PrimaryScreenHeight - Height) / 2;
|
||||||
|
|
||||||
|
MessageText = messageText;
|
||||||
|
IntPtr iconHandle = SystemIcons.Information.Handle;
|
||||||
|
|
||||||
|
switch (image)
|
||||||
|
{
|
||||||
|
case MessageBoxImage.Error:
|
||||||
|
iconHandle = SystemIcons.Error.Handle;
|
||||||
|
break;
|
||||||
|
case MessageBoxImage.Question:
|
||||||
|
iconHandle = SystemIcons.Question.Handle;
|
||||||
|
break;
|
||||||
|
case MessageBoxImage.Warning:
|
||||||
|
iconHandle = SystemIcons.Warning.Handle;
|
||||||
|
break;
|
||||||
|
case MessageBoxImage.Information:
|
||||||
|
iconHandle = SystemIcons.Information.Handle;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Icon = Imaging.CreateBitmapSourceFromHIcon(iconHandle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
||||||
|
Icon.Freeze();
|
||||||
|
|
||||||
|
MessageBoxButton = messageBoxButton;
|
||||||
|
Result = MessageBoxResult.None;
|
||||||
|
Topmost = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 显示模态对话框并返回结果
|
||||||
|
/// </summary>
|
||||||
|
public static MessageBoxResult Show(string messageText, MessageBoxButton messageBoxButton, MessageBoxImage image)
|
||||||
|
{
|
||||||
|
return Application.Current.Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
var window = new HexMessageBox(messageText, messageBoxButton, image);
|
||||||
|
window.ShowDialog();
|
||||||
|
return window.Result;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 显示非模态消息
|
||||||
|
/// </summary>
|
||||||
|
public static HexMessageBox ShowMessage(string messageText, MessageBoxImage image)
|
||||||
|
{
|
||||||
|
HexMessageBox? messageBox = null;
|
||||||
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
messageBox = new HexMessageBox(messageText, MessageBoxButton.OK, image);
|
||||||
|
messageBox.Show();
|
||||||
|
});
|
||||||
|
return messageBox!;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Yes_Click(object sender, RoutedEventArgs e) { Result = MessageBoxResult.Yes; Close(); }
|
||||||
|
private void No_Click(object sender, RoutedEventArgs e) { Result = MessageBoxResult.No; Close(); }
|
||||||
|
private void OK_Click(object sender, RoutedEventArgs e) { Result = MessageBoxResult.OK; Close(); }
|
||||||
|
private void Cancel_Click(object sender, RoutedEventArgs e) { Result = MessageBoxResult.Cancel; Close(); }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user