EaseFilter Demo Project
PopupNotifierForm.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.Windows.Forms;
12 using System.Drawing;
13 using System.Drawing.Drawing2D;
14 
15 namespace NotificationWindow
16 {
20  internal class PopupNotifierForm : System.Windows.Forms.Form
21  {
25  public event EventHandler LinkClick;
26 
30  public event EventHandler CloseClick;
31 
32  internal event EventHandler ContextMenuOpened;
33  internal event EventHandler ContextMenuClosed;
34 
35  private bool mouseOnClose = false;
36  private bool mouseOnLink = false;
37  private bool mouseOnOptions = false;
38  private int heightOfTitle;
39 
40  #region GDI objects
41 
42  private bool gdiInitialized = false;
43  private Rectangle rcBody;
44  private Rectangle rcHeader;
45  private Rectangle rcForm;
46  private LinearGradientBrush brushBody;
47  private LinearGradientBrush brushHeader;
48  private Brush brushButtonHover;
49  private Pen penButtonBorder;
50  private Pen penContent;
51  private Pen penBorder;
52  private Brush brushForeColor;
53  private Brush brushLinkHover;
54  private Brush brushContent;
55  private Brush brushTitle;
56 
57  #endregion
58 
63  public PopupNotifierForm(PopupNotifier parent)
64  {
65  Parent = parent;
66  this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
67  this.SetStyle(ControlStyles.ResizeRedraw, true);
68  this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
69  this.ShowInTaskbar = false;
70 
71  this.VisibleChanged += new EventHandler(PopupNotifierForm_VisibleChanged);
72  this.MouseMove += new MouseEventHandler(PopupNotifierForm_MouseMove);
73  this.MouseUp += new MouseEventHandler(PopupNotifierForm_MouseUp);
74  this.Paint += new PaintEventHandler(PopupNotifierForm_Paint);
75  }
76 
82  private void PopupNotifierForm_VisibleChanged(object sender, EventArgs e)
83  {
84  if (this.Visible)
85  {
86  mouseOnClose = false;
87  mouseOnLink = false;
88  mouseOnOptions = false;
89  }
90  }
91 
95  private void InitializeComponent()
96  {
97  this.SuspendLayout();
98  this.ClientSize = new System.Drawing.Size(392, 66);
99  this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
100  this.Name = "PopupNotifierForm";
101  this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
102  this.TopMost = true;
103  this.ResumeLayout(false);
104  }
105 
109  public new PopupNotifier Parent { get; set; }
110 
117  private int AddValueMax255(int input, int add)
118  {
119  return (input + add < 256) ? input + add : 255;
120  }
121 
128  private int DedValueMin0(int input, int ded)
129  {
130  return (input - ded > 0) ? input - ded : 0;
131  }
132 
138  private Color GetDarkerColor(Color color)
139  {
140  return System.Drawing.Color.FromArgb(255, DedValueMin0((int)color.R, Parent.GradientPower), DedValueMin0((int)color.G, Parent.GradientPower), DedValueMin0((int)color.B, Parent.GradientPower));
141  }
142 
148  private Color GetLighterColor(Color color)
149  {
150  return System.Drawing.Color.FromArgb(255, AddValueMax255((int)color.R, Parent.GradientPower), AddValueMax255((int)color.G, Parent.GradientPower), AddValueMax255((int)color.B, Parent.GradientPower));
151  }
152 
156  private RectangleF RectContentText
157  {
158  get
159  {
160  if (Parent.Image != null)
161  {
162  return new RectangleF(
163  Parent.ImagePadding.Left + Parent.ImageSize.Width + Parent.ImagePadding.Right + Parent.ContentPadding.Left,
164  Parent.HeaderHeight + Parent.TitlePadding.Top + heightOfTitle + Parent.TitlePadding.Bottom + Parent.ContentPadding.Top,
165  this.Width - Parent.ImagePadding.Left - Parent.ImageSize.Width - Parent.ImagePadding.Right - Parent.ContentPadding.Left - Parent.ContentPadding.Right - 16 - 5,
166  this.Height - Parent.HeaderHeight - Parent.TitlePadding.Top - heightOfTitle - Parent.TitlePadding.Bottom - Parent.ContentPadding.Top - Parent.ContentPadding.Bottom - 1);
167  }
168  else
169  {
170  return new RectangleF(
171  Parent.ContentPadding.Left,
172  Parent.HeaderHeight + Parent.TitlePadding.Top + heightOfTitle + Parent.TitlePadding.Bottom + Parent.ContentPadding.Top,
173  this.Width - Parent.ContentPadding.Left - Parent.ContentPadding.Right - 16 - 5,
174  this.Height - Parent.HeaderHeight - Parent.TitlePadding.Top - heightOfTitle - Parent.TitlePadding.Bottom - Parent.ContentPadding.Top - Parent.ContentPadding.Bottom - 1);
175  }
176  }
177  }
178 
182  private Rectangle RectClose
183  {
184  get { return new Rectangle(this.Width - 5 - 16, Parent.HeaderHeight + 3, 16, 16); }
185  }
186 
190  private Rectangle RectOptions
191  {
192  get { return new Rectangle(this.Width - 5 - 16, Parent.HeaderHeight + 3 + 16 + 5, 16, 16); }
193  }
194 
200  private void PopupNotifierForm_MouseMove(object sender, MouseEventArgs e)
201  {
202  if (Parent.ShowCloseButton)
203  {
204  mouseOnClose = RectClose.Contains(e.X, e.Y);
205  }
206  if (Parent.ShowOptionsButton)
207  {
208  mouseOnOptions = RectOptions.Contains(e.X, e.Y);
209  }
210  mouseOnLink = RectContentText.Contains(e.X, e.Y);
211  Invalidate();
212  }
213 
219  private void PopupNotifierForm_MouseUp(object sender, MouseEventArgs e)
220  {
221  if (e.Button == System.Windows.Forms.MouseButtons.Left)
222  {
223  if (RectClose.Contains(e.X, e.Y) && (CloseClick != null))
224  {
225  CloseClick(this, EventArgs.Empty);
226  }
227  if (RectContentText.Contains(e.X, e.Y) && (LinkClick != null))
228  {
229  LinkClick(this, EventArgs.Empty);
230  }
231  if (RectOptions.Contains(e.X, e.Y) && (Parent.OptionsMenu != null))
232  {
233  if (ContextMenuOpened != null)
234  {
235  ContextMenuOpened(this, EventArgs.Empty);
236  }
237  Parent.OptionsMenu.Show(this, new Point(RectOptions.Right - Parent.OptionsMenu.Width, RectOptions.Bottom));
238  Parent.OptionsMenu.Closed += new ToolStripDropDownClosedEventHandler(OptionsMenu_Closed);
239  }
240  }
241  }
242 
248  private void OptionsMenu_Closed(object sender, ToolStripDropDownClosedEventArgs e)
249  {
250  Parent.OptionsMenu.Closed -= new ToolStripDropDownClosedEventHandler(OptionsMenu_Closed);
251 
252  if (ContextMenuClosed != null)
253  {
254  ContextMenuClosed(this, EventArgs.Empty);
255  }
256  }
257 
261  private void AllocateGDIObjects()
262  {
263  rcBody = new Rectangle(0, 0, this.Width, this.Height);
264  rcHeader = new Rectangle(0, 0, this.Width, Parent.HeaderHeight);
265  rcForm = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
266 
267  brushBody = new LinearGradientBrush(rcBody, Parent.BodyColor, GetLighterColor(Parent.BodyColor), LinearGradientMode.Vertical);
268  brushHeader = new LinearGradientBrush(rcHeader, Parent.HeaderColor, GetDarkerColor(Parent.HeaderColor), LinearGradientMode.Vertical);
269  brushButtonHover = new SolidBrush(Parent.ButtonHoverColor);
270  penButtonBorder = new Pen(Parent.ButtonBorderColor);
271  penContent = new Pen(Parent.ContentColor, 2);
272  penBorder = new Pen(Parent.BorderColor);
273  brushForeColor = new SolidBrush(ForeColor);
274  brushLinkHover = new SolidBrush(Parent.ContentHoverColor);
275  brushContent = new SolidBrush(Parent.ContentColor);
276  brushTitle = new SolidBrush(Parent.TitleColor);
277 
278  gdiInitialized = true;
279  }
280 
284  private void DisposeGDIObjects()
285  {
286  if (gdiInitialized)
287  {
288  brushBody.Dispose();
289  brushHeader.Dispose();
290  brushButtonHover.Dispose();
291  penButtonBorder.Dispose();
292  penContent.Dispose();
293  penBorder.Dispose();
294  brushForeColor.Dispose();
295  brushLinkHover.Dispose();
296  brushContent.Dispose();
297  brushTitle.Dispose();
298  }
299  }
300 
306  private void PopupNotifierForm_Paint(object sender, PaintEventArgs e)
307  {
308  if (!gdiInitialized)
309  {
310  AllocateGDIObjects();
311  }
312 
313  // draw window
314  e.Graphics.FillRectangle(brushBody, rcBody);
315  e.Graphics.FillRectangle(brushHeader, rcHeader);
316  e.Graphics.DrawRectangle(penBorder, rcForm);
317  if (Parent.ShowGrip)
318  {
319  e.Graphics.DrawImage(Properties.Resources.Grip, (int)((this.Width - Properties.Resources.Grip.Width) / 2), (int)((Parent.HeaderHeight - 3) / 2));
320  }
321  if (Parent.ShowCloseButton)
322  {
323  if (mouseOnClose)
324  {
325  e.Graphics.FillRectangle(brushButtonHover, RectClose);
326  e.Graphics.DrawRectangle(penButtonBorder, RectClose);
327  }
328  e.Graphics.DrawLine(penContent, RectClose.Left + 4, RectClose.Top + 4, RectClose.Right - 4, RectClose.Bottom - 4);
329  e.Graphics.DrawLine(penContent, RectClose.Left + 4, RectClose.Bottom - 4, RectClose.Right - 4, RectClose.Top + 4);
330  }
331  if (Parent.ShowOptionsButton)
332  {
333  if (mouseOnOptions)
334  {
335  e.Graphics.FillRectangle(brushButtonHover, RectOptions);
336  e.Graphics.DrawRectangle(penButtonBorder, RectOptions);
337  }
338  e.Graphics.FillPolygon(brushForeColor, new Point[] { new Point(RectOptions.Left + 4, RectOptions.Top + 6), new Point(RectOptions.Left + 12, RectOptions.Top + 6), new Point(RectOptions.Left + 8, RectOptions.Top + 4 + 6) });
339  }
340 
341  // draw icon
342  if (Parent.Image != null)
343  {
344  e.Graphics.DrawImage(Parent.Image, Parent.ImagePadding.Left, Parent.HeaderHeight + Parent.ImagePadding.Top, Parent.ImageSize.Width, Parent.ImageSize.Height);
345  }
346 
347  // calculate height of title
348  heightOfTitle = (int)e.Graphics.MeasureString("A", Parent.TitleFont).Height;
349  int titleX = Parent.TitlePadding.Left;
350  if (Parent.Image != null)
351  {
352  titleX += Parent.ImagePadding.Left + Parent.ImageSize.Width + Parent.ImagePadding.Right;
353  }
354 
355  // draw title
356  e.Graphics.DrawString(Parent.TitleText, Parent.TitleFont, brushTitle, titleX, Parent.HeaderHeight + Parent.TitlePadding.Top);
357 
358  // draw content text, optionally with a bold part
359  this.Cursor = mouseOnLink ? Cursors.Hand : Cursors.Default;
360  Brush brushText = mouseOnLink ? brushLinkHover : brushContent;
361  e.Graphics.DrawString(Parent.ContentText, Parent.ContentFont, brushText, RectContentText);
362  }
363 
368  protected override void Dispose(bool disposing)
369  {
370  if (disposing)
371  {
372  DisposeGDIObjects();
373  }
374  base.Dispose(disposing);
375  }
376  }
377 }

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