模拟MessageBox
原文:《模拟MessageBox》 Posted on 2014/01/07
================================================================================
这段时间在用WPF开发项目。界面采用的是类似Win8系统的Metro风格,但是系统自带MessageBox貌似不能被Style,于是重写MessageBox。
之前一直没有写过自定义控件,就不知道怎么下手。于是从网上找了好些资料,发现了这篇博文《WPF 自定义 MessageBox (相对完善版)》。看了代码解释并运行了Demo,觉得根据实际需要改造下应该可以用到项目里来。
以前一直很好奇为什么能Style很多控件,而MessageBox却不能Style,查过.NET Reflector,发现:
public sealed class MessageBox : System.Object
MessageBox类 是直接继承自Object类,实际上是对具体细节进行了封装。
总体结构建立在Vito.K的代码基础之上,我加入的一些功能:
- 关闭按钮:
在MessageBoxModule中定义关闭命令的依赖项属性:/// <summary>
/// 关闭命令
/// </summary>
public static readonly DependencyProperty CloseCommandProperty =
DependencyProperty.Register(
"CloseCommand",
typeof(RoutedCommand),
typeof(MessageBoxModule));/// <summary>
/// 关闭命令
/// </summary>
public RoutedCommand CloseCommand
{
get { return (RoutedCommand)GetValue(CloseCommandProperty); }
set { SetValue(CloseCommandProperty, value); }
}设置方法:
/// <summary>
/// 设置关闭窗口命令
/// </summary>
private void SetupCloseCommand()
{
InputGestureCollection inputGestures =
new InputGestureCollection();
inputGestures.Add(new KeyGesture(Key.F4, ModifierKeys.Alt)); CloseCommand = new RoutedCommand(
"CloseCommand",
typeof(MessageBoxModule),
inputGestures); CommandBindings.Add(
new CommandBinding(CloseCommand, CloseCommandExecuted));
}在public MessageBoxModule()方法中调用即可。
在MessageBoxModule的Style文件中对该命令的使用:<Button x:Name="CloseButton" Command="{TemplateBinding CloseCommand}" .../> - 通过标题栏拖动MessageBox:
之前写过的无边框的Windows窗体都是通过Rectangle的MouseLeftButtonDown事件来触发拖动窗体的,然后这个方法在这边行不通,于是又用这方法,还是行不通:<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{TemplateBinding DragOverCommand}"/>
</i:EventTrigger/>
</i:Interaction.Triggers>无意间看到《WPF编程宝典 C# 2010版》第18章中的无外观控件代码中的TemplatePart。照猫画虎地写下了以下代码:
在Style文件中:<Rectangle x:Name="PART_TitleRectangle" .../>
在MessageBoxModule类中:
在类前面加上:[TemplatePart(Name="PART_TitleRectangle", Type=typeof(Rectangle))]
internal sealed class MessageBoxModule : Window
{ }重载public void OnApplyTemplate()方法:
public override void OnApplyTemplate()
{
base.OnApplyTemplate(); // 为TitleRectangle添加MouseLeftButtonDown事件处理程序
Rectangle rect = GetTemplateChild("PART_TitleRectangle") as Rectangle;
if (rect != null)
{
rect.MouseLeftButtonDown += (sender, e) => { DragMove(); };
}
} - 显示详细信息:
主要是Style中几个相关控件的Visibility的设置,依据是MessageBoxModule类中的指定值,即DependencyProperty MessageDetailProperty是否为空来选择是否启用显示相信信息功能:<DockPanel x:Name="RootLayoutPanel">
...
<Separator Visibility="{Binding Path=Visibility, ElementName=MessageDetailTextBlock}" DockPanel.Dock="Bottom"/>
<!--信息区-->
<DockPanel x:Name="ContentDockPanel" DockPanel.Dock="Top">
...
<TextBlock x:Name="MessageTextBlock" Text="{TemplateBinding Message}" DockPanel.Dock="Top" />
<Grid Visibility="{Binding Path=IsChecked, ElementName=DetailInfoToggleButton, Converter={converter:BooleanToVisibilityConverter}}" >
<TextBlock x:Name="MessageDetailTextBlock"
Text="{TemplateBinding MessageDetail}"
Visibility="{TemplateBinding MessageDetail, Converter={converter:StringToVisibilityConverter}}"
DockPanel.Dock="Bottom" />
</Grid>
</DockPanel>
</DockPanel> - 加入MessageBoxImage:
通过MessageBoxModule类中的DependencyProperty MessageBoxIconTypeProperty来指定显示哪种类型的图片:/// <summary>
/// 消息框图标
/// </summary>
public static readonly DependencyProperty MessageBoxIconTypeProperty =
DependencyProperty.Register(
"MessageBoxIconType",
typeof(MessageBoxIcon),
typeof(MessageBoxModule),
new PropertyMetadata(MessageBoxIcon.None));MessageBoxIcon是枚举类型类似于MessageBoxImage的一个枚举类型:
/// <summary>
/// 图标
/// </summary>
public enum MessageBoxIcon
{
None = ,
Error = 0x10,
//Hand = 0x10,
//Stop = 0x10,
Question = 0x20,
Warning = 0x30,
//Exclamation = 0x30,
//Asterisk = 0x40,
//Information = 0x40,
}在Style文件中:
<!--信息区-->
<DockPanel x:Name="ContentDockPanel" DockPanel.Dock="Top" Margin="15,10">
<Image x:Name="MB_MistakeImage" Visibility="Collapsed" Source="/Controls;component/Images/messagebox_mistake.png"/>
<Image x:Name="MB_QueryImage" Visibility="Collapsed" Source="/Controls;component/Images/messagebox_query.png"/>
<Image x:Name="MB_WarningImage" Visibility="Collapsed" Source="/Controls;component/Images/messagebox_warning.png"/>
...
</DockPanel>用ControlTemplate.Triggers进行控制对应Image的Visibility属性:
<ControlTemplate.Triggers>
<Trigger Property="MessageBoxIconType" Value="Error">
<Setter Property="Visibility" TargetName="MB_MistakeImage" Value="Visible"/>
</Trigger>
<Trigger Property="MessageBoxIconType" Value="Question">
<Setter Property="Visibility" TargetName="MB_QueryImage" Value="Visible"/>
</Trigger>
<Trigger Property="MessageBoxIconType" Value="Warning">
<Setter Property="Visibility" TargetName="MB_WarningImage" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
【END】
模拟MessageBox的更多相关文章
- WPF 自定义 MessageBox (相对完善版)
WPF 自定义 MessageBox (相对完善版) 基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当 ...
- WPF 自定义 MessageBox (相对完善版 v1.0.0.6)
基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当你不得不弹出一个消息框通知用户消息时(虽然很不建议在程序中频繁 ...
- ASP.NET中MessageBox的实现
asp.net中没有MessageBox这个控件,固然可以插入Winform里的MessageBox,但一般不提倡,所以只能变通实现,主要有这几种方法: 1.直接利用javascript的alert和 ...
- C语言嵌入式系统编程修炼之四:屏幕操作
汉字处理 现在要解决的问题是,嵌入式系统中经常要使用的并非是完整的汉字库,往往只是需要提供数量有限的汉字供必要的显示功能.例如,一个微波炉的LCD上没有必要提供显示"电子邮件"的功 ...
- C语言嵌入式系统编程修炼
C语言嵌入式系统编程修炼 2008-08-19 作者:宋宝华 来源:天极网 C语言嵌入式系统编程修炼之背景篇 本文的讨论主要围绕以通用处理器为中心的协议处理模块进行,因为它更多地牵涉到具体的C语言编程 ...
- [读书笔记2]《C语言嵌入式系统编程修炼》
第3章 屏幕操作 3.1 汉字处理 现在要解决的问题是,嵌入式系统中经常要使用的并非是完整的汉字库,往往只是需要提供数量有限的汉字供必要的显示功能.例如,一个微波炉的LCD上没有必要提供显示&qu ...
- c# winform 自动关闭messagebox 模拟回车
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 一个简单的Webservice的demo,简单模拟服务
前段时间一直在学习WCF,匆匆忙忙的把<WCF全面解析>和<WCF服务编程>看了一遍,好多东西都不是很懂,又听了一下WCF分布式开发的网络教程,算是马马虎虎的明白点了.回顾了一 ...
- NetworkComms V3 模拟登陆
演示NetworkComms V3的用法 例子很简单 界面如下: 服务器端代码: 开始监听: //服务器开始监听客户端的请求 Connection.StartListening(ConnectionT ...
随机推荐
- JavaScript简介及基础知识(1)
1.JavaScript是什么—它是个脚本语言,需要宿主文件,它的宿主文件是html文件. Javascript是一种脚本语言,比HTML要复杂.不过即便你先前不懂编程,也不用担心,因为Javascr ...
- HTML5语义元素
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python之Django--ORM连表操作
一对多 class UserType(models.Model): caption = models.CharField(max_length=32) class UserInfo(models.Mo ...
- android: 服务的基本用法
9.3 服务的基本用法 了解了 Android 多线程编程的技术之后,下面就让我们进入到本章的正题,开始对服务 的相关内容进行学习.作为 Android 四大组件之一,服务也少不了有很多非常重要的 ...
- phpweb成品网站最新版(注入、上传、写shell)
注入:之所以鸡肋就是该漏洞利用安装文件 重新生成 配置文件 写入可执行代码 鸡肋1: 具有破坏性 动作非常大 重新写了配置文件 数据库连接文件鸡肋2: 有一定安全常识的站长都会删掉 install 目 ...
- Java的自动装箱和拆箱的简单讲解
装箱就是把基础类型封装成一个类.比如把int封装成Integer,这时你就不能把他当成一个数了,而是一个类了,对他的操作就需要用它的方法了. 拆箱就是把类转换成基础类型.比如你算个加法什么的是不能用 ...
- 数据标准化 Normalization
数据的标准化(normalization)是将数据按比例缩放,使之落入一个小的特定区间.在某些比较和评价的指标处理中经常会用到,去除数据的单位限制,将其转化为无量纲的纯数值,便于不同单位或量级的指标能 ...
- Codeforces Beta Round #17 C. Balance DP
C. Balance 题目链接 http://codeforces.com/contest/17/problem/C 题面 Nick likes strings very much, he likes ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- 在redis一致性hash(shard)中使用lua脚本的坑
redis 2.8之前的版本,为了实现支持巨量数据缓存或者持久化,一般需要通过redis sharding模式来实现redis集群,普遍大家使用的是twitter开源的Twemproxy. twemp ...