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

WPF好像没有自带的文字提示漂浮,我们可以定制一个。

效果如下:

xaml

xaml如下:

csharp 复制代码
<Window x:Class="GroupServer.MsgTip"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GroupServer"
        mc:Ignorable="d"
        Title="MsgTip" ResizeMode="NoResize" Background="#00FFFFFF" AllowsTransparency="True" SizeToContent="WidthAndHeight" Topmost="True" ShowInTaskbar="False" WindowStyle="None" Loaded="Window_Loaded">
    <Border  Background="#91d024" CornerRadius="10" >
        <Label x:Name="labTxt" Margin="20"   HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="18">这是一个消息提示框</Label>
    </Border>
</Window>

这里SizeToContent是适配窗口大小,Topmost是置顶,CornerRadius是圆角。

cs

csharp 复制代码
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函数就可以了。

csharp 复制代码
public static void ShowTip(string msg, int delaytime = 3000)
{
    MsgTip tip = new MsgTip();
    tip.ShowTxt(msg, delaytime);
}
相关推荐
豫狮恒14 小时前
OpenHarmony Flutter 分布式多模态交互:融合音视频、手势与环境感知的跨端体验革新
flutter·wpf·openharmony
豫狮恒17 小时前
OpenHarmony Flutter 分布式数据共享实战:从基础存储到跨设备协同
flutter·wpf·openharmony
5008419 小时前
鸿蒙 Flutter 隐私合规:用户授权中心与数据审计日志
flutter·华为·开源·wpf·音视频
豫狮恒21 小时前
OpenHarmony Flutter 分布式软总线实战:跨设备通信的核心技术与应用
flutter·wpf·harmonyos
Hello.Reader21 小时前
Flink SQL 的 LIMIT 子句语义、坑点与实战技巧
sql·flink·wpf
豫狮恒21 小时前
OpenHarmony Flutter 分布式安全防护:跨设备身份认证与数据加密传输方案
flutter·wpf·openharmony
Wnq1007221 小时前
鸿蒙 OS 与 CORBA+DDS+QOS+SOA 在工业控制领域的核心技术对比研究
物联网·性能优化·wpf·代理模式·信号处理·harmonyos·嵌入式实时数据库
豫狮恒21 小时前
OpenHarmony Flutter 分布式设备发现与组网:跨设备无感连接与动态组网方案
分布式·flutter·wpf·openharmony
豫狮恒21 小时前
OpenHarmony Flutter 分布式边缘智能:跨设备算力协同与端侧 AI 推理优化方案
wpf
豫狮恒1 天前
OpenHarmony Flutter 分布式安全防护:跨设备身份认证与数据加密方案
分布式·安全·flutter·wpf·openharmony