wpf 自定义消息框

时间:2023-03-08 15:52:25

相信很多人用过MessageBox.show(),是不是觉得这个消息框有点丑呢,反正我是觉得有点丑的,所以我自己重写了一个。先不说,上两幅图对比先:

  wpf 自定义消息框wpf 自定义消息框

当然,也不是很好看,不过比原有的好多了。

不多说了,先上xmal代码:

 <Window x:Class="MESBox.MEGBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MEGBox" MinWidth="" WindowStyle="None"
AllowsTransparency="True" Background="#AA000000"
WindowStartupLocation="CenterScreen" Window.SizeToContent="WidthAndHeight"
MouseLeftButtonDown="DragWindow" ShowInTaskbar="False">
<Window.Resources>
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<!--设置样式 -->
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle x:Name="Rectangle" Stroke="#FFFFFFFF" StrokeMiterLimit="1.000000" StrokeThickness="0.500000" RadiusX="12.5" RadiusY="12.5" Fill="#FF777777">
</Rectangle>
<ContentPresenter x:Name="ContentPresenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
</Grid>
<!-- 设置鼠标移到关闭按钮上的效果 -->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Fill" TargetName="Rectangle">
<Setter.Value>
<SolidColorBrush Color="White"></SolidColorBrush>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Black"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources> <Grid Height="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto" ></RowDefinition>
</Grid.RowDefinitions>
<DockPanel Grid.Row="">
<Button DockPanel.Dock="Right" Style="{StaticResource ButtonStyle}"
Width="" Height="" Content="X"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="3,3,3,3"
Click="CloseWindow" >
</Button>
</DockPanel>
<TextBlock Padding="10,15,10,15" Grid.Row="" x:Name="content"
Foreground="White" FontSize=""
MaxWidth="" TextWrapping="Wrap"/> <StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" Grid.Row="">
<Button Content="确定" Width="" Click="CloseWindow" Height="" Margin="10,0,0,0" ></Button>
</StackPanel>
</Grid>
</Window>

  

其中,window 的属性里WindowStyle="None",AllowsTransparency="True"是设置window无边框的关键,WindowStartupLocation="CenterScreen",使窗口初始化时在屏幕正*出现,Background="#AA000000",#AA000000是具有半透明的颜色,另外,由于消息框的大小是随着内容的多少来变化的,所以并没有设置窗口的长和宽,因此设置Window.SizeToContent="WidthAndHeight",为的是使消息框能自适应内容。

  另外,还要注意的是,因为window失去了边框和它的头部,所以是不能够对它进行拖拽的,这就很别扭了,所以我给MouseLeftButtonDown设置了一个DragWindow处理方法。

具体的cs代码如下:

 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; namespace MESBox
{
/// <summary>
/// MEGBox.xaml 的交互逻辑
/// </summary>
public partial class MEGBox : Window
{
private static MEGBox _Instance;
public static MEGBox Instance
{
get
{
if (_Instance == null)
{
_Instance = new MEGBox();
}
return _Instance;
}
}
public MEGBox()
{
InitializeComponent();
}
public void Show(string content)
{
this.content.Text = " " + content;
this.ShowDialog();
}
private void DragWindow(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
public void CloseWindow(object sender, RoutedEventArgs args)
{ this.Close();
_Instance = null;
} }
}

代码简单易懂,也不详细说了。