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

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

效果如下:
在这里插入图片描述

xaml

xaml如下:

<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

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);
}

相关推荐

  1. C#如何实现一个输入输入,匹配提示数据

    2024-07-11 05:18:06       48 阅读
  2. WPF封装一个鼠标事件控件界面选的功能

    2024-07-11 05:18:06       53 阅读
  3. WPF之ToolTip提示

    2024-07-11 05:18:06       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 05:18:06       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 05:18:06       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 05:18:06       58 阅读
  4. Python语言-面向对象

    2024-07-11 05:18:06       69 阅读

热门阅读

  1. xml CDATA

    2024-07-11 05:18:06       22 阅读
  2. XML Schema 杂项数据类型

    2024-07-11 05:18:06       23 阅读
  3. 我的前端实习之旅

    2024-07-11 05:18:06       22 阅读
  4. 算法——二分法

    2024-07-11 05:18:06       25 阅读
  5. Python 简介

    2024-07-11 05:18:06       25 阅读
  6. 内核调试方法

    2024-07-11 05:18:06       22 阅读