当前位置: 首页 > news >正文

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);
}
http://www.lryc.cn/news/397335.html

相关文章:

  • Node.js_fs模块
  • 使用 Vue 3 实现打字机效果
  • unordered_map和set
  • java:运用字节缓冲输入流将文件中的数据写到集合中
  • 【机器学习】支持向量机与主成分分析在机器学习中的应用
  • SpringBoot项目架构实战之“网关zuul搭建“
  • 发挥储能系统领域优势,海博思创坚定不移推动能源消费革命
  • matlab R2016b安装cplex12.6,测试时cplex出现出现内部错误的解决方法
  • C#中的Dictionary
  • VSCode中多行文本的快速前后缩进
  • C# 8.0 新语法的学习和使用
  • 数据结构——约瑟夫环C语言链表实现
  • 【MyBatis】——入门基础知识必会内容
  • react父调用子的方法,子调用父的方法
  • C#知识|账号管理系统:UI层-添加账号窗体设计思路及流程。
  • 【机器学习】初学者经典案例(随记)
  • 进阶版智能家居系统Demo[C#]:整合AI和自动化
  • IC后端设计中的shrink系数设置方法
  • 在NVIDIA Jetson平台离线部署大模型
  • 51单片机嵌入式开发:8、 STC89C52RC 操作LCD1602原理
  • 数字化时代的供应链管理综合解决方案
  • CentOS 安装 annie/lux,以及 annie/lux 的使用
  • 拥抱UniHttp,规范Http接口对接之旅
  • Python 给存入 Redis 的键值对设置过期时间
  • 在linux中安装docker
  • 【JVM-04】线上CPU100%
  • try catch 解决大问题
  • 手动解析Collection
  • list模拟实现【C++】
  • nginx正向代理、反向代理、负载均衡