Toasts – Diff between revs 21 and 24
?pathlinks?
| Rev 21 | Rev 24 | |||
|---|---|---|---|---|
| Line 17... | Line 17... | |||
| 17 | |
17 | |
|
| 18 | namespace Toasts |
18 | namespace Toasts |
|
| 19 | { |
19 | { |
|
| 20 | public partial class ToastForm : Form |
20 | public partial class ToastForm : Form |
|
| - | 21 | { |
||
| - | 22 | #region Public Fields and Properties |
||
| - | 23 | public bool EnableChime { get; set; } = true; |
||
| - | 24 | |
||
| - | 25 | public int LingerTime { get; set; } = 5000; |
||
| - | 26 | |
||
| - | 27 | public FormAnimator.AnimationMethod AnimationMethod { get; set; } = FormAnimator.AnimationMethod.Slide; |
||
| - | 28 | |
||
| - | 29 | public FormAnimator.AnimationDirection AnimationDirection { get; set; } = FormAnimator.AnimationDirection.Up; |
||
| - | 30 | |
||
| - | 31 | public int AnimationDuration { get; set; } = 500; |
||
| - | 32 | |
||
| - | 33 | public byte[] Chime { get; set; } |
||
| - | 34 | |
||
| - | 35 | /// <summary> |
||
| - | 36 | /// |
||
| - | 37 | /// </summary> |
||
| - | 38 | /// <remarks>clone the image for safety</remarks> |
||
| - | 39 | public Image Image |
||
| - | 40 | { |
||
| - | 41 | get => new Bitmap(imageBox.Image); |
||
| - | 42 | set => imageBox.Image = new Bitmap(value); |
||
| - | 43 | } |
||
| - | 44 | |
||
| - | 45 | #endregion |
||
| 21 | { |
46 | |
|
| Line 22... | Line 47... | |||
| 22 | #region Static Fields and Constants |
47 | #region Static Fields and Constants |
|
| Line 23... | Line 48... | |||
| 23 | |
48 | |
|
| Line 56... | Line 81... | |||
| 56 | Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20)); |
81 | Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20)); |
|
| Line 57... | Line 82... | |||
| 57 | |
82 | |
|
| 58 | _screenWidth = Screen.PrimaryScreen.WorkingArea.Width; |
83 | _screenWidth = Screen.PrimaryScreen.WorkingArea.Width; |
|
| Line 59... | Line 84... | |||
| 59 | _screenHeight = Screen.PrimaryScreen.WorkingArea.Height; |
84 | _screenHeight = Screen.PrimaryScreen.WorkingArea.Height; |
|
| 60 | |
- | ||
| Line 61... | Line 85... | |||
| 61 | _formAnimator = new FormAnimator(this, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up, |
85 | |
|
| 62 | 500); |
86 | _formAnimator = new FormAnimator(this, AnimationMethod, AnimationDirection, AnimationDuration); |
|
| 63 | |
87 | |
|
| Line 64... | Line 88... | |||
| 64 | using (var memoryStream = new MemoryStream()) |
88 | using (var memoryStream = new MemoryStream()) |
|
| Line 65... | Line 89... | |||
| 65 | { |
89 | { |
|
| 66 | Resources.normal.CopyTo(memoryStream); |
90 | Resources.normal.CopyTo(memoryStream); |
|
| Line 67... | Line 91... | |||
| 67 | |
91 | |
|
| Line 68... | Line 92... | |||
| 68 | memoryStream.Position = 0L; |
92 | memoryStream.Position = 0L; |
|
| 69 | |
93 | |
|
| 70 | _sound = memoryStream.ToArray(); |
- | ||
| 71 | } |
94 | Chime = memoryStream.ToArray(); |
|
| Line 72... | Line 95... | |||
| 72 | |
95 | } |
|
| 73 | _showFormTaskCompletionSource = new TaskCompletionSource<object>(); |
96 | |
|
| 74 | |
97 | _showFormTaskCompletionSource = new TaskCompletionSource<object>(); |
|
| 75 | _toastTimer = new System.Timers.Timer(); |
98 | |
|
| 76 | _toastTimer.Elapsed += ToastTimer_Elapsed; |
99 | _toastTimer = new System.Timers.Timer { Enabled = true, AutoReset = false, Interval = LingerTime }; |
|
| 77 | _toastTimer.Enabled = false; |
100 | _toastTimer.Elapsed += ToastTimer_Elapsed; |
|
| 78 | } |
101 | } |
|
| 79 | |
- | ||
| 80 | /// <summary> |
- | ||
| 81 | /// Display a toast with a title, body and a logo indefinitely on screen. |
- | ||
| 82 | /// </summary> |
- | ||
| 83 | /// <param name="title">the toast title</param> |
- | ||
| 84 | /// <param name="body">the toast body</param> |
- | ||
| 85 | /// <param name="logo">the toast logo</param> |
- | ||
| 86 | public ToastForm(string title, string body, Image logo) : this() |
- | ||
| 87 | { |
- | ||
| 88 | _toastTimer.Enabled = false; |
- | ||
| 89 | labelTitle.Text = title; |
- | ||
| 90 | labelBody.Text = body; |
- | ||
| 91 | imageBox.Image = logo; |
- | ||
| 92 | } |
- | ||
| 93 | |
- | ||
| 94 | /// <summary> |
- | ||
| 95 | /// Display a toast on screen. |
- | ||
| 96 | /// </summary> |
- | ||
| 97 | /// <param name="title">the toast title</param> |
- | ||
| 98 | /// <param name="body">the toast body</param> |
- | ||
| 99 | /// <param name="duration">the duration in milliseconds to display the toast on screen for</param> |
102 | |
|
| 100 | /// <param name="logo">the toast logo</param> |
- | ||
| 101 | /// <param name="background">the background image for the toast</param> |
- | ||
| 102 | /// <param name="sound">a sound to play when the toast is displayed</param> |
103 | /// <summary> |
|
| 103 | /// <param name="animation">the form animation type <see cref="FormAnimator"/></param> |
104 | /// Display a toast with a title, body and a logo indefinitely on screen. |
|
| 104 | /// <param name="direction">the form animation direction <see cref="FormAnimator"/></param> |
- | ||
| 105 | public ToastForm(string title, string body, int duration, Image logo, Image background, byte[] sound, |
- | ||
| 106 | FormAnimator.AnimationMethod animation, FormAnimator.AnimationDirection direction) : this() |
- | ||
| 107 | { |
- | ||
| 108 | _toastTimer.Enabled = true; |
- | ||
| 109 | _toastTimer.Interval = duration; |
- | ||
| 110 | labelTitle.Text = title; |
- | ||
| 111 | labelBody.Text = body; |
- | ||
| 112 | imageBox.Image = logo; |
- | ||
| 113 | BackgroundImage = background; |
- | ||
| 114 | |
- | ||
| 115 | _sound = sound; |
- | ||
| 116 | |
- | ||
| 117 | _formAnimator = new FormAnimator(this, animation, direction, 500); |
- | ||
| 118 | } |
- | ||
| 119 | |
- | ||
| 120 | /// <summary> |
- | ||
| 121 | /// Display a toast on screen. |
- | ||
| 122 | /// </summary> |
- | ||
| 123 | /// <param name="title">the toast title</param> |
- | ||
| 124 | /// <param name="body">the toast body</param> |
- | ||
| 125 | /// <param name="duration">the duration in milliseconds to display the toast on screen for</param> |
- | ||
| 126 | /// <param name="logo">the toast logo</param> |
- | ||
| 127 | /// <param name="sound">a sound to play when the toast is displayed</param> |
- | ||
| 128 | /// <param name="animation">the form animation type <see cref="FormAnimator"/></param> |
- | ||
| 129 | /// <param name="direction">the form animation direction <see cref="FormAnimator"/></param> |
- | ||
| 130 | public ToastForm(string title, string body, int duration, Image logo, byte[] sound, |
- | ||
| 131 | FormAnimator.AnimationMethod animation, FormAnimator.AnimationDirection direction) : this() |
- | ||
| 132 | { |
- | ||
| 133 | _toastTimer.Enabled = true; |
- | ||
| 134 | _toastTimer.Interval = duration; |
- | ||
| 135 | labelTitle.Text = title; |
- | ||
| 136 | labelBody.Text = body; |
- | ||
| 137 | imageBox.Image = logo; |
- | ||
| 138 | |
- | ||
| 139 | _sound = sound; |
- | ||
| 140 | |
- | ||
| 141 | _formAnimator = new FormAnimator(this, animation, direction, 500); |
- | ||
| 142 | } |
- | ||
| 143 | |
- | ||
| 144 | /// <summary> |
- | ||
| 145 | /// Display a toast on screen. |
- | ||
| 146 | /// </summary> |
- | ||
| 147 | /// <param name="title">the toast title</param> |
- | ||
| 148 | /// <param name="body">the toast body</param> |
- | ||
| 149 | /// <param name="duration">the duration in milliseconds to display the toast on screen for</param> |
- | ||
| 150 | /// <param name="logo">the toast logo</param> |
- | ||
| 151 | /// <param name="sound">a sound to play when the toast is displayed</param> |
- | ||
| 152 | public ToastForm(string title, string body, int duration, Image logo, byte[] sound) : this() |
- | ||
| 153 | { |
- | ||
| 154 | _toastTimer.Enabled = true; |
- | ||
| 155 | _toastTimer.Interval = duration; |
- | ||
| 156 | labelTitle.Text = title; |
- | ||
| 157 | labelBody.Text = body; |
- | ||
| 158 | imageBox.Image = logo; |
- | ||
| 159 | |
- | ||
| 160 | _sound = sound; |
- | ||
| 161 | } |
- | ||
| 162 | |
- | ||
| 163 | /// <summary> |
- | ||
| 164 | /// Display a toast on screen. |
- | ||
| 165 | /// </summary> |
- | ||
| 166 | /// <param name="title">the toast title</param> |
- | ||
| 167 | /// <param name="body">the toast body</param> |
- | ||
| 168 | /// <param name="duration">the duration in milliseconds to display the toast on screen for</param> |
- | ||
| 169 | /// <param name="logo">the toast logo</param> |
- | ||
| 170 | public ToastForm(string title, string body, int duration, Image logo) : this() |
- | ||
| 171 | { |
- | ||
| 172 | _toastTimer.Enabled = true; |
- | ||
| 173 | _toastTimer.Interval = duration; |
- | ||
| 174 | labelTitle.Text = title; |
- | ||
| 175 | labelBody.Text = body; |
- | ||
| 176 | imageBox.Image = logo; |
- | ||
| 177 | } |
- | ||
| 178 | |
- | ||
| 179 | /// <summary> |
- | ||
| 180 | /// Display a toast on screen. |
- | ||
| 181 | /// </summary> |
- | ||
| 182 | /// <param name="title">the toast title</param> |
- | ||
| 183 | /// <param name="body">the toast body</param> |
- | ||
| 184 | /// <param name="duration">the duration in milliseconds to display the toast on screen for</param> |
- | ||
| 185 | /// <param name="logo">the toast logo</param> |
105 | /// </summary> |
|
| Line 186... | Line 106... | |||
| 186 | public ToastForm(string title, string body, int duration, Icon logo) : this() |
106 | /// <param name="title">the toast title</param> |
|
| 187 | { |
107 | /// <param name="body">the toast body</param> |
|
| 188 | _toastTimer.Enabled = true; |
108 | /// <param name="logo">the toast logo</param> |
|
| Line 204... | Line 124... | |||
| 204 | { |
124 | { |
|
| 205 | _toastTimer.Dispose(); |
125 | _toastTimer.Dispose(); |
|
| 206 | _toastTimer = null; |
126 | _toastTimer = null; |
|
| 207 | } |
127 | } |
|
| Line -... | Line 128... | |||
| - | 128 | |
||
| 208 | |
129 | Image.Dispose(); |
|
| 209 | components.Dispose(); |
130 | components.Dispose(); |
|
| 210 | } |
131 | } |
|
| 211 | base.Dispose(disposing); |
132 | base.Dispose(disposing); |
|
| Line 215... | Line 136... | |||
| 215 | |
136 | |
|
| Line 216... | Line 137... | |||
| 216 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
137 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
|
| Line 217... | Line -... | |||
| 217 | |
- | ||
| 218 | private readonly FormAnimator _formAnimator; |
- | ||
| 219 | |
138 | |
|
| Line 220... | Line 139... | |||
| 220 | private readonly byte[] _sound; |
139 | private readonly FormAnimator _formAnimator; |
|
| Line 221... | Line 140... | |||
| 221 | |
140 | |
|
| Line 231... | Line 150... | |||
| 231 | |
150 | |
|
| Line 232... | Line 151... | |||
| 232 | #region Event Handlers |
151 | #region Event Handlers |
|
| 233 | |
152 | |
|
| 234 | private void Notification_Load(object sender, EventArgs e) |
153 | private void Notification_Load(object sender, EventArgs e) |
|
| 235 | { |
154 | { |
|
| 236 | Task.Factory.StartNew(() => |
155 | if (EnableChime) |
|
| 237 | { |
156 | { |
|
| 238 | using (var memoryStream = new MemoryStream(_sound)) |
157 | Task.Factory.StartNew(() => |
|
| 239 | { |
158 | { |
|
| - | 159 | using (var memoryStream = new MemoryStream(Chime)) |
||
| - | 160 | { |
||
| 240 | using (var player = new SoundPlayer(memoryStream)) |
161 | using (var player = new SoundPlayer(memoryStream)) |
|
| - | 162 | { |
||
| 241 | { |
163 | player.Play(); |
|
| 242 | player.Play(); |
164 | } |
|
| 243 | } |
165 | } |
|
| Line 244... | Line 166... | |||
| 244 | } |
166 | }); |
|
| 245 | }); |
167 | } |
|
| 246 | |
168 | |
|
| 247 | try |
169 | try |
|
| Line 294... | Line 216... | |||
| 294 | Location = new Point(_screenWidth - Width, _screenHeight - Height); |
216 | Location = new Point(_screenWidth - Width, _screenHeight - Height); |
|
| 295 | })); |
217 | })); |
|
| Line 296... | Line 218... | |||
| 296 | |
218 | |
|
| Line 297... | Line -... | |||
| 297 | OpenNotifications.Add(this); |
- | ||
| 298 | |
- | ||
| 299 | // Only start the timer if it has been enabled in the constructor. |
- | ||
| 300 | if (_toastTimer.Enabled) |
219 | OpenNotifications.Add(this); |
|
| 301 | { |
- | ||
| 302 | _toastTimer.Start(); |
220 | |
|
| 303 | } |
221 | _toastTimer.Start(); |
|
| 304 | } |
222 | } |
|
| 305 | } |
223 | } |
|
| 306 | catch |
224 | catch |
|