Toasts – Blame information for rev 4

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  
40 /// <summary>
41 /// </summary>
42 /// <param name="title"></param>
43 /// <param name="body"></param>
44 /// <param name="duration"></param>
45 /// <param name="image"></param>
46 /// <param name="animation"></param>
47 /// <param name="direction"></param>
2 office 48 public ToastForm(string title, string body, int duration, Image logo, Image background,
1 office 49 FormAnimator.AnimationMethod animation, FormAnimator.AnimationDirection direction)
50 {
51 InitializeComponent();
52  
53 lifeTimer.Interval = duration;
54 labelTitle.Text = title;
55 labelBody.Text = body;
56 imageBox.Image = logo;
57 BackgroundImage = background;
58  
59 _animator = new FormAnimator(this, animation, direction, 500);
60  
61 Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
62 }
63  
2 office 64 public ToastForm(string title, string body, int duration, Image logo,
65 FormAnimator.AnimationMethod animation, FormAnimator.AnimationDirection direction)
66 {
67 InitializeComponent();
1 office 68  
2 office 69 lifeTimer.Interval = duration;
70 labelTitle.Text = title;
71 labelBody.Text = body;
72 imageBox.Image = logo;
1 office 73  
2 office 74 _animator = new FormAnimator(this, animation, direction, 500);
75  
76 Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
77 }
78  
79 public ToastForm(string title, string body, int duration, Image logo)
1 office 80 {
2 office 81 InitializeComponent();
1 office 82  
2 office 83 lifeTimer.Interval = duration;
84 labelTitle.Text = title;
85 labelBody.Text = body;
86 imageBox.Image = logo;
87  
88 _animator = new FormAnimator(this, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up,
89 500);
90  
91 Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));
1 office 92 }
93  
94 #endregion
95  
96 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
97  
98 private readonly FormAnimator _animator;
99  
100 private bool _allowFocus;
101  
102 private IntPtr _currentForegroundWindow;
103  
104 #endregion
105  
106 #region Event Handlers
107  
108 private void Notification_Load(object sender, EventArgs e)
109 {
110 // Display the form just above the system tray.
111 Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width,
112 Screen.PrimaryScreen.WorkingArea.Height - Height);
113  
114 // Move each open form upwards to make room for this one
4 office 115 foreach (var openForm in OpenNotifications)
116 {
117 if (openForm.InvokeRequired)
118 {
119 Action action = delegate { openForm.Top -= Height; };
120 openForm.Invoke(action);
121 return;
122 }
1 office 123  
4 office 124 openForm.Top -= Height;
125 }
126  
1 office 127 OpenNotifications.Add(this);
128 lifeTimer.Start();
129 }
130  
131 private void Notification_Activated(object sender, EventArgs e)
132 {
133 // Prevent the form taking focus when it is initially shown
134 if (!_allowFocus)
135 // Activate the window that previously had focus
136 NativeMethods.SetForegroundWindow(_currentForegroundWindow);
137 }
138  
139 private void Notification_Shown(object sender, EventArgs e)
140 {
141 // Once the animation has completed the form can receive focus
142 _allowFocus = true;
143  
144 // Close the form by sliding down.
145 _animator.Duration = 0;
146 _animator.Direction = FormAnimator.AnimationDirection.Down;
147 }
148  
149 private void Notification_FormClosed(object sender, FormClosedEventArgs e)
150 {
151 // Move down any open forms above this one
152 foreach (var openForm in OpenNotifications)
153 {
154 if (openForm == this)
155 // Remaining forms are below this one
156 break;
157  
158 openForm.Top += Height;
159 }
160  
161 OpenNotifications.Remove(this);
162 }
163  
164 private void LifeTimer_Tick(object sender, EventArgs e)
165 {
166 Close();
167 }
168  
169 private void Notification_Click(object sender, EventArgs e)
170 {
171 Close();
172 }
173  
174 private void LabelTitle_Click(object sender, EventArgs e)
175 {
176 Close();
177 }
178  
179 private void LabelRO_Click(object sender, EventArgs e)
180 {
181 Close();
182 }
183  
184 #endregion
185 }
186 }