using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Media; using System.Windows.Threading; using System.Windows; using MaterialDesignThemes.Wpf; namespace SlnMesnac.WPF.MessageTips { public class MessageAdorner : Adorner { private ListBox listBox; private UIElement _child; private FrameworkElement adornedElement; public MessageAdorner(UIElement adornedElement) : base(adornedElement) { this.adornedElement = adornedElement as FrameworkElement; } public void PushMessage(string message, int msgtype, int outTime) { if (listBox == null) { listBox = new ListBox() { Style = null, BorderThickness = new Thickness(0), Background = Brushes.Transparent }; listBox.IsHitTestVisible = false; Child = listBox; } Border border = new Border(); border.CornerRadius = new CornerRadius(10); border.BorderThickness = new Thickness(0); border.Opacity = 0.8; StackPanel panel = new StackPanel(); panel.Orientation = Orientation.Horizontal; panel.MaxWidth = 700; PackIcon icon = new PackIcon(); icon.Padding = new Thickness(2); icon.Margin = new Thickness(2); icon.MaxWidth = 100; icon.VerticalAlignment = VerticalAlignment.Center; icon.HorizontalAlignment = HorizontalAlignment.Center; panel.Children.Add(icon); border.Height = 100; // 设置高度 TextBlock textBlock = new TextBlock(); textBlock.Text = message; textBlock.TextWrapping = TextWrapping.Wrap; textBlock.Padding = new Thickness(5); textBlock.Margin = new Thickness(5); textBlock.MaxWidth = 600; textBlock.FontSize = 25; // 增加字体大小 textBlock.VerticalAlignment = VerticalAlignment.Center; textBlock.HorizontalAlignment = HorizontalAlignment.Center; panel.Children.Add(textBlock); border.Child = panel; if (msgtype == 0) { border.Background = Brushes.LightGreen; icon.Kind = PackIconKind.Check; //textBlock.Foreground=Brushes.Green; } if (msgtype == 1) { border.Background = Brushes.Yellow; icon.Kind = PackIconKind.Alert; //textBlock.Foreground = Brushes.Yellow; } if (msgtype == 2) { border.Background = Brushes.Red; icon.Kind = PackIconKind.Close; //textBlock.Foreground = Brushes.Red; } var item = new MessageItem { Content = border }; var timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(outTime); timer.Tick += (sender, e) => { listBox.Items.Remove(item); timer.Stop(); }; listBox.Items.Insert(0, item); timer.Start(); } public UIElement Child { get => _child; set { if (value == null) { RemoveVisualChild(_child); _child = value; return; } AddVisualChild(value); _child = value; } } protected override int VisualChildrenCount { get { return _child != null ? 1 : 0; } } protected override Size ArrangeOverride(Size finalSize) { var x = (adornedElement.ActualWidth - _child.DesiredSize.Width) / 2; _child.Arrange(new Rect(new Point(x, 20), _child.DesiredSize)); return finalSize; } protected override Visual GetVisualChild(int index) { if (index == 0 && _child != null) return _child; return base.GetVisualChild(index); } } }