Toasts – Rev 15
?pathlinks?
// =====COPYRIGHT=====
// Code originally retrieved from http://www.vbforums.com/showthread.php?t=547778 - no license information supplied
// =====COPYRIGHT=====
using System;
using System.Collections.Concurrent;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Windows.Forms;
using Toasts.Properties;
namespace Toasts
{
public partial class ToastForm : Form
{
#region Static Fields and Constants
private static readonly ConcurrentDictionary<IntPtr, ToastForm> OpenNotifications = new ConcurrentDictionary<IntPtr, ToastForm>();
#endregion
#region Private Overrides
protected override bool ShowWithoutActivation => true;
protected override CreateParams CreateParams
{
get
{
var baseParams = base.CreateParams;
const int WS_EX_NOACTIVATE = 0x08000000;
const int WS_EX_TOOLWINDOW = 0x00000080;
const int WS_EX_TOPMOST = 0x00000008;
baseParams.ExStyle |= WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST;
return baseParams;
}
}
#endregion
#region Constructors, Destructors and Finalizers
private ToastForm()
{
InitializeComponent();
foreach (var control in Controls.Cast<Control>())
{
control.Click += Toast_Click;
}
Click += Toast_Click;
_toastTimer = new System.Timers.Timer();
_toastTimer.Elapsed += ToastTimer_Elapsed;
_toastTimer.Enabled = false;
}
/// <summary>
/// Display a toast with a title, body and a logo indefinitely on screen.
/// </summary>
/// <param name="title">the toast title</param>
/// <param name="body">the toast body</param>
/// <param name="logo">the toast logo</param>
public ToastForm(string title, string body, Image logo) : this()
{
_toastTimer.Enabled = false;
labelTitle.Text = title;
labelBody.Text = body;
imageBox.Image = logo;
_animator = new FormAnimator(this, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up,
500);
Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
}
/// <summary>
/// Display a toast on screen.
/// </summary>
/// <param name="title">the toast title</param>
/// <param name="body">the toast body</param>
/// <param name="duration">the duration in milliseconds to display the toast on screen for</param>
/// <param name="logo">the toast logo</param>
/// <param name="background">the background image for the toast</param>
/// <param name="sound">a sound to play when the toast is displayed</param>
/// <param name="animation">the form animation type <see cref="FormAnimator"/></param>
/// <param name="direction">the form animation direction <see cref="FormAnimator"/></param>
public ToastForm(string title, string body, int duration, Image logo, Image background, byte[] sound,
FormAnimator.AnimationMethod animation, FormAnimator.AnimationDirection direction) : this()
{
_toastTimer.Enabled = true;
_toastTimer.Interval = duration;
labelTitle.Text = title;
labelBody.Text = body;
imageBox.Image = logo;
BackgroundImage = background;
_sound = sound;
_animator = new FormAnimator(this, animation, direction, 500);
Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
}
/// <summary>
/// Display a toast on screen.
/// </summary>
/// <param name="title">the toast title</param>
/// <param name="body">the toast body</param>
/// <param name="duration">the duration in milliseconds to display the toast on screen for</param>
/// <param name="logo">the toast logo</param>
/// <param name="sound">a sound to play when the toast is displayed</param>
/// <param name="animation">the form animation type <see cref="FormAnimator"/></param>
/// <param name="direction">the form animation direction <see cref="FormAnimator"/></param>
public ToastForm(string title, string body, int duration, Image logo, byte[] sound,
FormAnimator.AnimationMethod animation, FormAnimator.AnimationDirection direction) : this()
{
_toastTimer.Enabled = true;
_toastTimer.Interval = duration;
labelTitle.Text = title;
labelBody.Text = body;
imageBox.Image = logo;
_sound = sound;
_animator = new FormAnimator(this, animation, direction, 500);
Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
}
/// <summary>
/// Display a toast on screen.
/// </summary>
/// <param name="title">the toast title</param>
/// <param name="body">the toast body</param>
/// <param name="duration">the duration in milliseconds to display the toast on screen for</param>
/// <param name="logo">the toast logo</param>
/// <param name="sound">a sound to play when the toast is displayed</param>
public ToastForm(string title, string body, int duration, Image logo, byte[] sound) : this()
{
_toastTimer.Enabled = true;
_toastTimer.Interval = duration;
labelTitle.Text = title;
labelBody.Text = body;
imageBox.Image = logo;
_sound = sound;
_animator = new FormAnimator(this, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up,
500);
Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
}
/// <summary>
/// Display a toast on screen.
/// </summary>
/// <param name="title">the toast title</param>
/// <param name="body">the toast body</param>
/// <param name="duration">the duration in milliseconds to display the toast on screen for</param>
/// <param name="logo">the toast logo</param>
public ToastForm(string title, string body, int duration, Image logo) : this()
{
_toastTimer.Enabled = true;
_toastTimer.Interval = duration;
labelTitle.Text = title;
labelBody.Text = body;
imageBox.Image = logo;
_animator = new FormAnimator(this, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up,
500);
Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
}
/// <summary>
/// Display a toast on screen.
/// </summary>
/// <param name="title">the toast title</param>
/// <param name="body">the toast body</param>
/// <param name="duration">the duration in milliseconds to display the toast on screen for</param>
/// <param name="logo">the toast logo</param>
public ToastForm(string title, string body, int duration, Icon logo) : this()
{
_toastTimer.Interval = duration;
labelTitle.Text = title;
labelBody.Text = body;
imageBox.Image = logo.ToBitmap();
_animator = new FormAnimator(this, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up,
500);
Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && components != null) components.Dispose();
base.Dispose(disposing);
}
#endregion
#region Private Delegates, Events, Enums, Properties, Indexers and Fields
private readonly FormAnimator _animator;
private byte[] _sound;
private readonly System.Timers.Timer _toastTimer;
#endregion
#region Event Handlers
private void Notification_Load(object sender, EventArgs e)
{
// Play the sound.
if (_sound == null)
using (var memoryStream = new MemoryStream())
{
Resources.normal.CopyTo(memoryStream);
memoryStream.Position = 0L;
_sound = memoryStream.ToArray();
}
using (var memoryStream = new MemoryStream(_sound))
{
using (var player = new SoundPlayer(memoryStream))
{
player.Play();
}
}
// Move each open form upwards to make room for this one
foreach (var openForm in OpenNotifications.Values)
{
if (openForm.InvokeRequired)
{
void Move()
{
openForm.Top -= Height;
}
openForm.Invoke((Action)Move);
continue;
}
openForm.Top -= Height;
}
// Display the form just above the system tray.
Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width,
Screen.PrimaryScreen.WorkingArea.Height - Height);
OpenNotifications.TryAdd(Handle, this);
// Only start the timer if it has been enabled in the constructor.
if (_toastTimer.Enabled)
{
_toastTimer.Start();
}
}
private void Notification_Shown(object sender, EventArgs e)
{
// Close the form by sliding down.
_animator.Duration = 0;
_animator.Direction = FormAnimator.AnimationDirection.Down;
}
private void Notification_FormClosed(object sender, FormClosedEventArgs e)
{
// Move down any open forms above this one
foreach (var openForm in OpenNotifications.Values)
{
if (openForm.Handle == Handle)
continue;
if (openForm.InvokeRequired)
{
void Move()
{
openForm.Top += Height;
}
openForm.Invoke((Action)Move);
continue;
}
openForm.Top += Height;
}
OpenNotifications.TryRemove(Handle, out _);
}
private void ToastTimer_Elapsed(object sender, EventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new MethodInvoker(Close));
return;
}
Close();
}
private void Toast_Click(object sender, EventArgs e)
{
Close();
}
#endregion
}
}
Generated by GNU Enscript 1.6.5.90.