Today we will introduce a C # program: a lightweight MSN Messenger-style notification form (winform). It is lightweight because it only supports text. policywindow: A different MSN Messenger style notification window By Robert Misiak
[Introduction]
NotifyWindow displays a notification form in the MSN Messenger style. if you need to display an image in the notification form, refer to the TaskbarNotifer of John O 'byrne. (prepared for tomorrow's Introduction ). if you only want to display the text, the lightweight yywindow is enough. when the yywindow notification form is opened or closed, there is an animated effect of up/down translation. the form is displayed for 11 seconds by default. Of course, you can change it in the code. notifyWindow is responsible for drawing all forms without any other image files.
[Use Code]
A very simple example:
// Display the text "This is a sample policywindow"
Policywindow nw = new policywindow ("This is a sample policywindow ");
Nw. Policy ();
// 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 policywindow ");
Nw. Policy ();
Options such as font and color can be changed at will. the testpolicywindow application in the download code allows you to see some options, but the following code shows the settings of all options more comprehensively. notifyWindow nw = new NotifyWindow ();
Nw. Text = "This is the policywindow text ";
Nw. Title = "Title Text ";
// Change the background style. Other valid
// Styles are Solid, VerticalGradient,
// HorizontalGradient and BackwardDiagonalGradient
// (Default: VerticalGradient)
Nw. BackgroundStyle = policywindow. 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 the policywindow size. (Default: 130,110)
Nw. SetDimensions (nwWidth, nwHeight );
// Do not close the policywindow 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 20000 ms. (Default: 11000 ms)
Nw. WaitTime = 20000;
// Now show the policywindow.
Nw. Policy ();
The effect of the form implemented by the above Code is as follows:
Programmers can also customize the background, Text and Title through nw. Blend and nw. StringFormat.
[Features]
The special feature of NotifyWindow is that it has completed the painting. the background is Graphics. fillRectangle and LinearGradientBrush (default) or SolidBrush. the border is composed of a series of Graphics. drawRectangle and Graphics. drawLine call to complete the drawing. in Windows XP or later versions, When Visual Styles is enabled, the close button is used to call UxTheme. drawThemeBackground () in dll. If topic skin effect is not enabled, ControlPaint is used. drawCaptionButton.
In this program and similar programs, a difficulty lies in how to put the form on the top, but do not take the focus. use Form. show () and set TopMost = true to activate the form. we can do this: Call ShowWindow () First, call SetWindowPos (), and tell the operating system not to activate the form in the parameter.
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, cannot t don't activate the window.
SetWindowPos (this. Handle, HWND_TOPMOST, Left, Top, Width, Height, SWP_NOACTIVATE );
A similar policywindow class was initially developed in an open-source project, ChronosXP.. later, we thought we needed to use the code similar to yywindow outside of this project, so we could make it more general and use it separately.
The downloaded code contains a class named NotifyWindow2000, which displays NotifyWindow until the mouse or keyboard is active. it uses SetWindowsHookEx () to set the hook WH_KEYBOARD_LL/WH_MOUSE_LL to monitor user activity. Therefore, it can only run on Windows 2000 or later versions. I still don't know how to implement this in earlier windows versions.