WPF 制作一个文字漂浮提示框-cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GroupServer
{
    /// <summary>
    /// MsgTip.xaml 的交互逻辑
    /// </summary>
    public partial class MsgTip : Window
    {
        public MsgTip()
        {
            InitializeComponent();
        }

        public void ShowTxt(string msg, int delaytime = 3000)
        {
            labTxt.Content = msg;
            Show();
            _ = DelayClose(msg, delaytime);
        }

        async Task DelayClose(string msg,int delaytime = 3000)
        {
            await Task.Delay(delaytime);
            Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;
            this.Top = mainWindow.Top + (mainWindow.Height - Height) / 2;
            this.Left = mainWindow.Left + (mainWindow.Width - Width) / 2;
        }
    }
}

WindowLoad是为了让窗口在应用主界面中心显示
我们只要调用ShowTxt函数就可以了。

public static void ShowTip(string msg, int delaytime = 3000)
{
    MsgTip tip = new MsgTip();
    tip.ShowTxt(msg, delaytime);
}
上一篇:docker-compose mongodb 副本集


下一篇:IAR全面支持芯驰科技E3系列车规MCU产品E3119/E3118