• WPF 实现带蒙版的 MessageBox 消息提示框


    🚀 优质资源分享 🚀

    学习路线指引(点击解锁)知识定位人群定位
    🧡 Python实战微信订餐小程序 🧡进阶级本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。
    💛Python量化交易实战💛入门级手把手带你打造一个易扩展、更安全、效率更高的量化交易系统

    WPF 实现带蒙版的 MessageBox 消息提示框

    WPF 实现带蒙版的 MessageBox 消息提示框

    作者:WPFDevelopersOrg

    原文链接: https://github.com/WPFDevelopersOrg/WPFDevelopers.Minimal

    • 框架使用大于等于.NET40
    • Visual Studio 2022;
    • 项目使用 MIT 开源许可协议;
    • Nuget Install-Package WPFDevelopers.Minimal 3.2.6-preview

    MessageBox

    • 实现MessageBoxShow五种方法;
      • Show(string messageBoxText) 传入Msg参数;
      • Show(string messageBoxText, string caption) 传入Msg标题参数;
      • Show(string messageBoxText, string caption, MessageBoxButton button) 传入Msg标题操作按钮参数;
      • Show(string messageBoxText, string caption, MessageBoxImage icon) 传入Msg标题消息图片参数;
      • Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon) 传入Msg标题操作按钮消息图片参数;
    • 拿到父级Window窗体的内容Content,放入一个Grid里,再在容器里放入一个半透明的Grid,最后将整个Grid赋给父级Window窗体的内容Content

    一、MessageBox.cs 代码如下;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    
    namespace WPFDevelopers.Minimal.Controls
    {
        public static class MessageBox
     {
            public static MessageBoxResult Show(string messageBoxText)
            {
                var msg = new WPFMessageBox(messageBoxText);
                return GetWindow(msg);
            }
            public static MessageBoxResult Show(string messageBoxText, string caption)
            {
                var msg = new WPFMessageBox(messageBoxText, caption);
                return GetWindow(msg);
            }
            public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button)
            {
                var msg = new WPFMessageBox(messageBoxText, caption, button);
                return GetWindow(msg);
            }
            public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxImage icon)
            {
                var msg = new WPFMessageBox(messageBoxText, caption, icon);
                return GetWindow(msg);
            }
            public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon)
            {
                var msg = new WPFMessageBox(messageBoxText, caption,button,icon);
                return GetWindow(msg);
            }
    
            static MessageBoxResult GetWindow(WPFMessageBox msg)
            {
                msg.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                Window win = null;
                if (Application.Current.Windows.Count > 0)
                    win = Application.Current.Windows.OfType().FirstOrDefault(o => o.IsActive);
     if (win != null)
     {
     var layer = new Grid() { Background = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0)) };
     UIElement original = win.Content as UIElement;
     win.Content = null;
     var container = new Grid();
     container.Children.Add(original);
     container.Children.Add(layer);
     win.Content = container;
     msg.Owner = win;
     msg.ShowDialog();
     container.Children.Clear();
     win.Content = original;
     }
     else
     msg.Show();
     return msg.Result;
     }
     }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    二、Styles.MessageBox.xaml 代码如下;

    
        
        
            
            
        ResourceDictionary.MergedDictionaries>