Toasts – Blame information for rev 2

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
115 foreach (var openForm in OpenNotifications) openForm.Top -= Height;
116  
117 OpenNotifications.Add(this);
118 lifeTimer.Start();
119 }
120  
121 private void Notification_Activated(object sender, EventArgs e)
122 {
123 // Prevent the form taking focus when it is initially shown
124 if (!_allowFocus)
125 // Activate the window that previously had focus
126 NativeMethods.SetForegroundWindow(_currentForegroundWindow);
127 }
128  
129 private void Notification_Shown(object sender, EventArgs e)
130 {
131 // Once the animation has completed the form can receive focus
132 _allowFocus = true;
133  
134 // Close the form by sliding down.
135 _animator.Duration = 0;
136 _animator.Direction = FormAnimator.AnimationDirection.Down;
137 }
138  
139 private void Notification_FormClosed(object sender, FormClosedEventArgs e)
140 {
141 // Move down any open forms above this one
142 foreach (var openForm in OpenNotifications)
143 {
144 if (openForm == this)
145 // Remaining forms are below this one
146 break;
147  
148 openForm.Top += Height;
149 }
150  
151 OpenNotifications.Remove(this);
152 }
153  
154 private void LifeTimer_Tick(object sender, EventArgs e)
155 {
156 Close();
157 }
158  
159 private void Notification_Click(object sender, EventArgs e)
160 {
161 Close();
162 }
163  
164 private void LabelTitle_Click(object sender, EventArgs e)
165 {
166 Close();
167 }
168  
169 private void LabelRO_Click(object sender, EventArgs e)
170 {
171 Close();
172 }
173  
174 #endregion
175 }
176 }