NotificationDialog.ShowNotification("Backup and Restor center"
, "You need to be an Administrator to run backup"
, "Use Fast User Switching to switch to an account with administrator privileges, or log off and log on as an administrator"
, NotifyIcon.Exclamation);
字符串
产品代码:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
namespace WpfApplication8
{
/// <summary>
/// Interaction logic for NotificationDialog.xaml
/// </summary>
public partial class NotificationDialog : Window, INotifyPropertyChanged
{
public static void ShowNotification(string title, string caption, string message, NotifyIcon icon)
{
NotificationDialog dialog = new NotificationDialog();
dialog.Title = title;
dialog.Caption = caption;
dialog.Message = message;
dialog.Image = dialog.GetIcon(icon);
dialog.ShowDialog();
}
private string _caption;
private string _message;
private BitmapSource _image;
private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x80000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
public NotificationDialog()
{
DataContext = this;
InitializeComponent();
Loaded += (s, e) =>
{
var hwnd = new WindowInteropHelper(this).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
};
}
public string Caption
{
get { return _caption; }
set { _caption = value; NotifyPropertyChanged(); }
}
public string Message
{
get { return _message; }
set { _message = value; NotifyPropertyChanged(); }
}
public BitmapSource Image
{
get { return _image; }
set { _image = value; NotifyPropertyChanged(); }
}
private BitmapSource GetIcon(NotifyIcon iconType)
{
Icon icon = (Icon)typeof(SystemIcons).GetProperty(iconType.ToString(), BindingFlags.Public | BindingFlags.Static).GetValue(null, null);
return Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
private void Button_Close_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName]string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public enum NotifyIcon
{
Application,
Asterisk,
Error,
Exclamation,
Hand,
Information,
Question,
Shield,
Warning,
WinLogo
}
}
2条答案
按热度按时间wqsoz72f1#
您可以使用Window创建自己的通知对话框
这里有一个简单的例子,我刚刚拼凑在一起
用法:
字符串
产品代码:
型
Xaml:
型
结果:
r7xajy2e2#
我不认为有官方的框架支持这种风格,但是构建这样的消息框的指导方针(包括截图中使用的特定字体)在这里:User Interface Text。
另请参阅这篇CodeProject文章:WPF TaskDialog Wrapper and Emulator。显然,它使用本机Windows 7调用来生成正确的样式。