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
.
NotifyWindow nw = new NotifyWindow ("This is a sample NotifyWindow");
nw.Notify();
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";
nw.BackgroundStyle = NotifyWindow.BackgroundStyles.ForwardDiagonalGradient;
nw.BackColor = Color.SpringGreen;
nw.GradientColor = Color.White;
nw.TextColor = Color.Blue;
nw.TitleColor = Color.Black;
nw.PressedColor = Color.Red;
nw.Font = new Font ("Tahoma", 8.25F);
nw.TitleFont = new Font ("Tahoma", 8.25F, FontStyle.Bold);
nw.SetDimensions (nwWidth, nwHeight);
nw.WaitOnMouseOver = true;
nw.TextClicked += new System.EventHandler (nwTextClicked);
nw.TitleClicked += new System.EventHandler (nwTitleClicked);
nw.WaitTime = 20000;
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);
...
ShowWindow (this.Handle, SW_SHOWNOACTIVATE);
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