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

  

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

不多说了,先上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;
} }
}

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

wpf 自定义消息框的更多相关文章

  1. WPF 自定义消息框(转)

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

  2. WPF 文本框添加水印效果

    有的时候我们需要为我们的WPF文本框TextBox控件添加一个显示水印的效果来增强用户体验,比如登陆的时候提示输入用户名,输入密码等情形.如下图所示: 这个时候我们除了可以修改TextBox控件的控件 ...

  3. WPF文本框密码框添加水印效果

    WPF文本框密码框添加水印效果 来源: 阅读:559 时间:2014-12-31 分享: 0 按照惯例,先看下效果 文本框水印 文本框水印相对简单,不需要重写模板,仅仅需要一个VisualBrush ...

  4. WPF提示框效果

    WPF提示框效果 1,新建WPF应用程序 2,添加用户控件Message 3,在Message中编写如下代码 <Border x:Name="border" BorderTh ...

  5. WPF 提示框、确认框、确认输入框

    1.提示框 分为提示.异常.失败.成功几种类型 方法: /// <summary> /// 弹出提示 /// 标题:提示 /// </summary> /// <para ...

  6. 【Unity技巧】自定义消息框(弹出框)

    写在前面 这一篇我个人认为还是很常用的,一开始也是实习的时候学到的,所以我觉得实习真的是一个快速学习工程技巧的途径. 提醒:这篇教程比较复杂,如果你不熟悉NGUI.iTween.C#的回调函数机制,那 ...

  7. WPF 文本框设置了阴影效果后,因左右的transform变化引发的拉伸渲染问题

    背景 最近遇到一个动画执行时,文本位置变化的问题.如下图: 如果你仔细看的话,当星星变小时,文本往下降了几个像素. 貌似有点莫名其妙,因为控件之间并不在同一个Panel布局控件中,不存在高度限制变化引 ...

  8. WPF 矩形框8个控制点伸缩及拖拽

    最近在研发图片控件矩形框8个控制点进行控制边框的大小.位置等信息,之前查阅了相关的信息,比如别人整合的类:ControlResizer 这个类虽然是好,但是很大程度上是有限制,换句话说,它需要你二次更 ...

  9. WPF密码框中禁止复制、粘贴

    如题: " Margin="215,32,151,0" > <PasswordBox.CommandBindings> <CommandBindi ...

随机推荐

  1. Updating Protobuf and GRPC in Golang

    转自: http://russmatney.com/techsposure/update-protobuf-golang-grpc/ TL;DR: When protobuf updates, all ...

  2. 在Oracle Linux Server release 6.4下配置ocfs2文件系统

    ① 安装ocfs-tools-1.8 如果是使用RedHat Enterprise Linux 6.4,也可以安装ocfs-tools-1.8的,只是要插入Oracle Linux Server re ...

  3. 【转】中国正爆发聊天APP大战 未来或影响西方

    [搜狐IT消息]8月15日消息,<金融时报>报道称,在中国,聊天应用程序的竞争日渐激烈,腾讯.阿里巴巴都加入大战,在西方市场的未来竞争中,中国聊天应用可能会成为先驱. 一些分析师认为,快速 ...

  4. CSS3学习笔记--transform基于原始数据(旋转木马实例)

    参考链接:好吧,CSS3 3D transform变换,不过如此! transform-style:preserve-3d属性要在图片所在的容器(父元素)中定义,perspective定义在父子元素上 ...

  5. fedora Server 21 安装 Opera 29.0.1795.47

    最新文章:Virson's Blog 安装源: yum localinstall --nogpgcheck http://mirror.yandex.ru/fedora/russianfedora/r ...

  6. Selenium WebDriver屏幕截图(C#版)

    Selenium WebDriver屏幕截图(C#版)http://www.automationqa.com/forum.php?mod=viewthread&tid=3595&fro ...

  7. Legolas工业自动化平台入门(三)交互事件响应动作

    在上一篇Legolas工业自动化平台入门(二)数据响应动作 一文中,我们介绍了"动作"相关内容,了解到"动作"分为多种,各种动作的添加方式相同,但是应用方式各自 ...

  8. MyBatis知多少(8)关系型数据库

    MyBatis的存在就是为了简化对关系数据库的访问.数据库的确非常复杂,要正确地使用它们需要做很多的工作.数据库负责管理数据和修改数据.我们使用数据库而不简简单单地使用一个 平板文件的原因就在于数据库 ...

  9. Android中查找一个Layout中指定的子控件

    我们通常希望查找一个页面中指定类型的控件,单个控件知道id很容易找到,但是如果是多个呢?或者说是在程序中自定义的控件,且不知道id怎么办呢?如想找到页面中的Spinner,可用以下方法 /** * 从 ...

  10. LDPC编译码基本原理

    LDPC编译码基本原理     学习笔记 V1.1 2015/02/18 LDPC编译码基本原理   概述   本文是个人针对LDPC的学习笔记,主要针对LDPC译码算法做了简要的总结.该版本主要致力 ...