Click here to Skip to main content
15,881,840 members
Articles / Programming Languages / C#
Article

NotifyWindow: A different MSN Messenger style notification window

Rate me:
Please Sign up or sign in to vote.
4.85/5 (48 votes)
2 Aug 2004Public Domain2 min read 461.7K   10.2K   213   112
Another MSN Messenger-like notification window, this one does its own drawing

Introduction

NotifyWindow displays an MSN Messenger-like notification window. If you want to display your own images in the notification window, you may prefer to use John O'Byrne's TaskbarNotifer. NotifyWindow may be easier to use if you just intend to display text. Animations are used in opening and closing the NotifyWindow, and the window is displayed TopMost while not stealing focus. The window will be shown for a default of 11 seconds, although this can be changed. NotifyWindow does all of its own drawing, so no extra image files are required.

Using the code

It is pretty simple to display text using NotifyWindow.

C#
// Display the text "This is a sample NotifyWindow"
NotifyWindow nw = new NotifyWindow ("This is a sample NotifyWindow");
nw.Notify();

// The following two lines of code will display a window that 
// looks exactly like the one shown at the beginning of this article.
NotifyWindow nw = new NotifyWindow ("NotifyWindow", 
  "This is a sample notification created with NotifyWindow");
nw.Notify();

If desired, a variety of other options can be changed - such as fonts and colors. The included TestNotifyWindow application will let you play around with a few of the settings, but the code displayed here should serve as a more complete reference.

C#
NotifyWindow nw = new NotifyWindow();

nw.Text = "This is the NotifyWindow text";
nw.Title = "Title Text";

// Change the background style.  Other valid 
// styles are Solid, VerticalGradient,
// HorizontalGradient and BackwardDiagonalGradient  
// (Default: VerticalGradient)
nw.BackgroundStyle = NotifyWindow.BackgroundStyles.ForwardDiagonalGradient;

// Change the background colors  
// (Default: BackColor=SteelBlue, GradientColor=WhiteSmoke)
nw.BackColor = Color.SpringGreen;
nw.GradientColor = Color.White;

// Change the text and title colors.  (Default: ControlText)
nw.TextColor = Color.Blue;
nw.TitleColor = Color.Black;

// Change the color displayed when the text is pressed.  (Default: Gray)
nw.PressedColor = Color.Red;

// Use non-default fonts.  If TitleFont is not set 
// by the user, nw.Font will be used.
nw.Font = new Font ("Tahoma", 8.25F);
nw.TitleFont = new Font ("Tahoma", 8.25F, FontStyle.Bold);

// Change NotifyWindow size.  (Default: 130, 110)
nw.SetDimensions (nwWidth, nwHeight);

// Do not close the NotifyWindow if the mouse 
// cursor is over the window.  (Default: true)
nw.WaitOnMouseOver = true;

// Set up an EventHandler to be called if the text or title are clicked.
nw.TextClicked += new System.EventHandler (nwTextClicked);
nw.TitleClicked += new System.EventHandler (nwTitleClicked);

// Display the window for 20 seconds, or 20000ms.  (Default: 11000ms)
nw.WaitTime = 20000;

// Now show the NotifyWindow.
nw.Notify();

This is how the NotifyWindow created with the above code will look:

Programmers can also use their own Blend (for the background) or StringFormat (which will be used when Text and Title are drawn) if desired by setting the nw.Blend and nw.StringFormat variables.

Points of Interest

NotifyWindow is unique because it does all of its own drawing. The background is drawn using Graphics.FillRectangle with either a LinearGradientBrush (default) or a SolidBrush. The borders are drawn using a series of calls to Graphics.DrawRectangle and Graphics.DrawLine. On Windows XP or higher systems with Visual Styles enabled, the close button is drawn using DrawThemeBackground() from UxTheme.dll - otherwise, ControlPaint.DrawCaptionButton is used.

An obstacle faced in both this and similar applications has been displaying the window on top without stealing focus. Both Form.Show() and setting TopMost = true individually activate the form, which steals focus. We get around this by calling ShowWindow() and SetWindowPos() with arguments instructing the operating system not to activate the window.

C#
const Int32 HWND_TOPMOST = -1;
const Int32 SWP_NOACTIVATE = 0x0010;
const Int32 SW_SHOWNOACTIVATE = 4;
[DllImport ("user32.dll")]
protected static extern bool ShowWindow (IntPtr hWnd, Int32 flags);
[DllImport ("user32.dll")]
protected static extern bool SetWindowPos (IntPtr hWnd, 
  Int32 hWndInsertAfter, Int32 X, Int32 Y, Int32 cx, Int32 cy, uint uFlags);

...

// Show the window without activating it.
ShowWindow (this.Handle, SW_SHOWNOACTIVATE);

// Equivalent to setting TopMost = true, except don't activate the window.
SetWindowPos (this.Handle, HWND_TOPMOST, Left, Top, Width, Height, SWP_NOACTIVATE);

A similar NotifyWindow class was originally implemented for an open-source project called ChronosXP. When it became apparant that others might like to use this code outside of that project, it was modified, removing the ChronosXP-specific parts and making it more generic.

A class called NotifyWindow2000 is included with the distribution that will display the NotifyWindow indefinitely until there is mouse or keyboard activity, similar to balloon windows. It uses SetWindowsHookEx() with WH_KEYBOARD_LL/WH_MOUSE_LL to detect user activity, so it will only work with Windows 2000 or higher. If anyone knows how to do this on older versions of Windows I would like to hear about it.

History

  • Initial coding: July 28, 2004

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Architect Onestop Internet
United States United States
Web architect and PM, in the E-commerce space.

Comments and Discussions

 
GeneralRe: Image Display Pin
MarkoStamcar30-Nov-05 12:54
MarkoStamcar30-Nov-05 12:54 
QuestionOpacity? Pin
DarenIott14-Jul-05 5:55
DarenIott14-Jul-05 5:55 
AnswerRe: Opacity? Pin
Robert Misiak14-Jul-05 6:20
professionalRobert Misiak14-Jul-05 6:20 
GeneralRe: Opacity? Pin
DarenIott15-Jul-05 1:50
DarenIott15-Jul-05 1:50 
GeneralUnmanaged MFC version Pin
pjnaughter22-Apr-05 11:05
pjnaughter22-Apr-05 11:05 
GeneralRe: Unmanaged MFC version Pin
Ravi Bhavnani22-Apr-05 18:26
professionalRavi Bhavnani22-Apr-05 18:26 
GeneralRe: Unmanaged MFC version Pin
pjnaughter23-Apr-05 1:38
pjnaughter23-Apr-05 1:38 
Generalchange the position based on another form's position Pin
Jasti Srinivas4-Mar-05 3:26
Jasti Srinivas4-Mar-05 3:26 
GeneralRe: change the position based on another form's position Pin
Robert Misiak4-Mar-05 12:28
professionalRobert Misiak4-Mar-05 12:28 
GeneralRe: change the position based on another form's position Pin
Jasti Srinivas4-Mar-05 16:22
Jasti Srinivas4-Mar-05 16:22 
QuestionHow to use animated GIFs as your display pic Pin
Anonymous25-Feb-05 23:00
Anonymous25-Feb-05 23:00 
AnswerRe: How to use animated GIFs as your display pic Pin
Robert Misiak4-Mar-05 12:30
professionalRobert Misiak4-Mar-05 12:30 
GeneralRe: How to use animated GIFs as your display pic Pin
Member 192988126-Aug-05 9:07
Member 192988126-Aug-05 9:07 
AnswerRe: How to use animated GIFs as your display pic [modified] Pin
[|FuRiA|]1-Aug-06 12:04
[|FuRiA|]1-Aug-06 12:04 
GeneralRe: How to use animated GIFs as your display pic Pin
Anonymous29-Aug-05 6:03
Anonymous29-Aug-05 6:03 
GeneralIs It possible to use in an ASP.net App Pin
mreyeros5-Jan-05 10:04
mreyeros5-Jan-05 10:04 
GeneralRe: Is It possible to use in an ASP.net App Pin
Robert Misiak5-Jan-05 16:52
professionalRobert Misiak5-Jan-05 16:52 
GeneralRe: Is It possible to use in an ASP.net App Pin
mreyeros6-Jan-05 2:08
mreyeros6-Jan-05 2:08 
Questionhow to show in a NT service? Pin
Mad200331-Oct-04 3:35
Mad200331-Oct-04 3:35 
AnswerRe: how to show in a NT service? Pin
Robert Misiak31-Oct-04 9:09
professionalRobert Misiak31-Oct-04 9:09 
GeneralRe: how to show in a NT service? Pin
Sgt-Peppa10-Dec-04 3:03
Sgt-Peppa10-Dec-04 3:03 
GeneralRe: how to show in a NT service? Pin
Robert Misiak10-Dec-04 4:48
professionalRobert Misiak10-Dec-04 4:48 
GeneralRe: how to show in a NT service? Pin
papillon33320-Apr-05 22:27
papillon33320-Apr-05 22:27 
GeneralRe: how to show in a NT service? Pin
eLToBiS10-Jan-06 11:37
eLToBiS10-Jan-06 11:37 
AnswerRe: how to show in a NT service? Pin
Vadim Tabakman3-Feb-05 11:25
Vadim Tabakman3-Feb-05 11:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.