先上效果图
窗体xaml代码
<local:BaseWindow x:Class="YssAMSS_YGJServo.MyUserControl.XMessageBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:YssAMSS_YGJServo"
mc:Ignorable="d"
Height="240" Width="400"
MaxHeight="600" WindowStyle="None"
MinWidth="240"
MinHeight="180" Style="{DynamicResource MsgWindowStyle}">
<Window.Resources>
<Style x:Key="MsgWindowStyle" TargetType="{x:Type local:BaseWindow}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="ResizeMode" Value="NoResize"/>
<Setter Property="BorderBrush" Value="#ff666666"></Setter>
<Setter Property="BorderThickness" Value="1"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:BaseWindow}">
<Grid Background="#fff3f7f9">
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="#ffdbdbdb">
<Label RenderOptions.BitmapScalingMode="Fant" UseLayoutRounding="True" Height="26" HorizontalAlignment="Left" Margin="14,0,0,0" Width="26" Background="{StaticResource MsgTitle_Brush}"></Label>
<TextBox Foreground="#ff666666" Text="{TemplateBinding Title}" BorderThickness="0" HorizontalAlignment="Left" FontSize="16" VerticalAlignment="Center" Background="Transparent" VerticalContentAlignment="Center" Margin="44,0,0,0" Width="auto" Height="26" IsReadOnly="True"></TextBox>
<Button Margin="0,0,10,0" Background="{StaticResource CloseBrush}" HorizontalAlignment="Right" Height="26" Width="26" Command="{Binding CloseWindowCommand,RelativeSource={RelativeSource TemplatedParent}}">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{DynamicResource CloseBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource CloseBrush}"/>
<Setter Property="Cursor" Value="Hand"></Setter>
<Setter Property="ToolTip" Value="关闭"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
<Grid Grid.Row="1">
<Label x:Name="MsgIcon" BorderThickness="0" Background="{Binding AIcon,RelativeSource={RelativeSource TemplatedParent}}" Height="40" Width="40" HorizontalAlignment="Left" Margin="30,0,0,0"></Label>
<TextBox TextWrapping="Wrap" IsReadOnly="True" Margin="80,0,30,0" BorderThickness="0" Background="Transparent" Text="{Binding AContent, RelativeSource={RelativeSource TemplatedParent}}" TextAlignment="Center" FontSize="18" Height="auto" VerticalAlignment="Center" Foreground="{StaticResource FontColor}">
</TextBox>
</Grid>
<Button x:Name="Confirm" FontSize="13" Foreground="White" Click="Confirm_Click" Grid.Row="2" Style="{StaticResource ButtonStyle}" HorizontalAlignment="Right" Margin="0,0,40,10" Content="确定" Height="26" Width="54" Background="{StaticResource BlueBtnBG}"></Button>
<Button x:Name="Cancel" Visibility="Collapsed" FontSize="13" Click="Cancel_Click" Grid.Row="2" Style="{StaticResource ButtonStyle}" HorizontalAlignment="Right" Margin="0,0,20,10" Content="取消" Height="26" Width="54" Foreground="#ff666666" Background="{StaticResource WhiteBG}"></Button>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsShowCancle" Value="true">
<Setter Property="Visibility" Value="Visible" TargetName="Cancel"></Setter>
<Setter Property="Margin" Value="0,0,110,10" TargetName="Confirm"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="ResizeMode" Value="CanResizeWithGrip">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<Grid>
<AdornerDecorator>
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}"/>
</AdornerDecorator>
<ResizeGrip x:Name="WindowResizeGrip" HorizontalAlignment="Right" IsTabStop="False" Visibility="Collapsed" VerticalAlignment="Bottom"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="ResizeMode" Value="CanResizeWithGrip"/>
<Condition Property="WindowState" Value="Normal"/>
</MultiTrigger.Conditions>
<Setter Property="Visibility" TargetName="WindowResizeGrip" Value="Visible"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
</Grid>
</local:BaseWindow>
窗体对应逻辑代码
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace YssAMSS_YGJServo.MyUserControl
{
/// <summary>
/// XMessageBox.xaml 的交互逻辑
/// </summary>
public partial class XMessageBox : BaseWindow
{
private bool Result = false;
public XMessageBox(EnumNotifyType type,string msg)
{
this.AContent = msg;
switch (type)
{
case EnumNotifyType.Error:
this.Title = "错误";
this.AIcon = (Brush)FindResource("MsgError_Brush");
break;
case EnumNotifyType.Warning:
this.Title = "警告";
this.AIcon = (Brush)FindResource("MsgWarnning_Brush");
break;
case EnumNotifyType.Info:
this.Title = "通知";
this.AIcon = (Brush)FindResource("MsgInfo_Brush");
break;
case EnumNotifyType.Question:
this.IsShowCancle = true;
this.Title = "询问";
this.AIcon = (Brush)FindResource("MsgQuestion_Brush");
break;
default:
break;
}
InitializeComponent();
this.MouseLeftButtonDown += delegate {
DragMove();
};
this.KeyDown += XMessageBox_KeyDown;
}
/// <summary>
/// 确定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Confirm_Click(object sender, RoutedEventArgs e)
{
this.Result = true;
this.Close();
e.Handled = true;
}
/// <summary>
/// 取消
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Cancel_Click(object sender, RoutedEventArgs e)
{
this.Result = false;
this.Close();
e.Handled = true;
}
/// <summary>
/// 显示提示消息框,
/// owner指定所属父窗体,默认参数值为null,则指定主窗体为父窗体。
/// </summary>
private static bool Show(EnumNotifyType type, string mes, Window owner = null)
{
var res = true;
Application.Current.Dispatcher.Invoke(new Action(() => {
XMessageBox nb = new XMessageBox(type,mes);
nb.WindowStartupLocation = WindowStartupLocation.CenterOwner;
nb.Owner = owner ?? MyFuncHelper.GetTopWindow();
nb.ShowInTaskbar = false;
nb.ShowDialog();
res = nb.Result;
}));
return res;
}
public static void Info(string msg, Window owner = null)
{
Show(EnumNotifyType.Info, msg, owner);
}
public static void Warnning(string msg, Window owner = null)
{
Show(EnumNotifyType.Warning, msg, owner);
}
public static void Error(string msg, Window owner = null)
{
Show(EnumNotifyType.Error, msg, owner);
}
public static bool Question(string msg, Window owner = null)
{
return Show(EnumNotifyType.Question,msg,owner);
}
/// <summary>
/// 弹窗的退出快捷键和Esc确认快捷键Enter
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void XMessageBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
Result = false;
this.Close();
}
if (e.Key == Key.Enter)
{
Result = true;
this.Close();
}
}
}
/// <summary>
/// 通知消息类型
/// </summary>
public enum EnumNotifyType
{
[Description("错误")]
Error,
[Description("警告")]
Warning,
[Description("提示")]
Info,
[Description("确认")]
Question,
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace YssAMSS_YGJServo
{
public class BaseWindow : Window
{
public ICommand CloseWindowCommand { get; protected set; }
public ICommand MaximizeWindowCommand { get; protected set; }
public ICommand MinimizeWindowCommand { get; protected set; }
public ICommand DoubleClickMaximizeWindowCommand { get; protected set; }
public BaseWindow()
{
//this.Style = this.FindResource("AmssWindowStyle") as Style;
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
this.MaxHeight = SystemParameters.WorkArea.Height;
this.MaxWidth = SystemParameters.WorkArea.Width;
this.CloseWindowCommand = new RoutedUICommand();
this.MaximizeWindowCommand = new RoutedUICommand();
this.MinimizeWindowCommand = new RoutedUICommand();
this.DoubleClickMaximizeWindowCommand = new RoutedUICommand();
var bind = new CommandBinding(CloseWindowCommand);
bind.Executed += new ExecutedRoutedEventHandler(CloseCommand_Execute);
this.CommandBindings.Add(bind);
var bind1 = new CommandBinding(MaximizeWindowCommand);
bind1.Executed += new ExecutedRoutedEventHandler(MaxCommand_Execute);
this.CommandBindings.Add(bind1);
var bind3 = new CommandBinding(MinimizeWindowCommand);
bind3.Executed += new ExecutedRoutedEventHandler(MinCommand_Execute);
this.CommandBindings.Add(bind3);
var bind4 = new CommandBinding(DoubleClickMaximizeWindowCommand);
//bind4.Executed += new ExecutedRoutedEventHandler(DoubleClickMaxCommand_Execute);
}
private void CloseCommand_Execute(object sender, ExecutedRoutedEventArgs e)
{
this.Close();
}
private void MaxCommand_Execute(object sender, ExecutedRoutedEventArgs e)
{
this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
e.Handled = true;
}
private void MinCommand_Execute(object sender, ExecutedRoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
e.Handled = true;
}
private void DoubleClickMaxCommand_Execute(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
e.Handled = true;
}
}
public static readonly DependencyProperty AIconProperty =
DependencyProperty.Register("AIcon",typeof(Brush),typeof(BaseWindow),new PropertyMetadata(Brushes.DarkCyan));
public Brush AIcon { get { return (Brush)GetValue(AIconProperty); }set { SetValue(AIconProperty,value); } }
public static readonly DependencyProperty AContentProperty =
DependencyProperty.Register("AContent",typeof(object),typeof(BaseWindow),new PropertyMetadata(""));
public object AContent { get { return GetValue(AContentProperty); } set { SetValue(AContentProperty,value); } }
public static readonly DependencyProperty IsShowCancleProperty =
DependencyProperty.Register("IsShowCancle",typeof(bool),typeof(BaseWindow),new PropertyMetadata(false));
public bool IsShowCancle { get { return (bool)GetValue(IsShowCancleProperty); } set { SetValue(IsShowCancleProperty,value); } }
}
}
public static class UIHelper
{
//GetForegroundWindow API
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
/// <summary>
/// 获取当前顶级窗体,若获取失败则返回主窗体
/// </summary>
public static Window GetTopWindow()
{
var hwnd = GetForegroundWindow();
Window win = GetWindowFromHwnd(hwnd);
if (hwnd == IntPtr.Zero || win == null)
{
return Application.Current.MainWindow;
}
return win;
}
private static Window GetWindowFromHwnd(IntPtr hwnd)
{
HwndSource sd = HwndSource.FromHwnd(hwnd);
if (sd != null)
{
return (Window)sd.RootVisual;
}
return null;
}
}
/*禁止转载*/