wpf 自定义消息框
相信很多人用过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 自定义消息框的更多相关文章
- WPF 自定义消息框(转)
相信很多人用过MessageBox.show(),是不是觉得这个消息框有点丑呢,反正我是觉得有点丑的,所以我自己重写了一个.先不说,上两幅图对比先: 当然,也不是很好看,不过比原有的好多了. 不多说了 ...
- WPF 文本框添加水印效果
有的时候我们需要为我们的WPF文本框TextBox控件添加一个显示水印的效果来增强用户体验,比如登陆的时候提示输入用户名,输入密码等情形.如下图所示: 这个时候我们除了可以修改TextBox控件的控件 ...
- WPF文本框密码框添加水印效果
WPF文本框密码框添加水印效果 来源: 阅读:559 时间:2014-12-31 分享: 0 按照惯例,先看下效果 文本框水印 文本框水印相对简单,不需要重写模板,仅仅需要一个VisualBrush ...
- WPF提示框效果
WPF提示框效果 1,新建WPF应用程序 2,添加用户控件Message 3,在Message中编写如下代码 <Border x:Name="border" BorderTh ...
- WPF 提示框、确认框、确认输入框
1.提示框 分为提示.异常.失败.成功几种类型 方法: /// <summary> /// 弹出提示 /// 标题:提示 /// </summary> /// <para ...
- 【Unity技巧】自定义消息框(弹出框)
写在前面 这一篇我个人认为还是很常用的,一开始也是实习的时候学到的,所以我觉得实习真的是一个快速学习工程技巧的途径. 提醒:这篇教程比较复杂,如果你不熟悉NGUI.iTween.C#的回调函数机制,那 ...
- WPF 文本框设置了阴影效果后,因左右的transform变化引发的拉伸渲染问题
背景 最近遇到一个动画执行时,文本位置变化的问题.如下图: 如果你仔细看的话,当星星变小时,文本往下降了几个像素. 貌似有点莫名其妙,因为控件之间并不在同一个Panel布局控件中,不存在高度限制变化引 ...
- WPF 矩形框8个控制点伸缩及拖拽
最近在研发图片控件矩形框8个控制点进行控制边框的大小.位置等信息,之前查阅了相关的信息,比如别人整合的类:ControlResizer 这个类虽然是好,但是很大程度上是有限制,换句话说,它需要你二次更 ...
- WPF密码框中禁止复制、粘贴
如题: " Margin="215,32,151,0" > <PasswordBox.CommandBindings> <CommandBindi ...
随机推荐
- vps主机修改系统远程端口号/添加防火墙
3389端口是远程终端服务端口,Windows 系统中的远程终端服务是一项功能非常强大的服务,同时也成了入侵者长驻主机的通道,入侵者可以利用一些手段得到管理员账号和密码并入侵主机. 众所周知,入侵者一 ...
- Difference between SET, SETQ and SETF in LISP
SET can set the value of symbols; SETQ can set the value of variables; SETF is a macro that will ...
- Winform中进行MD5加密
Winform,c#进行MD5加密直接上步骤: 1.添加引用 2.在.NET选项卡中找到“System.Web” 3.选中之后,点击“确定”即可,此时会在解决方案中看到刚才添加的引用 4.引用名空间: ...
- 修改windows密码后ssrs报错
昨夜修改了windows的登录密码,第二日发现ssrs全部无法访问.显示filenotfound等错误.细想一下,应该是修改了windows的密码导致ssrs权限验证失败. 因此将ssrs的服务帐号修 ...
- C primer plus 练习题 第七章
1. #include <stdio.h> #define SPACE ' ' #define NEWLINE '\n' int main() { int spaces,newlines, ...
- 【Android】如何写一个JsBridge
JsBridge 简介 Android JsBridge 就是用来在 Android app的原生 java 代码与 javascript 代码中架设通信(调用)桥梁的辅助工具. 原文地址点这里 gi ...
- 关于windows的service编程
最近需要学习下windows的service编程框架,查了下msdn发现不知所云.于是谷歌之,发现了一个非常不错的文章,重点推荐讲的非常详细,深入,看完之后基本上就能很清楚windows的servic ...
- Gson解析数组多类型元素
本文的出现是为了解决以下需求:使用Gson对json数组进行解析,但是数组里面元素的类型是多种的.数据如下: {"list":[{ "type":0, &quo ...
- 十九、【.Net开源】EFW框架核心类库之WCF控制器
回<[开源]EFW框架系列文章索引> EFW框架源代码下载V1.1:http://pan.baidu.com/s/1qWJjo3U EFW框架实例源代码下载:http://pan.baid ...
- ps图像渐变
整理自:http://zhidao.baidu.com/question/16374167.html 1.用ps打开图片 2.在切换至英文输入法状态下(下面的操作均如此)按q 快捷键q的作用是切换标准 ...