欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于WPF實現(xiàn)帶蒙版的MessageBox消息提示框

 更新時間:2022年08月09日 14:24:45   作者:WPFDevelopersOrg  
這篇文章主要介紹了如何利用WPF實現(xiàn)帶蒙版的MessageBox消息提示框,文中的示例代碼講解詳細,對我們學習或工作有一定幫助,需要的可以參考一下

介紹

框架使用大于等于.NET40

Visual Studio 2022;

項目使用 MIT 開源許可協(xié)議;

Nuget Install-Package WPFDevelopers.Minimal 3.2.6-preview

MessageBox

實現(xiàn)MessageBoxShow五種方法;

  • Show(string messageBoxText) 傳入Msg參數(shù);
  • Show(string messageBoxText, string caption) 傳入Msg標題參數(shù);
  • Show(string messageBoxText, string caption, MessageBoxButton button) 傳入Msg與標題、操作按鈕參數(shù);
  • Show(string messageBoxText, string caption, MessageBoxImage icon) 傳入Msg與標題、消息圖片參數(shù);
  • Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon) 傳入Msg與標題、操作按鈕、消息圖片參數(shù);

拿到父級Window窗體的內(nèi)容Content,放入一個Grid里,再在容器里放入一個半透明的Grid,最后將整個Grid賦給父級Window窗體的內(nèi)容Content;

實現(xiàn)代碼

一、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<Window>().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 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib"
                    xmlns:wpfsc="clr-namespace:WPFDevelopers.Minimal.Controls">
    
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="../Themes/Basic/ControlBasic.xaml"/>
        <ResourceDictionary Source="../Themes/Basic/Animations.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="{x:Type wpfsc:WPFMessageBox}">
        <Setter Property="Foreground" Value="{DynamicResource PrimaryTextSolidColorBrush}" />
        <Setter Property="Background"  Value="{DynamicResource WhiteSolidColorBrush}" />
        <Setter Property="BorderBrush" Value="{DynamicResource PrimaryNormalSolidColorBrush}" />
        <Setter Property="SizeToContent"  Value="WidthAndHeight" />
        <Setter Property="ResizeMode" Value="NoResize" />
        <Setter Property="ShowInTaskbar" Value="False" />
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="UseLayoutRounding" Value="True" />
        <Setter Property="TextOptions.TextFormattingMode" Value="Display" />
        <Setter Property="TextOptions.TextRenderingMode" Value="ClearType" />
        <Setter Property="WindowStyle"  Value="None" />
        <Setter Property="FontFamily" Value="{DynamicResource NormalFontFamily}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type wpfsc:WPFMessageBox}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition />
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Grid Grid.Row="0">
                                <DockPanel Margin="20,0,0,0">
                                    <TextBlock x:Name="PART_Title"
                                           HorizontalAlignment="Left"
                                           VerticalAlignment="Center" 
                                           FontSize="{DynamicResource TitleFontSize}"
                                           Foreground="{DynamicResource PrimaryTextSolidColorBrush}"/>
                                    <Button Name="PART_CloseButton" Margin="0,6" 
                                            ToolTip="Close" HorizontalAlignment="Right"
                                            IsTabStop="False" Style="{DynamicResource WindowButtonStyle}">
                                        <Path Width="10" Height="10"
                              HorizontalAlignment="Center"
                              VerticalAlignment="Center"
                              Data="{DynamicResource PathMetroWindowClose}"
                              Fill="{DynamicResource PrimaryTextSolidColorBrush}"
                              Stretch="Fill" />
                                    </Button>
                                </DockPanel>
                            </Grid>
                            <Grid Grid.Row="1" Margin="20">
                                <DockPanel>
                                    <Path x:Name="PART_Path" Data="{DynamicResource PathInformation}"
                                      Fill="{DynamicResource PrimaryNormalSolidColorBrush}"
                                      Height="25" Width="25" Stretch="Fill"></Path>
                                    <TextBlock x:Name="PART_Message" TextWrapping="Wrap" 
                                           MaxWidth="500" Width="Auto" VerticalAlignment="Center"
                                           FontSize="{DynamicResource NormalFontSize}"
                                           Padding="10,0"
                                           Foreground="{DynamicResource RegularTextSolidColorBrush}"/>
                                </DockPanel>
                            </Grid>
                            <Grid Grid.Row="2" Margin="140,20,10,10">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                                    <Button x:Name="PART_ButtonCancel" Content="取消" Visibility="Collapsed"/>
                                    <Button x:Name="PART_ButtonOK" Style="{DynamicResource PrimaryButton}" 
                                        Margin="10,0,0,0" Content="確認"/>
                                </StackPanel>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

三、WPFMessageBox.cs 代碼如下;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WPFDevelopers.Minimal.Controls
{
    [TemplatePart(Name = TitleTemplateName, Type = typeof(TextBlock))]
    [TemplatePart(Name = CloseButtonTemplateName, Type = typeof(Button))]
    [TemplatePart(Name = MessageTemplateName, Type = typeof(TextBlock))]
    [TemplatePart(Name = ButtonCancelTemplateName, Type = typeof(Button))]
    [TemplatePart(Name = ButtonCancelTemplateName, Type = typeof(Button))]
    [TemplatePart(Name = PathTemplateName, Type = typeof(Path))]
    public sealed class WPFMessageBox : Window
    {

        private const string TitleTemplateName = "PART_Title";
        private const string CloseButtonTemplateName = "PART_CloseButton";
        private const string MessageTemplateName = "PART_Message";
        private const string ButtonCancelTemplateName = "PART_ButtonCancel";
        private const string ButtonOKTemplateName = "PART_ButtonOK";
        private const string PathTemplateName = "PART_Path";

        private string _messageString;
        private string _titleString;
        private Geometry _geometry;
        private SolidColorBrush _solidColorBrush;
        private Visibility _cancelVisibility = Visibility.Collapsed;
        private Visibility _okVisibility;

        private TextBlock _title;
        private TextBlock _message;
        private Button _closeButton;
        private Button _buttonCancel;
        private Button _buttonOK;
        private Path _path;


        static WPFMessageBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WPFMessageBox), new FrameworkPropertyMetadata(typeof(WPFMessageBox)));
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _title = GetTemplateChild(TitleTemplateName) as TextBlock;
            _message = GetTemplateChild(MessageTemplateName) as TextBlock;

            if (_title == null || _message == null)
                throw new InvalidOperationException("the title or message control is null!");

            _title.Text = _titleString;
            _message.Text = _messageString;
            _path = GetTemplateChild(PathTemplateName) as Path;
            if (_path != null)
            {
                _path.Data = _geometry;
                _path.Fill = _solidColorBrush;
            }
            _closeButton = GetTemplateChild(CloseButtonTemplateName) as Button;
            if (_closeButton != null)
                _closeButton.Click += _closeButton_Click;
            _buttonCancel = GetTemplateChild(ButtonCancelTemplateName) as Button;
            if (_buttonCancel != null)
            {
                _buttonCancel.Visibility = _cancelVisibility;
                _buttonCancel.Click += _buttonCancel_Click;
            }
            _buttonOK = GetTemplateChild(ButtonOKTemplateName) as Button;
            if (_buttonOK != null)
            {
                _buttonOK.Visibility = _okVisibility;
                _buttonOK.Click += _buttonOK_Click;
            }
            if (Owner == null)
            {
                BorderThickness = new Thickness(1);
                WindowStartupLocation = WindowStartupLocation.CenterScreen;
            }
        }

        private void _buttonOK_Click(object sender, RoutedEventArgs e)
        {
            Result = MessageBoxResult.OK;
            Close();
        }

        private void _buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            Result = MessageBoxResult.Cancel;
            Close();
        }

        private void _closeButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            if (Owner == null)
                return;
            var grid = Owner.Content as Grid;
            UIElement original = VisualTreeHelper.GetChild(grid, 0) as UIElement;
            grid.Children.Remove(original);
            Owner.Content = original;
        }

        public MessageBoxResult Result { get; set; }

        public WPFMessageBox(string message)
        {
            
            _messageString = message;
        }

        public WPFMessageBox(string message, string caption)
        {
            _titleString = caption;
            _messageString = message;
           
        }

        public WPFMessageBox(string message, string caption, MessageBoxButton button)
        {
            _titleString = caption;
            _messageString = message; ;
        }

        public WPFMessageBox(string message, string caption, MessageBoxImage image)
        {
            _titleString = caption;
            _messageString = message;
            DisplayImage(image);
        }

        public WPFMessageBox(string message, string caption, MessageBoxButton button, MessageBoxImage image)
        {
            _titleString = caption;
            _messageString = message;
            DisplayImage(image);
            DisplayButtons(button);
        }

        private void DisplayButtons(MessageBoxButton button)
        {
            switch (button)
            {
                case MessageBoxButton.OKCancel:
                case MessageBoxButton.YesNo:
                    _cancelVisibility = Visibility.Visible;
                    _okVisibility = Visibility.Visible;
                    break;
                //case MessageBoxButton.YesNoCancel:
                //    break;
                default:
                    _okVisibility = Visibility.Visible;
                    break;
            }
        }
        private void DisplayImage(MessageBoxImage image)
        {
            switch (image)
            {
                case MessageBoxImage.Warning:
                    _geometry = Application.Current.Resources["PathWarning"] as Geometry;
                    _solidColorBrush = Application.Current.Resources["WarningSolidColorBrush"] as SolidColorBrush;
                    break;
                case MessageBoxImage.Error:
                    _geometry = Application.Current.Resources["PathError"] as Geometry;
                    _solidColorBrush = Application.Current.Resources["DangerSolidColorBrush"] as SolidColorBrush;
                    break;
                case MessageBoxImage.Information:
                    _geometry = Application.Current.Resources["PathWarning"] as Geometry;
                    _solidColorBrush = Application.Current.Resources["SuccessSolidColorBrush"] as SolidColorBrush;
                    break;
                case MessageBoxImage.Question:
                    _geometry = Application.Current.Resources["PathQuestion"] as Geometry;
                    _solidColorBrush = Application.Current.Resources["PrimaryNormalSolidColorBrush"] as SolidColorBrush;
                    break;
                default:
                    break;
            }
        }

    }
}

Nuget Install-Package WPFDevelopers.Minimal

以上就是基于WPF實現(xiàn)帶蒙版的MessageBox消息提示框的詳細內(nèi)容,更多關(guān)于WPF消息提示框的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#微信開發(fā)之發(fā)送模板消息

    C#微信開發(fā)之發(fā)送模板消息

    這篇文章主要為大家詳細介紹了C#微信開發(fā)之發(fā)送模板消息的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 提權(quán)函數(shù)之RtlAdjustPrivilege()使用說明

    提權(quán)函數(shù)之RtlAdjustPrivilege()使用說明

    RtlAdjustPrivilege() 這玩意是在 NTDLL.DLL 里的一個不為人知的函數(shù),MS沒有公開,原因就是這玩意實在是太NB了,以至于不需要任何其他函數(shù)的幫助,僅憑這一個函數(shù)就可以獲得進程ACL的任意權(quán)限!
    2011-06-06
  • 雜談try-catch-finally異常處理

    雜談try-catch-finally異常處理

    這篇文章主要介紹了雜談try-catch-finally異常處理的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • C#.NET學習筆記5 C#中的條件編譯

    C#.NET學習筆記5 C#中的條件編譯

    條件編譯是C#比Java多出的東西,但我跟前輩請教后,他們都說條件編譯在實際的項目開發(fā)中不怎么使用.鑒于是新內(nèi)容,我還是做做筆記,理解一下好了
    2012-11-11
  • js驗證電話號碼手機號碼的正則表達式

    js驗證電話號碼手機號碼的正則表達式

    本篇文章主要是對js驗證電話號碼手機號碼的正則表達式進行了介紹。需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • Repeater控件綁定的三種方式

    Repeater控件綁定的三種方式

    Repeater 控件用于顯示重復(fù)的信息,這些信息被綁定在該控件上。一般項目中經(jīng)常出現(xiàn)三種使用方式
    2013-05-05
  • C#實現(xiàn)餐飲管理系統(tǒng)

    C#實現(xiàn)餐飲管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C#實現(xiàn)餐飲管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • 深入解析C#編程中struct所定義的結(jié)構(gòu)

    深入解析C#編程中struct所定義的結(jié)構(gòu)

    這篇文章主要介紹了C#編程中struct所定義的結(jié)構(gòu),與C++一樣,C#語言同時擁有類和結(jié)構(gòu),需要的朋友可以參考下
    2016-01-01
  • C#實現(xiàn)QQ窗口抖動效果

    C#實現(xiàn)QQ窗口抖動效果

    這篇文章主要為大家詳細介紹了C#實現(xiàn)QQ窗口抖動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • c#如何使用UDP進行聊天通信

    c#如何使用UDP進行聊天通信

    這篇文章主要介紹了c#如何使用UDP進行聊天通信問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06

最新評論