Toasts – Blame information for rev 1
?pathlinks?
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 | { |
||
12 | public partial class Notification : Form |
||
13 | { |
||
14 | #region Static Fields and Constants |
||
15 | |||
16 | private static readonly List<Notification> OpenNotifications = new List<Notification>(); |
||
17 | |||
18 | #endregion |
||
19 | |||
20 | #region Constructors, Destructors and Finalizers |
||
21 | |||
22 | /// <summary> |
||
23 | /// </summary> |
||
24 | /// <param name="title"></param> |
||
25 | /// <param name="body"></param> |
||
26 | /// <param name="duration"></param> |
||
27 | /// <param name="image"></param> |
||
28 | /// <param name="animation"></param> |
||
29 | /// <param name="direction"></param> |
||
30 | public Notification(string title, string body, int duration, Image logo, Image background, |
||
31 | FormAnimator.AnimationMethod animation, FormAnimator.AnimationDirection direction) |
||
32 | { |
||
33 | InitializeComponent(); |
||
34 | |||
35 | lifeTimer.Interval = duration; |
||
36 | labelTitle.Text = title; |
||
37 | labelBody.Text = body; |
||
38 | imageBox.Image = logo; |
||
39 | BackgroundImage = background; |
||
40 | |||
41 | _animator = new FormAnimator(this, animation, direction, 500); |
||
42 | |||
43 | Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20)); |
||
44 | } |
||
45 | |||
46 | #endregion |
||
47 | |||
48 | #region Public Methods |
||
49 | |||
50 | /// <summary> |
||
51 | /// Displays the form |
||
52 | /// </summary> |
||
53 | /// <remarks> |
||
54 | /// Required to allow the form to determine the current foreground window before being displayed |
||
55 | /// </remarks> |
||
56 | public new void Show() |
||
57 | { |
||
58 | // Determine the current foreground window so it can be reactivated each time this form tries to get the focus |
||
59 | _currentForegroundWindow = NativeMethods.GetForegroundWindow(); |
||
60 | |||
61 | base.Show(); |
||
62 | } |
||
63 | |||
64 | #endregion |
||
65 | |||
66 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
67 | |||
68 | private readonly FormAnimator _animator; |
||
69 | |||
70 | private bool _allowFocus; |
||
71 | |||
72 | private IntPtr _currentForegroundWindow; |
||
73 | |||
74 | #endregion |
||
75 | |||
76 | #region Event Handlers |
||
77 | |||
78 | private void Notification_Load(object sender, EventArgs e) |
||
79 | { |
||
80 | // Display the form just above the system tray. |
||
81 | Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width, |
||
82 | Screen.PrimaryScreen.WorkingArea.Height - Height); |
||
83 | |||
84 | // Move each open form upwards to make room for this one |
||
85 | foreach (var openForm in OpenNotifications) openForm.Top -= Height; |
||
86 | |||
87 | OpenNotifications.Add(this); |
||
88 | lifeTimer.Start(); |
||
89 | } |
||
90 | |||
91 | private void Notification_Activated(object sender, EventArgs e) |
||
92 | { |
||
93 | // Prevent the form taking focus when it is initially shown |
||
94 | if (!_allowFocus) |
||
95 | // Activate the window that previously had focus |
||
96 | NativeMethods.SetForegroundWindow(_currentForegroundWindow); |
||
97 | } |
||
98 | |||
99 | private void Notification_Shown(object sender, EventArgs e) |
||
100 | { |
||
101 | // Once the animation has completed the form can receive focus |
||
102 | _allowFocus = true; |
||
103 | |||
104 | // Close the form by sliding down. |
||
105 | _animator.Duration = 0; |
||
106 | _animator.Direction = FormAnimator.AnimationDirection.Down; |
||
107 | } |
||
108 | |||
109 | private void Notification_FormClosed(object sender, FormClosedEventArgs e) |
||
110 | { |
||
111 | // Move down any open forms above this one |
||
112 | foreach (var openForm in OpenNotifications) |
||
113 | { |
||
114 | if (openForm == this) |
||
115 | // Remaining forms are below this one |
||
116 | break; |
||
117 | |||
118 | openForm.Top += Height; |
||
119 | } |
||
120 | |||
121 | OpenNotifications.Remove(this); |
||
122 | } |
||
123 | |||
124 | private void LifeTimer_Tick(object sender, EventArgs e) |
||
125 | { |
||
126 | Close(); |
||
127 | } |
||
128 | |||
129 | private void Notification_Click(object sender, EventArgs e) |
||
130 | { |
||
131 | Close(); |
||
132 | } |
||
133 | |||
134 | private void LabelTitle_Click(object sender, EventArgs e) |
||
135 | { |
||
136 | Close(); |
||
137 | } |
||
138 | |||
139 | private void LabelRO_Click(object sender, EventArgs e) |
||
140 | { |
||
141 | Close(); |
||
142 | } |
||
143 | |||
144 | #endregion |
||
145 | } |
||
146 | } |