相信很多人用过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. R语言之词云:wordcloud&wordcloud2安装及参数说明

    一.wordcloud安装说明 install.packages("wordcloud"); 二.wordcloud2安装说明 install.packages("dev ...

  2. C#后台弹出对话框

    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='jav ...

  3. RxJava基本流程和lift源码分析

    1.subscribe过程 2.lift过程

  4. [原创]Android从xml加载到View对象过程解析

    我们从Activity的setContentView()入手,开始源码解析, //Activity.setContentView public void setContentView(int layo ...

  5. windows 程序设计自学:添加图标资源

    #include <windows.h> #include "resource.h" LRESULT CALLBACK MyWndProc( HWND hwnd, // ...

  6. dell 电脑关闭触摸板的。

    桌面计算机(点击右键)----管理----设备管理器-----鼠标------选择触摸板(ps/2 兼容鼠标)---右击------跟新驱动-------浏览计算机查找------从计算机列表中选择- ...

  7. 如何在IIS7或IIS7.5中导入导出站点及应用程序池.

    为实现负载平衡,我们可能会使用多个WEB服务器,也就会需要给多个IIS配置同样的站点和应用程序池.那么我们需要一个一个的重新建吗?当然不用,我们只需要一些简单的命令就可以在IIS7(Windows S ...

  8. Application MyTest has not been registered. This is either due to a require() error during initialization or failure to call AppRegistry.registerComponent.

    运行react-native项目时报错. 说明一下:项目本来是好的,再次运行就报错了 解决解决办法倒是有,不过具体什么原因不知道.希望有知道具体原因的童鞋能够补充一下 第一种情况:真的是注册的时候写错 ...

  9. 动态密码卡TOTP算法

    TOTP NET实现:http://googleauthcsharp.codeplex.com/ 引用:http://www.cnblogs.com/wangxin201492/p/5030943.h ...

  10. Nginx 单机百万QPS环境搭建

    一.背景 最近公司在做一些物联网产品,物物通信用的是MQTT协议,内部权限与内部关系等业务逻辑准备用HTTP实现.leader要求在本地测试中要模拟出百万用户同时在线的需求.虽然该产品最后不一定有这么 ...