1.先上效果圖: 2.1t提示框界面。 主視窗界面沒什麼內容,就放了一個觸發按鈕。先繪製通知視窗(一個關閉按鈕,倆個文本控制項),可以設置下ResizeMode="NoResize" WindowStyle="None" Topmost="True", 去掉視窗標題,並使提示視窗始終處於最上層。 <B ...
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; set; } public string Content { get; set; } }
public partial class MainWindow : Window { int i = 0; public static List<Window1> _dialogs = new List<Window1>(); public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { i++; NotifyData data = new NotifyData(); data.Title = "提示"; data.Content = "XXX餘額不足,XXX餘額不足,XXX餘額不足,XXX餘額不足" + i; Window1 dialog = new Window1();//new 一個通知 dialog.Closed += Dialog_Closed; dialog.TopFrom = GetTopFrom(); dialog.DataContext = data;//設置通知里要顯示的數據 dialog.Show(); _dialogs.Add(dialog); } private void Dialog_Closed(object sender, EventArgs e) { var closedDialog = sender as Window1; _dialogs.Remove(closedDialog); } 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; 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); } }
3.源碼下載地址
https://files-cdn.cnblogs.com/files/king10086/NoticeDemo.7z