![在这里插入图片描述](https://img-blog.csdnimg.cn/b34df1c7249343c09801168404ebbb74.png)
using GrowlMessage.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Threading;
namespace GrowlMessage
{
public class Growl
{
private const double topOffset = 20;
private const double leftOffset = 355;
public static readonly MainWindow growlNotifications = new MainWindow();
public static void InitGrowl()
{
growlNotifications.Top = SystemParameters.WorkArea.Top + topOffset;
growlNotifications.Left = SystemParameters.WorkArea.Right - leftOffset;
}
public static void Error(string msg)
{
DispatcherCur(msg, "Image\\Error.png");
}
public static void Info(string msg)
{
DispatcherCur(msg, "Image\\Info.png");
}
public static void Message(string msg)
{
DispatcherCur(msg, "Image\\Message.png");
}
public static void NetWork(string msg)
{
DispatcherCur(msg, "Image\\Network-Error.png");
}
public static void Success(string msg)
{
DispatcherCur(msg, "Image\\Success.png");
}
public static void Warring(string msg)
{
DispatcherCur(msg, "Image\\Warring.png");
}
public static void Colse()
{
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
growlNotifications.Close();
}));
}
public static void DispatcherCur(string msg,string image)
{
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
growlNotifications.AddNotification(new Notification
{
Image = AppDomain.CurrentDomain.BaseDirectory + image,
Message = msg,
Date = DateTime.Now,
});
}));
}
}
}
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.Navigation;
using System.Windows.Shapes;
namespace GrowlMessage
{
public partial class MainWindow : Window
{
private const byte MAX_NOTIFICATIONS = 10;
private int count;
public Notifications Notifications = new Notifications();
private readonly Notifications buffer = new Notifications();
public MainWindow()
{
InitializeComponent();
NotificationsControl.DataContext = Notifications;
}
public void AddNotification(Notification notification)
{
try
{
notification.ID = count++;
if (Notifications.Count + 1 > MAX_NOTIFICATIONS)
buffer.Add(notification);
else
Notifications.Add(notification);
if (Notifications.Count > 0 && !IsActive)
Show();
}
catch (Exception ex)
{
}
}
public void RemoveNotification(Notification notification)
{
try
{
if (Notifications.Contains(notification))
Notifications.Remove(notification);
if (buffer.Count > 0)
{
Notifications.Add(buffer[0]);
buffer.RemoveAt(0);
}
if (Notifications.Count < 1)
Hide();
}
catch (Exception ex)
{
}
}
private void NotificationWindowSizeChanged(object sender, SizeChangedEventArgs e)
{
if (e.NewSize.Height != 0.0)
return;
var element = sender as Grid;
RemoveNotification(Notifications.FirstOrDefault(n => n.ID == Int32.Parse(element.Tag.ToString())));
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GrowlMessage
{
public class Notification : INotifyPropertyChanged
{
private int id;
public int ID
{
get { return id; }
set
{
if (id == value) return;
id = value;
OnPropertyChanged(nameof(ID));
}
}
private string message;
public string Message
{
get { return message; }
set
{
if (message == value) return;
message = value;
OnPropertyChanged(nameof(Message));
}
}
private DateTime? date;
public DateTime? Date
{
get { return date; }
set
{
if (date == value) return;
date = value;
OnPropertyChanged(nameof(Date));
}
}
private string image;
public string Image
{
get { return image; }
set
{
if (image == value) return;
image = value;
OnPropertyChanged(nameof(Image));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Notifications : ObservableCollection<Notification> { }
}