模拟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 ...
随机推荐
- HTML+CSS提升小实战
1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html ...
- javaweb学习总结(十九)——JSP标签
一.JSP标签介绍 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. 二.JSP常用标签 ...
- 部署rfc5766-turn-server--谷歌推荐的开源穿透服务器 [复制链接]
谷歌推荐的开源穿透服务器,包含trun和stun服务,主页:https://code.google.com/p/rfc5766-turn-server/(个人觉得可以利用这个来进一步搭建VPN,有兴趣 ...
- C#与C++之间类型的对应
Windows Data Type .NET Data Type BOOL, BOOLEAN Boolean or Int32 BSTR String BYTE Byte CHAR Char DOUB ...
- Nginx + FastCgi + Spawn-fcgi + c 的架构
参考: nginx+c/c++ fastcgi:http://www.yis.me/web/2011/11/01/66.htm cgi探索之路:http://github.tiankonguse.co ...
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 查看mysql版本的四种方法
1:在终端下:mysql -V. 以下是代码片段: [shengting@login ~]$ mysql -V mysql Ver 14.7 Distrib 4.1.10a, for redhat-l ...
- GEMR: Get the parent window for view
Window window = Utilities.GetVisualParent<Window>(this); if (window != null) { window.DialogRe ...
- ISO/IEC 9899 C语言标准(非官方翻译)
本系列博文将以ISO/IEC 9899最新的官方手册为准,然后再添加GCC以及Clang编译器对标准的扩展. 本系列博文将不仅仅是针对C编程语言(C Programming Language)标准的翻 ...
- html5media.js 让浏览器兼容<Video><Audio> 标签
介绍:https://html5media.info/ 项目:https://github.com/etianen/html5media Wiki:https://github.com/etianen ...