| 学习路线指引(点击解锁) | 知识定位 | 人群定位 |
|---|---|---|
| 🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
| 💛Python量化交易实战💛 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
WPF 实现带蒙版的 MessageBox 消息提示框
WPF 实现带蒙版的 MessageBox 消息提示框
作者:WPFDevelopersOrg
原文链接: https://github.com/WPFDevelopersOrg/WPFDevelopers.Minimal
.NET40;Visual Studio 2022;Install-Package WPFDevelopers.Minimal 3.2.6-previewMessageBox
MessageBox的Show五种方法;
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;
}
}
}
二、Styles.MessageBox.xaml 代码如下;
ResourceDictionary.MergedDictionaries>