WPF实现消息提醒(广告弹窗)

更新时间:2023-07-01 19:50:20 阅读: 评论:0

WPF实现消息提醒(⼴告弹窗)工商局注册
1.先上效果图:
2.1t提⽰框界⾯。主窗⼝界⾯没什么内容,就放了⼀个触发按钮。先绘制通知窗⼝(⼀个关闭按钮,俩个⽂本控件),可以设置下ResizeMode="NoResize" WindowStyle="None"  Topmost="True",
去掉窗⼝标题,并使提⽰窗⼝始终处于最上层。
<Border BorderThickness="1" BorderBrush="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="6*" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Background="White">
<Button
Margin="5"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Background="White"
BorderThickness="0"
Click="Button_Click"
Content=" × "
FontSize="18" />
<TextBlock
x:Name="tbTitle"旅游美景
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontSize="16"
Text="TextBlock"
TextWrapping="Wrap" />
<TextBlock
x:Name="tbContent"
Margin="20,40,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontSize="16"
Text="TextBlock"
TextWrapping="Wrap" />
</Grid>
<Grid Grid.Row="2" Background="#F0F0F0" />
</Grid>
</Border>
2.2主窗⼝后台代码。
NotifyData类是弹出窗⼝的推送消息。
List<Window1>计算⾼度时,如果窗⼝关闭,窗⼝就会从List中移除。
GetTopFrom负责计算弹出窗⼝距离屏幕顶端的⾼度。
Button_Click 传递消息,弹出窗⼝。
class NotifyData
{
public string Title { get; t; }
public string Content { get; t; }
}
public partial class MainWindow : Window
{
int i = 0;鸿门宴原文及翻译
public static List<Window1> _dialogs = new List<Window1>();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object nder, RoutedEventArgs e)
{
i++;
NotifyData data = new NotifyData();微信网站登录
data.Title = "提⽰";
data.Content = "XXX余额不⾜,XXX余额不⾜,XXX余额不⾜,XXX余额不⾜" + i;
Window1 dialog = new Window1();//new ⼀个通知
dialog.Clod += Dialog_Clod;孩子呕吐吃什么药
dialog.TopFrom = GetTopFrom();
dialog.DataContext = data;//设置通知⾥要显⽰的数据
dialog.Show();
_dialogs.Add(dialog);
}
private void Dialog_Clod(object nder, EventArgs e)
{
var clodDialog = nder as Window1;
_dialogs.Remove(clodDialog);
}
double GetTopFrom()
{
//屏幕的⾼度-底部TaskBar的⾼度。
double topFrom = System.Windows.SystemParameters.WorkArea.Bottom - 10;
bool isContinueFind = _dialogs.Any(o => o.TopFrom == topFrom);
while (isContinueFind)
{
topFrom = topFrom - 160;//此处100是NotifyWindow的⾼ 160-100剩下的10  是通知之间的间距
isContinueFind = _dialogs.Any(o => o.TopFrom == topFrom);
}
if (topFrom <= 0)
topFrom = System.Windows.SystemParameters.WorkArea.Bottom-10;
return topFrom;
}
}
2.3消息提醒窗后台代码
NotificationWindow_Loaded 接收传过来的内容TopFrom和NotifyData,然后确定弹出窗⼝位置,并在5S后关闭窗⼝。Button_Click 关闭窗⼝
public partial class Window1 : Window
{
public double TopFrom
{
get; t;
}
public Window1()
{
InitializeComponent();
this.Loaded += NotificationWindow_Loaded;
}
private void NotificationWindow_Loaded(object nder, RoutedEventArgs e)
{
NotifyData data = this.DataContext as NotifyData;
if (data != null)
{成都有哪些好耍的地方
tbContent.Text = data.Content;
tbTitle.Text = data.Title;
}
Window1 lf = nder as Window1;
if (lf!=null)
老豆腐怎么做{
double right=SystemParameters.WorkArea.Right-10;//⼯作区最右边的值
lf.Top = TopFrom - 160;
DoubleAnimation animation = new DoubleAnimation();
animation.Duration = new Duration(TimeSpan.FromMilliconds(500));
animation.From = right;
animation.To = right - lf.ActualWidth;
lf.BeginAnimation(Window.LeftProperty, animation);
Task.Factory.StartNew(delegate习俗英文
{
int conds = 5;//通知持续5s后消失
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(conds));
/
/Invoke到主进程中去执⾏
this.Dispatcher.Invoke(delegate
{
animation = new DoubleAnimation();
animation.Duration = new Duration(TimeSpan.FromMilliconds(500));
animation.Completed += (s, a) => { lf.Clo(); };//动画执⾏完毕,关闭当前窗体                        animation.From = right - lf.ActualWidth;
animation.To = right;//通知从左往右收回
lf.BeginAnimation(Window.LeftProperty, animation);
});
});
}
}
private void Button_Click(object nder, RoutedEventArgs e)
{
double right = SystemParameters.WorkArea.Right;
DoubleAnimation animation = new DoubleAnimation();
animation.Duration = new Duration(TimeSpan.FromMilliconds(500));
animation.Completed += (s, a) => { this.Clo(); };
animation.From = right - this.ActualWidth;
animation.To = right;
this.BeginAnimation(Window.LeftProperty, animation);
}
}
3.源码下载地址

本文发布于:2023-07-01 19:50:20,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1063557.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:弹出   通知   消息   提醒
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图