DotCat

喜欢技术,喜欢简单,喜欢猫

导航

[CodeProject每日一荐] NotifyWindow: 一个轻量级 MSN Messenger 风格的通知窗体

今天介绍一个C#程序: 轻量级的MSN Messenger 风格的通知窗体(winform), 之所以说是轻量级,最主要是因为只支持文本.NotifyWindow: A different MSN Messenger style notification window By Robert Misiak




[介绍]
NotifyWindow显示一种MSN Messenger 风格的通知窗体. 如果需要在通知窗体中显示图片,请参照 John O'Byrne的TaskbarNotifer.(译者:准备明天介绍) .如果你只准备显示文本,轻量级的NotifyWindow 就足够了.在打开和关闭NotifyWindow通知窗体时有向上/下平移的动画效果.窗体默认显示11秒,当然代码里可以改. 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();


有字体和颜色等选项可随意改变. 下载代码中的TestNotifyWindow应用程序可以让你和看到一些选项的设置,但下面的代码更全面地展示了所有选项的设置.
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();

以上代码实现的窗体效果如下图

程序员也可以通过nw.Blend and nw.StringFormat来定制背景, Text 和 Title 

[特色]

NotifyWindow的特色在于它自己完成了绘制工作.背景是由Graphics.FillRectangle 和LinearGradientBrush (默认的情况)  或SolidBrush绘制的. 边框是由一系列对 Graphics.DrawRectangle 和 Graphics.DrawLine的调用完成绘制的.在Windows XP 或更新版本的启用了主题皮肤效果(Visual Styles)的操作系统中, 关闭按钮的绘制是调用 UxTheme.dll 中的DrawThemeBackground(),若没有启用主题皮肤效果,则使用 ControlPaint.DrawCaptionButton.

在此程序和类似程序中,一个难点在于如何把窗体放在最上层,但又不抢走焦点. 分别通过Form.Show() 和设置TopMost = true来激活窗体都会抢走焦点. 我们可以这样来实现:先调用ShowWindow(),再调用 SetWindowPos(),并在参数中告知操作系统不要激活窗体.

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);

 

一个类似的NotifyWindow类最初是在一个开源的项目 ChronosXP.中实现的. 后来大家觉得需要在此项目之外使用这些类似NotifyWindow的代码, 就将其修改得更一般化,拿出来可单独使用.
提供下载的代码中包括一个名为NotifyWindow2000的类, 它会在鼠标或键盘有所活动之前一直显示NotifyWindow.它用SetWindowsHookEx() 设置钩子WH_KEYBOARD_LL/WH_MOUSE_LL来监测用户活动,因此它只能在Windows 2000或更高版本的windows上运行.笔者还不知道如何在老版本的windows上实现这一点.

posted on 2006-04-12 00:06  DotCat  阅读(3272)  评论(4编辑  收藏  举报