如何在WPF中为消息框设置不同样式的字体

问题描述 投票:0回答:3

如何才能非常轻松地获得以下样式的消息框? MessageBox 函数 似乎无法解决问题

enter image description here

wpf messagebox
3个回答
4
投票

您可以使用窗口制作自己的通知对话框

这是我刚刚整理的一个简单示例

用途:

  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
    }

}

Xaml:

<Window x:Class="WpfApplication8.NotificationDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="239" Width="417" Title="{Binding Title}" ResizeMode="NoResize" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="223*"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <Button Content="Close" HorizontalAlignment="Right" Margin="0,0,10,7" Grid.Row="1" VerticalAlignment="Bottom" Width="75" Click="Button_Close_Click"/>
        <StackPanel Margin="70,10,10,0">
            <TextBlock Text="{Binding Caption}" FontSize="20" Foreground="DarkBlue" TextWrapping="Wrap"/>
            <TextBlock Margin="0,10,0,0" Text="{Binding Message}" TextWrapping="Wrap"/>
        </StackPanel>
        <Image Source="{Binding Image}" Stretch="None" HorizontalAlignment="Left" Height="52" Margin="6,10,0,0" VerticalAlignment="Top" Width="55"/>
    </Grid>
</Window>

结果: enter image description here


0
投票

我认为这种风格没有官方框架支持,但构建此类消息框的指南(包括该屏幕截图中使用的特定字体)在这里:用户界面文本

另请参阅此 CodeProject 文章:WPF TaskDialog 包装器和模拟器。显然,它使用本机 Windows 7 调用来生成正确的样式。


0
投票

当我通过 Alt+Tab 移动表单时,您的自定义消息框已失去焦点或所有者表单。

但是当我通过Alt+Tab移动表单时,MessageBox并没有丢失。

如何在一个表单中设置仅焦点自定义消息框?

© www.soinside.com 2019 - 2024. All rights reserved.