WPF实现消息提醒(广告弹窗)
2021-03-11 05:27
标签:anim cli rop uefi span hit oid str reading
1.先上效果图: 2.1t提示框界面。 主窗口界面没什么内容,就放了一个触发按钮。先绘制通知窗口(一个关闭按钮,俩个文本控件),可以设置下ResizeMode="NoResize" WindowStyle="None" Topmost="True", 去掉窗口标题,并使提示窗口始终处于最上层。 2.2主窗口后台代码。 2.3消息提醒窗后台代码 3.源码下载地址 https://files-cdn.cnblogs.com/files/king10086/NoticeDemo.7z WPF实现消息提醒(广告弹窗) 标签:anim cli rop uefi span hit oid str reading 原文地址:https://www.cnblogs.com/lonelyxmas/p/12652497.htmlNotifyData类是弹出窗口的推送消息。
List
GetTopFrom负责计算弹出窗口距离屏幕顶端的高度。
Button_Click 传递消息,弹出窗口。
class NotifyData
{
public string Title { get; set; }
public string Content { get; set; }
}
public partial class MainWindow : Window
{
int i = 0;
public static List
NotificationWindow_Loaded 接收传过来的内容TopFrom和NotifyData,然后确定弹出窗口位置,并在5S后关闭窗口。
Button_Click 关闭窗口
public partial class Window1 : Window
{
public double TopFrom
{
get; set;
}
public Window1()
{
InitializeComponent();
this.Loaded += NotificationWindow_Loaded;
}
private void NotificationWindow_Loaded(object sender, RoutedEventArgs e)
{
NotifyData data = this.DataContext as NotifyData;
if (data != null)
{
tbContent.Text = data.Content;
tbTitle.Text = data.Title;
}
Window1 self = sender as Window1;
if (self!=null)
{
double right=SystemParameters.WorkArea.Right-10;//工作区最右边的值
self.Top = TopFrom - 160;
DoubleAnimation animation = new DoubleAnimation();
animation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
animation.From = right;
animation.To = right - self.ActualWidth;
self.BeginAnimation(Window.LeftProperty, animation);
Task.Factory.StartNew(delegate
{
int seconds = 5;//通知持续5s后消失
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(seconds));
//Invoke到主进程中去执行
this.Dispatcher.Invoke(delegate
{
animation = new DoubleAnimation();
animation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
animation.Completed += (s, a) => { self.Close(); };//动画执行完毕,关闭当前窗体
animation.From = right - self.ActualWidth;
animation.To = right;//通知从左往右收回
self.BeginAnimation(Window.LeftProperty, animation);
});
});
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
double right = SystemParameters.WorkArea.Right;
DoubleAnimation animation = new DoubleAnimation();
animation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
animation.Completed += (s, a) => { this.Close(); };
animation.From = right - this.ActualWidth;
animation.To = right;
this.BeginAnimation(Window.LeftProperty, animation);
}
}