EaseFilter Demo Project
PopupNotifier.cs
Go to the documentation of this file.
1 /*
2  * Created/modified in 2011 by Simon Baer
3  *
4  * Based on the Code Project article by Nicolas W√§lti:
5  * http://www.codeproject.com/KB/cpp/PopupNotifier.aspx
6  *
7  * Licensed under the Code Project Open License (CPOL).
8  */
9 
10 using System;
11 using System.ComponentModel;
12 using System.Windows.Forms;
13 using System.Drawing;
14 
16 {
21  [ToolboxBitmapAttribute(typeof(PopupNotifier), "Icon.ico")]
22  [DefaultEvent("Click")]
23  public class PopupNotifier : Component
24  {
28  public event EventHandler Click;
29 
33  public event EventHandler Close;
34 
35  private bool disposed = false;
36  private PopupNotifierForm frmPopup;
37  private Timer tmrAnimation;
38  private Timer tmrWait;
39 
40  private bool isAppearing = true;
41  private bool mouseIsOn = false;
42  private int maxPosition;
43  private double maxOpacity;
44  private int posStart;
45  private int posStop;
46  private double opacityStart;
47  private double opacityStop;
48  private System.Diagnostics.Stopwatch sw;
49 
50  #region Properties
51 
52  [Category("Header"), DefaultValue(typeof(Color), "ControlDark")]
53  [Description("Color of the window header.")]
54  public Color HeaderColor { get; set; }
55 
56  [Category("Appearance"), DefaultValue(typeof(Color), "Control")]
57  [Description("Color of the window background.")]
58  public Color BodyColor { get; set; }
59 
60  [Category("Title"), DefaultValue(typeof(Color), "Gray")]
61  [Description("Color of the title text.")]
62  public Color TitleColor { get; set; }
63 
64  [Category("Content"), DefaultValue(typeof(Color), "ControlText")]
65  [Description("Color of the content text.")]
66  public Color ContentColor { get; set; }
67 
68  [Category("Appearance"), DefaultValue(typeof(Color), "WindowFrame")]
69  [Description("Color of the window border.")]
70  public Color BorderColor { get; set; }
71 
72  [Category("Buttons"), DefaultValue(typeof(Color), "WindowFrame")]
73  [Description("Border color of the close and options buttons when the mouse is over them.")]
74  public Color ButtonBorderColor { get; set; }
75 
76  [Category("Buttons"), DefaultValue(typeof(Color), "Highlight")]
77  [Description("Background color of the close and options buttons when the mouse is over them.")]
78  public Color ButtonHoverColor { get; set; }
79 
80  [Category("Content"), DefaultValue(typeof(Color), "HotTrack")]
81  [Description("Color of the content text when the mouse is hovering over it.")]
82  public Color ContentHoverColor { get; set; }
83 
84  [Category("Appearance"), DefaultValue(50)]
85  [Description("Gradient of window background color.")]
86  public int GradientPower { get; set; }
87 
88  [Category("Content")]
89  [Description("Font of the content text.")]
90  public Font ContentFont { get; set; }
91 
92  [Category("Title")]
93  [Description("Font of the title.")]
94  public Font TitleFont { get; set; }
95 
96  [Category("Image")]
97  [Description("Size of the icon image.")]
98  public Size ImageSize
99  {
100  get
101  {
102  if (imageSize.Width == 0)
103  {
104  if (Image != null)
105  {
106  return Image.Size;
107  }
108  else
109  {
110  return new Size(0, 0);
111  }
112  }
113  else
114  {
115  return imageSize;
116  }
117  }
118  set { imageSize = value; }
119  }
120 
121  public void ResetImageSize()
122  {
123  imageSize = Size.Empty;
124  }
125 
126  private bool ShouldSerializeImageSize()
127  {
128  return (!imageSize.Equals(Size.Empty));
129  }
130 
131  private Size imageSize = new Size(0, 0);
132 
133  [Category("Image")]
134  [Description("Icon image to display.")]
135  public Image Image { get; set; }
136 
137  [Category("Image")]
138  [Description("Icon image to display.")]
139  public void ShowInternalImage(bool isErrorIcon, bool isAboutIcon)
140  {
141  if (isErrorIcon)
142  {
143  Image = Properties.Resources.error;
144  }
145  else if (isAboutIcon)
146  {
147  Image = Properties.Resources.about;
148  }
149  }
150 
151  [Category("Header"), DefaultValue(true)]
152  [Description("Whether to show a 'grip' image within the window header.")]
153  public bool ShowGrip { get; set; }
154 
155  [Category("Behavior"), DefaultValue(true)]
156  [Description("Whether to scroll the window or only fade it.")]
157  public bool Scroll { get; set; }
158 
159  [Category("Content")]
160  [Description("Content text to display.")]
161  public string ContentText { get; set; }
162 
163  [Category("Title")]
164  [Description("Title text to display.")]
165  public string TitleText { get; set; }
166 
167  [Category("Title")]
168  [Description("Padding of title text.")]
169  public Padding TitlePadding { get; set; }
170 
171  private void ResetTitlePadding()
172  {
173  TitlePadding = Padding.Empty;
174  }
175 
176  private bool ShouldSerializeTitlePadding()
177  {
178  return (!TitlePadding.Equals(Padding.Empty));
179  }
180 
181  [Category("Content")]
182  [Description("Padding of content text.")]
183  public Padding ContentPadding { get; set; }
184 
185  private void ResetContentPadding()
186  {
187  ContentPadding = Padding.Empty;
188  }
189 
190  private bool ShouldSerializeContentPadding()
191  {
192  return (!ContentPadding.Equals(Padding.Empty));
193  }
194 
195  [Category("Image")]
196  [Description("Padding of icon image.")]
197  public Padding ImagePadding { get; set; }
198 
199  private void ResetImagePadding()
200  {
201  ImagePadding = Padding.Empty;
202  }
203 
204  private bool ShouldSerializeImagePadding()
205  {
206  return (!ImagePadding.Equals(Padding.Empty));
207  }
208 
209  [Category("Header"), DefaultValue(9)]
210  [Description("Height of window header.")]
211  public int HeaderHeight { get; set; }
212 
213  [Category("Buttons"), DefaultValue(true)]
214  [Description("Whether to show the close button.")]
215  public bool ShowCloseButton { get; set; }
216 
217  [Category("Buttons"), DefaultValue(false)]
218  [Description("Whether to show the options button.")]
219  public bool ShowOptionsButton { get; set; }
220 
221  [Category("Behavior")]
222  [Description("Context menu to open when clicking on the options button.")]
223  public ContextMenuStrip OptionsMenu { get; set; }
224 
225  [Category("Behavior"), DefaultValue(3000)]
226  [Description("Time in milliseconds the window is displayed.")]
227  public int Delay { get; set; }
228 
229  [Category("Behavior"), DefaultValue(1000)]
230  [Description("Time in milliseconds needed to make the window appear or disappear.")]
231  public int AnimationDuration { get; set; }
232 
233  [Category("Behavior"), DefaultValue(10)]
234  [Description("Interval in milliseconds used to draw the animation.")]
235  public int AnimationInterval { get; set; }
236 
237  [Category("Appearance")]
238  [Description("Size of the window.")]
239  public Size Size { get; set; }
240 
241  #endregion
242 
246  public PopupNotifier()
247  {
248  // set default values
249  HeaderColor = SystemColors.ControlDark;
250  BodyColor = SystemColors.Control;
251  TitleColor = System.Drawing.Color.Gray;
252  ContentColor = SystemColors.ControlText;
253  BorderColor = SystemColors.WindowFrame;
254  ButtonBorderColor = SystemColors.WindowFrame;
255  ButtonHoverColor = SystemColors.Highlight;
256  ContentHoverColor = SystemColors.HotTrack;
257  GradientPower = 50;
258  ContentFont = SystemFonts.DialogFont;
259  TitleFont = SystemFonts.CaptionFont;
260  ShowGrip = true;
261  Scroll = true;
262  TitlePadding = new Padding(0);
263  ContentPadding = new Padding(0);
264  ImagePadding = new Padding(0);
265  HeaderHeight = 9;
266  ShowCloseButton = true;
267  ShowOptionsButton = false;
268  Delay = 3000;
269  AnimationInterval = 10;
270  AnimationDuration = 1000;
271  Size = new Size(400, 100);
272 
273  BodyColor = System.Drawing.Color.SteelBlue;
274  BorderColor = System.Drawing.Color.Navy;
275  ButtonHoverColor = System.Drawing.Color.Orange;
276  ContentColor = System.Drawing.Color.White;
277  HeaderColor = System.Drawing.Color.Navy;
278  TitleColor = System.Drawing.Color.White;
279  ContentFont = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
280  TitleFont = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
281 
282 
283  frmPopup = new PopupNotifierForm(this);
284  frmPopup.TopMost = true;
285  frmPopup.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
286  frmPopup.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
287  frmPopup.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
288  frmPopup.MouseEnter += new EventHandler(frmPopup_MouseEnter);
289  frmPopup.MouseLeave += new EventHandler(frmPopup_MouseLeave);
290  frmPopup.CloseClick += new EventHandler(frmPopup_CloseClick);
291  frmPopup.LinkClick += new EventHandler(frmPopup_LinkClick);
292  frmPopup.ContextMenuOpened += new EventHandler(frmPopup_ContextMenuOpened);
293  frmPopup.ContextMenuClosed += new EventHandler(frmPopup_ContextMenuClosed);
294 
295  tmrAnimation = new Timer();
296  tmrAnimation.Tick += new EventHandler(tmAnimation_Tick);
297 
298  tmrWait = new Timer();
299  tmrWait.Tick += new EventHandler(tmWait_Tick);
300  }
301 
306  public void Popup()
307  {
308  if (!disposed)
309  {
310  if (!frmPopup.Visible)
311  {
312  frmPopup.Size = Size;
313  if (Scroll)
314  {
315  posStart = Screen.PrimaryScreen.WorkingArea.Bottom;
316  posStop = Screen.PrimaryScreen.WorkingArea.Bottom - frmPopup.Height;
317  }
318  else
319  {
320  posStart = Screen.PrimaryScreen.WorkingArea.Bottom - frmPopup.Height;
321  posStop = Screen.PrimaryScreen.WorkingArea.Bottom - frmPopup.Height;
322  }
323  opacityStart = 0;
324  opacityStop = 1;
325 
326  frmPopup.Opacity = opacityStart;
327  frmPopup.Location = new Point(Screen.PrimaryScreen.WorkingArea.Right - frmPopup.Size.Width - 1, posStart);
328  frmPopup.Show();
329  isAppearing = true;
330 
331  tmrWait.Interval = Delay;
332  tmrAnimation.Interval = AnimationInterval;
333  tmrAnimation.Start();
334  sw = System.Diagnostics.Stopwatch.StartNew();
335  System.Diagnostics.Debug.WriteLine("Animation started.");
336  }
337  else
338  {
339  if (!isAppearing)
340  {
341  frmPopup.Top = maxPosition;
342  frmPopup.Opacity = maxOpacity;
343  tmrAnimation.Stop();
344  System.Diagnostics.Debug.WriteLine("Animation stopped.");
345  tmrWait.Stop();
346  tmrWait.Start();
347  System.Diagnostics.Debug.WriteLine("Wait timer started.");
348  }
349  frmPopup.Invalidate();
350  }
351  }
352  }
353 
357  public void Hide()
358  {
359  System.Diagnostics.Debug.WriteLine("Animation stopped.");
360  System.Diagnostics.Debug.WriteLine("Wait timer stopped.");
361  tmrAnimation.Stop();
362  tmrWait.Stop();
363  frmPopup.Hide();
364  }
365 
372  private void frmPopup_ContextMenuClosed(object sender, EventArgs e)
373  {
374  System.Diagnostics.Debug.WriteLine("Menu closed.");
375  if (!mouseIsOn)
376  {
377  tmrWait.Interval = Delay;
378  tmrWait.Start();
379  System.Diagnostics.Debug.WriteLine("Wait timer started.");
380  }
381  }
382 
389  private void frmPopup_ContextMenuOpened(object sender, EventArgs e)
390  {
391  System.Diagnostics.Debug.WriteLine("Menu opened.");
392  tmrWait.Stop();
393  System.Diagnostics.Debug.WriteLine("Wait timer stopped.");
394  }
395 
401  private void frmPopup_LinkClick(object sender, EventArgs e)
402  {
403  if (Click != null)
404  {
405  Click(this, EventArgs.Empty);
406  }
407  }
408 
415  private void frmPopup_CloseClick(object sender, EventArgs e)
416  {
417  Hide();
418  if (Close != null)
419  {
420  Close(this, EventArgs.Empty);
421  }
422  }
423 
429  private void tmAnimation_Tick(object sender, EventArgs e)
430  {
431  long elapsed = sw.ElapsedMilliseconds;
432 
433  int posCurrent = (int)(posStart + ((posStop - posStart) * elapsed / AnimationDuration));
434  bool neg = (posStop - posStart) < 0;
435  if ((neg && posCurrent < posStop) ||
436  (!neg && posCurrent > posStop))
437  {
438  posCurrent = posStop;
439  }
440 
441  double opacityCurrent = opacityStart + ((opacityStop - opacityStart) * elapsed / AnimationDuration);
442  neg = (opacityStop - opacityStart) < 0;
443  if ((neg && opacityCurrent < opacityStop) ||
444  (!neg && opacityCurrent > opacityStop))
445  {
446  opacityCurrent = opacityStop;
447  }
448 
449  frmPopup.Top = posCurrent;
450  frmPopup.Opacity = opacityCurrent;
451 
452  // animation has ended
453  if (elapsed > AnimationDuration)
454  {
455  int posTemp = posStart;
456  posStart = posStop;
457  posStop = posTemp;
458 
459  double opacityTemp = opacityStart;
460  opacityStart = opacityStop;
461  opacityStop = opacityTemp;
462 
463  sw.Reset();
464  tmrAnimation.Stop();
465  System.Diagnostics.Debug.WriteLine("Animation stopped.");
466 
467  if (isAppearing)
468  {
469  isAppearing = false;
470  maxPosition = frmPopup.Top;
471  maxOpacity = frmPopup.Opacity;
472  if (!mouseIsOn)
473  {
474  tmrWait.Stop();
475  tmrWait.Start();
476  System.Diagnostics.Debug.WriteLine("Wait timer started.");
477  }
478  }
479  else
480  {
481  frmPopup.Hide();
482  }
483  }
484  }
485 
491  private void tmWait_Tick(object sender, EventArgs e)
492  {
493  System.Diagnostics.Debug.WriteLine("Wait timer elapsed.");
494  tmrWait.Stop();
495  tmrAnimation.Interval = AnimationInterval;
496  tmrAnimation.Start();
497  sw.Restart();
498  System.Diagnostics.Debug.WriteLine("Animation started.");
499  }
500 
506  private void frmPopup_MouseLeave(object sender, EventArgs e)
507  {
508  System.Diagnostics.Debug.WriteLine("MouseLeave");
509  if (frmPopup.Visible && (OptionsMenu == null || !OptionsMenu.Visible))
510  {
511  tmrWait.Interval = Delay;
512  tmrWait.Start();
513  System.Diagnostics.Debug.WriteLine("Wait timer started.");
514  }
515  mouseIsOn = false;
516  }
517 
523  private void frmPopup_MouseEnter(object sender, EventArgs e)
524  {
525  System.Diagnostics.Debug.WriteLine("MouseEnter");
526  if (!isAppearing)
527  {
528  frmPopup.Top = maxPosition;
529  frmPopup.Opacity = maxOpacity;
530  tmrAnimation.Stop();
531  System.Diagnostics.Debug.WriteLine("Animation stopped.");
532  }
533 
534  tmrWait.Stop();
535  System.Diagnostics.Debug.WriteLine("Wait timer stopped.");
536 
537  mouseIsOn = true;
538  }
539 
544  protected override void Dispose(bool disposing)
545  {
546  if (!disposed)
547  {
548  if (disposing && frmPopup != null)
549  {
550  frmPopup.Dispose();
551  }
552  disposed = true;
553  }
554  base.Dispose(disposing);
555  }
556  }
557 }
void Hide()
Hide the notification window.
EventHandler Close
Event that is raised when the notification window is manually closed.
override void Dispose(bool disposing)
Dispose the notification form.
void ShowInternalImage(bool isErrorIcon, bool isAboutIcon)
void Popup()
Show the notification window if it is not already visible. If the window is currently disappearing...
EventHandler Click
Event that is raised when the text in the notification window is clicked.
PopupNotifier()
Create a new instance of the popup component.
Non-visual component to show a notification window in the right lower corner of the screen...

Social Network


Services Overview

Architect, implement and test file system filter drivers for a wide range of functionality. We can offer several levels of assistance to meet your specific.

Contact Us

You are welcome to contact us for salse or partnership.

Sales: sales@easefilter.com
Support: support@easefilter.com
Info: info@easefilter.com