Toasts – Blame information for rev 7

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 // =====COPYRIGHT=====
2 // Code originally retrieved from http://www.vbforums.com/showthread.php?t=547778 - no license information supplied
3 // =====COPYRIGHT=====
4  
5 using System;
6 using System.Collections.Generic;
7 using System.Drawing;
8 using System.Windows.Forms;
9  
10 namespace Toasts
11 {
2 office 12 public partial class ToastForm : Form
1 office 13 {
14 #region Static Fields and Constants
15  
2 office 16 private static readonly List<ToastForm> OpenNotifications = new List<ToastForm>();
1 office 17  
18 #endregion
19  
2 office 20 #region Public Methods
21  
22 /// <summary>
23 /// Displays the form
24 /// </summary>
25 /// <remarks>
26 /// Required to allow the form to determine the current foreground window before being displayed
27 /// </remarks>
28 public new void Show()
29 {
30 // Determine the current foreground window so it can be reactivated each time this form tries to get the focus
31 _currentForegroundWindow = NativeMethods.GetForegroundWindow();
32  
33 base.Show();
34 }
35  
36 #endregion
37  
1 office 38 #region Constructors, Destructors and Finalizers
39  
7 office 40 private ToastForm()
41 {
42 InitializeComponent();
43 }
44  
1 office 45 /// <summary>
46 /// </summary>
47 /// <param name="title"></param>
48 /// <param name="body"></param>
49 /// <param name="duration"></param>
50 /// <param name="image"></param>
51 /// <param name="animation"></param>
52 /// <param name="direction"></param>
2 office 53 public ToastForm(string title, string body, int duration, Image logo, Image background,
7 office 54 FormAnimator.AnimationMethod animation, FormAnimator.AnimationDirection direction) : this()
1 office 55 {
56 lifeTimer.Interval = duration;
57 labelTitle.Text = title;
58 labelBody.Text = body;
59 imageBox.Image = logo;
60 BackgroundImage = background;
61  
62 _animator = new FormAnimator(this, animation, direction, 500);
63  
64 Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
65 }
66  
2 office 67 public ToastForm(string title, string body, int duration, Image logo,
7 office 68 FormAnimator.AnimationMethod animation, FormAnimator.AnimationDirection direction) : this()
2 office 69 {
70 lifeTimer.Interval = duration;
71 labelTitle.Text = title;
72 labelBody.Text = body;
73 imageBox.Image = logo;
1 office 74  
2 office 75 _animator = new FormAnimator(this, animation, direction, 500);
76  
77 Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
78 }
79  
7 office 80 public ToastForm(string title, string body, int duration, Image logo) : this()
1 office 81 {
2 office 82 lifeTimer.Interval = duration;
83 labelTitle.Text = title;
84 labelBody.Text = body;
85 imageBox.Image = logo;
86  
87 _animator = new FormAnimator(this, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up,
88 500);
89  
90 Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
1 office 91 }
92  
7 office 93 /// <summary>
94 /// Clean up any resources being used.
95 /// </summary>
96 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
97 protected override void Dispose(bool disposing)
98 {
99 if (disposing && (components != null))
100 {
101 components.Dispose();
102 }
103 base.Dispose(disposing);
104 }
105  
1 office 106 #endregion
107  
108 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
109  
110 private readonly FormAnimator _animator;
111  
112 private bool _allowFocus;
113  
114 private IntPtr _currentForegroundWindow;
115  
116 #endregion
117  
118 #region Event Handlers
119  
120 private void Notification_Load(object sender, EventArgs e)
121 {
122 // Display the form just above the system tray.
123 Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width,
124 Screen.PrimaryScreen.WorkingArea.Height - Height);
125  
126 // Move each open form upwards to make room for this one
4 office 127 foreach (var openForm in OpenNotifications)
128 {
129 if (openForm.InvokeRequired)
130 {
131 Action action = delegate { openForm.Top -= Height; };
132 openForm.Invoke(action);
7 office 133  
134 continue;
4 office 135 }
1 office 136  
4 office 137 openForm.Top -= Height;
138 }
139  
1 office 140 OpenNotifications.Add(this);
141 lifeTimer.Start();
142 }
143  
144 private void Notification_Activated(object sender, EventArgs e)
145 {
146 // Prevent the form taking focus when it is initially shown
147 if (!_allowFocus)
7 office 148 {
1 office 149 // Activate the window that previously had focus
150 NativeMethods.SetForegroundWindow(_currentForegroundWindow);
7 office 151 }
1 office 152 }
153  
154 private void Notification_Shown(object sender, EventArgs e)
155 {
156 // Once the animation has completed the form can receive focus
157 _allowFocus = true;
158  
159 // Close the form by sliding down.
160 _animator.Duration = 0;
161 _animator.Direction = FormAnimator.AnimationDirection.Down;
162 }
163  
164 private void Notification_FormClosed(object sender, FormClosedEventArgs e)
165 {
166 // Move down any open forms above this one
167 foreach (var openForm in OpenNotifications)
168 {
169 if (openForm == this)
170 // Remaining forms are below this one
171 break;
172  
7 office 173 if (openForm.InvokeRequired)
174 {
175 Action action = delegate { openForm.Top += Height; };
176 openForm.Invoke(action);
177  
178 continue;
179 }
1 office 180 }
181  
182 OpenNotifications.Remove(this);
183 }
184  
185 private void LifeTimer_Tick(object sender, EventArgs e)
186 {
187 Close();
188 }
189  
190 private void Notification_Click(object sender, EventArgs e)
191 {
192 Close();
193 }
194  
195 private void LabelTitle_Click(object sender, EventArgs e)
196 {
197 Close();
198 }
199  
200 private void LabelRO_Click(object sender, EventArgs e)
201 {
202 Close();
203 }
204  
205 #endregion
206 }
207 }