Click here to Skip to main content
Click here to Skip to main content

NotifyWindow: A different MSN Messenger style notification window

By , 2 Aug 2004
 

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.

// 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.

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.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Robert Misiak
Web Developer
United States United States
Member
Robert Misiak is a C# developer, working at a credit card company, in Las Vegas, NV.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGood idea!membercen_jin_long7 Mar '13 - 12:21 
GeneralMy vote of 5memberRafik El-Kheer3 Aug '12 - 14:49 
Questioni want to hide CLOSE button on NotifyWindow......what to do?memberRobin Purbia12 Jul '12 - 0:48 
QuestionNotifyWindowsmemberPunisher935 Mar '12 - 7:32 
QuestionHeartly thank youmemberAnil B. Patil22 Feb '12 - 18:16 
GeneralMy vote of 5memberamitmane55527 Feb '11 - 23:41 
GeneralNotification window won't closemembermike Svidron30 Apr '10 - 6:12 
QuestionHow can i just show notification window not form windowmembername3221 Apr '10 - 2:56 
GeneralQuestionmemberBenjamin Unanka24 Jul '08 - 6:42 
QuestionVB.Net version?membercal1918 Apr '08 - 2:08 
AnswerRe: VB.Net version?memberrzeylah6 Aug '08 - 0:29 
GeneralRe: VB.Net version?memberpaladinkerb30 Mar '11 - 9:01 
GeneralUnable to download NotifyWindowmemberGlauter7 Mar '08 - 4:12 
GeneralNotifyWindow on Vistamemberpogarek15 Feb '07 - 22:13 
GeneralRe: NotifyWindow on VistamemberRobert Misiak10 Jul '07 - 8:19 
QuestionHow to Add a Controlmemberraj_32722 Jan '07 - 20:30 
GeneralShow two NotifyWindows above each othermemberLpH198126 May '06 - 0:41 
GeneralRe: Show two NotifyWindows above each othermemberronaldovn17 Mar '10 - 21:17 
GeneralRe: Show two NotifyWindows above each othermemberLucas heezen18 Mar '10 - 0:59 
GeneralRe: Show two NotifyWindows above each othermemberronaldovn18 Mar '10 - 16:24 
GeneralRe: Show two NotifyWindows above each othermemberDekkie17 Feb '11 - 4:52 
QuestionUsing notify window in a ThreadFunctionmemberjis17 May '06 - 2:36 
AnswerRe: Using notify window in a ThreadFunctionmemberRobert Misiak17 May '06 - 3:19 
GeneralRe: Using notify window in a ThreadFunctionmemberjis17 May '06 - 18:41 
GeneralRe: Using notify window in a ThreadFunctionmemberManjit Sooch20 Jul '06 - 13:33 
GeneralRe: Using notify window in a ThreadFunctionmembershane198527 Sep '07 - 9:21 
GeneralRe: Using notify window in a ThreadFunctionmemberlxiongh3 Sep '09 - 4:03 
GeneralStacking windows codememberjdmwood29 Mar '06 - 0:13 
GeneralRe: Stacking windows codememberjdmwood29 Mar '06 - 0:16 
GeneralRe: Stacking windows codememberBob-ish11 Dec '06 - 12:51 
GeneralRe: Stacking windows codememberPooranPrasad8 Apr '07 - 21:54 
GeneralRe: Stacking windows codemembern2jtx15 Apr '08 - 7:27 
GeneralRe: Stacking windows codememberronaldovn18 Mar '10 - 16:40 
GeneralRe: Stacking windows codememberronaldovn18 Mar '10 - 17:55 
QuestionHow would you call this from a C# ASP.Net Web Applicationmembermranimal25 Feb '06 - 9:11 
AnswerRe: How would you call this from a C# ASP.Net Web ApplicationmemberPaulza25 Jan '07 - 23:37 
GeneralGood jumping off point!memberdavegalligher26 Jan '06 - 10:06 
GeneralNative Windowmemberhandle1237 Dec '05 - 20:22 
GeneralRe: Native WindowmemberRobert Misiak8 Dec '05 - 2:49 
GeneralRe: Native Windowmemberpjnaughter12 May '06 - 3:59 
GeneralVery LeakymemberMatware7 Nov '05 - 18:33 
GeneralRe: Very LeakymemberRobert Misiak8 Dec '05 - 2:48 
GeneralRe: Very LeakymemberMember 469789318 Sep '08 - 8:46 
GeneralImage Displaymembersuresh_unique18 Aug '05 - 4:46 
GeneralRe: Image DisplaymemberMarkoStamcar30 Nov '05 - 12:54 
QuestionOpacity?memberDarenIott14 Jul '05 - 5:55 
AnswerRe: Opacity?memberRobert Misiak14 Jul '05 - 6:20 
GeneralRe: Opacity?memberDarenIott15 Jul '05 - 1:50 
GeneralUnmanaged MFC versionmemberpjnaughter22 Apr '05 - 11:05 
GeneralRe: Unmanaged MFC versionmemberRavi Bhavnani22 Apr '05 - 18:26 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 3 Aug 2004
Article Copyright 2004 by Robert Misiak
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid