石家庄网站建设网站建设,凡科网站投票排行榜是怎么做的,官方网站下载微博,wordpress中科大字体WPF里面的消息提示一般都是MessageBox.Show()#xff0c;这种样式不是很好看#xff0c;所以就想办法重新搞了一个类似弹出消息的功能。原理很简单#xff0c;就是弹出一个新窗体#xff0c;然后等几秒窗体自动关闭。  
先上效果图#xff1a; 新建一个MsgHelper.cs类这种样式不是很好看所以就想办法重新搞了一个类似弹出消息的功能。原理很简单就是弹出一个新窗体然后等几秒窗体自动关闭。  
先上效果图 新建一个MsgHelper.cs类然后全局可以调用。 
using System.Windows;
using System.Windows.Threading;namespace WpfApp1;public static class MsgHelper
{/// summary/// 弹出消息/// /summary/// param namemsg/param/// param namemsgState/parampublic static void ShowMsg(string msg, MsgState msgState  MsgState.Info){//调用之前先关闭可能已打开的窗口CloseWindow();_timer  new DispatcherTimer();_timer.Interval  TimeSpan.FromSeconds(2);_timer.Tick  _timer_Tick;_timer.Start();var mainWindow  Application.Current.Windows.OfTypeMainWindow().FirstOrDefault();var window  new MsgWindow(msg, msgState) { Owner  mainWindow };window.Show();}/// summary/// 定时器/// /summarystatic DispatcherTimer _timer;private static void _timer_Tick(object? sender, EventArgs e){CloseWindow();}/// summary/// 关闭窗体/// /summarypublic static void CloseWindow(){if (_timer ! null){_timer.Stop();}var msgWindow  Application.Current.Windows.OfTypeMsgWindow().FirstOrDefault();msgWindow?.Close();}/// summary/// 消息类型/// /summarypublic enum MsgState{Info,Success,Error}
} 新建MsgWindow窗体用于显示消息。样式可以自己写的好看一点我这里就没做美化了。 
MsgWindow.xaml代码如下 
Window x:ClassWpfApp1.MsgWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfApp1mc:IgnorabledTitleMsgWindow LoadedWindow_Loaded Height40 Width300 BackgroundTransparentShowInTaskbarFalse WindowStyleNone AllowsTransparencyTrue WindowStartupLocationCenterScreen Border BorderBrushDarkBlue BorderThickness1Grid BackgroundWhite Namegrid TextBlock Text VerticalAlignmentCenter HorizontalAlignmentCenter NametxtMsg Margin5/TextBlock/Grid/Border
/Window 
MsgWindow.xaml.cs 
using System.Windows;
using System.Windows.Media;
using static WpfApp1.MsgHelper;namespace WpfApp1
{/// summary/// MsgWindow.xaml 的交互逻辑/// /summarypublic partial class MsgWindow : Window{private string _msg  ;private MsgState _msgState  MsgState.Info;public MsgWindow(string msg, MsgState msgState){InitializeComponent();_msg  msg;_msgState  msgState;}private void Window_Loaded(object sender, RoutedEventArgs e){txtMsg.Text  _msg;switch (_msgState){case MsgState.Info:grid.Background  new SolidColorBrush(Colors.LightGray);break;case MsgState.Success:grid.Background  new SolidColorBrush(Colors.Green);txtMsg.Foreground  new SolidColorBrush(Colors.White);break;case MsgState.Error:grid.Background  new SolidColorBrush(Colors.Red);txtMsg.Foreground  new SolidColorBrush(Colors.White);break;}}}
}然后在调用。我这里是在主窗体里面调用的其他窗体里调用是一样的。 
MainWindow.xaml代码如下 
Window x:ClassWpfApp1.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfApp1mc:IgnorabledTitleMainWindow Height450 Width800GridGrid.RowDefinitionsRowDefinition/RowDefinition/Grid.RowDefinitionsGrid.ColumnDefinitionsColumnDefinition/ColumnDefinitionColumnDefinition/ColumnDefinitionColumnDefinition/ColumnDefinition/Grid.ColumnDefinitionsButton Contentinfo Width100 Height100 ClickButtonBase_OnClick Taginfo/ButtonButton Contentsuccess Grid.Row0 Grid.Column1 Width100 Height100 Tagsuccess ClickButtonBase_OnClick/ButtonButton Contenterror Grid.Row0 Grid.Column2 Width100 Height100 Tagerror ClickButtonBase_OnClick/Button/Grid
/WindowMainWindow.xaml.cs 
using System.Windows;
using System.Windows.Controls;
using static WpfApp1.MsgHelper;namespace WpfApp1;/// summary
/// Interaction logic for MainWindow.xaml
/// /summary
public partial class MainWindow
{public MainWindow(){InitializeComponent();}private void ButtonBase_OnClick(object sender, RoutedEventArgs e){if (sender is Button btn){var tag  btn.Tag.ToString();var infoType  MsgState.Info;switch (tag){case info:infoType  MsgState.Info;break;case success:infoType  MsgState.Success;break;case error:infoType  MsgState.Error;break;default:break;}MsgHelper.ShowMsg(DateTime.Now.ToString(), infoType);}}
} 这里要注意一个问题在调用ShowMsg()方法有个owner要指定我这里给的是主窗体因为主窗体一般不会被关闭如果是在其他窗体里面可以把这个方法再加个参数然后调用的地方传个this就可以。 还有就是MsgWindow的窗体的ShowInTaskbar 要设置为false不然当鼠标移到任务栏的程序图标时会显示有两个窗体。 
也可以将MsgWindow的Topmost设置为true这样就肯定是在最上层但是这个最上层是所有软件的最上层感觉不是很好。可以根据自己的需求来。 
超时时间自己设置我这里设置的是2秒。