MSN messenger 支持的消息弹出机制:
using CustomUIControls;
TaskbarNotifier taskbarNotifier;
在构造器中加入下面的行:
taskbarNotifier=new TaskbarNotifier();
taskbarNotifier.SetBackgroundBitmap("skin.bmp", Color.FromArgb(255,0,255));
taskbarNotifier.SetCloseBitmap("close.bmp", Color.FromArgb(255,0,255),new Point(127,8));
taskbarNotifier.TitleRectangle=new Rectangle(40,9,70,25);
taskbarNotifier.ContentRectangle=new Rectangle(8,41,133,68);
taskbarNotifier.TitleClick+=new EventHandler(TitleClick);
taskbarNotifier.ContentClick+=new EventHandler(ContentClick);
taskbarNotifier.CloseClick+=new EventHandler(CloseClick);
taskbarNotifier.SetBackgroundBitmap("skin.bmp", Color.FromArgb(255,0,255));
taskbarNotifier.SetCloseBitmap("close.bmp",Color.FromArgb(255,0,255),new Point(127,8));
第一行代码设置背景外观的图片样式和透明度;第二行代码设置3个状态下关闭按钮的透明度及其在窗口中的位置(可选项);
taskbarNotifier.TitleRectangle=new Rectangle(40,9,70,25); taskbarNotifier.ContentRectangle=new Rectangle(8,41,133,68);这两行定义矩形框中的标题和内容,下面是标题、内容、关闭按钮的的点击事件:
taskbarNotifier.TitleClick+=new EventHandler(OnTitleClick); taskbarNotifier.ContentClick+=new EventHandler(OnContentClick); taskbarNotifier.CloseClick+=new EventHandler(OnCloseClick);
taskbarNotifier.Show("TitleText","ContentText",500,3000,500);
动态显示弹出窗口的3种状态 showing/visible/hiding,将时间延迟设置为500ms/3000ms/500ms
可以控制一些属性项:
标题/内容的字体、颜色;设置标题/内容/关闭按钮的点击或离开时的事件;可以禁用矩形框的聚集(focus);查看更多内容(的按钮)
void Show(string strTitle, string strContent, int nTimeToShow, int nTimeToStay, int nTimeToHide)
显示弹击窗体所需的时间参数:
strTitle
: 弹出消息窗口标题, string 型 strContent
: 弹出消息窗口的内容;string型
nTimeToShow
: 动态显示时间(滚动效果快慢程度)(毫秒) nTimeToStay
: 窗体持续显示时长(毫秒)
nTimeToHide
: 持续多久后自动隐藏
void Hide() 可将popup隐藏
void SetBackgroundBitmap(string strFilename, Color transparencyColor)
设置背景外观的位图样式及其透明度
strFilename:要作为背景位图在硬盘中的路径
transparencyColor: 不可见时的位图颜色
void SetBackgroundBitmap(Image image, Color transparencyColor)
设置背景外观的位图样式及其透明度: image
: 背景图片;
transparencyColor
: 不可见时的位图颜色
void SetCloseBitmap(string strFilename,
Color transparencyColor, Point position)
设置3 个状态下关闭按钮位图,它的透明度和它的座标
strFilename
: 3种状态下关闭按钮位图在磁盘上的路径(width must be a multiple of 3) transparencyColor
: 不可见时的位图颜色(Color of the bitmap which won't be visible) position
: 关闭按钮的位置void SetCloseBitmap(Image image, Color transparencyColor, Point position)
设置3 个状态下关闭按钮位图,它的透明度和它的座标
图象: 图象/位图 代表3个状态下关闭按钮位图的对象(宽度必须是3倍)
透明度:不可见时的位图颜色
位置: 关闭按钮在popup上的位置
string TitleText (get/set) string ContentText (get/set) TaskbarStates TaskbarState (get) Color NormalTitleColor (get/set) Color HoverTitleColor (get/set) Color NormalContentColor (get/set) Color HoverContentColor (get/set) Font NormalTitleFont (get/set) Font HoverTitleFont (get/set) Font NormalContentFont (get/set) Font HoverContentFont (get/set) Rectangle TitleRectangle (get/set) //must be defined before calling show()) Rectangle ContentRectangle (get/set) //must be defined before calling show()) bool TitleClickable (get/set) (default = false); bool ContentClickable (get/set) (default = true); bool CloseClickable (get/set) (default = true); bool EnableSelectionRectangle (get/set) (default = true);
event EventHandler CloseClick event EventHandler TitleClick event EventHandler ContentClick
The popup is skinned using a region generated dynamically from a bitmap and a transparency color:
protected Region BitmapToRegion(Bitmap bitmap, Color transparencyColor)
{
if (bitmap == null)
throw new ArgumentNullException("Bitmap", "Bitmap cannot be null!");
int height = bitmap.Height;
int width = bitmap.Width;
GraphicsPath path = new GraphicsPath();
for (int j=0; j<height; j++ )
for (int i=0; i<width; i++)
{
if (bitmap.GetPixel(i, j) == transparencyColor)
continue;
int x0 = i;
while ((i < width) &&
(bitmap.GetPixel(i, j) != transparencyColor))
i++;
path.AddRectangle(new Rectangle(x0, j, i-x0, 1));
}
Region region = new Region(path);
path.Dispose();
return region;
}
The refresh()
of the popup is done using the double buffering technique to avoid flickering:
protected override void OnPaintBackground(PaintEventArgs pea) { Graphics grfx = pea.Graphics; grfx.PageUnit = GraphicsUnit.Pixel; Graphics offScreenGraphics; Bitmap offscreenBitmap; offscreenBitmap = new Bitmap(BackgroundBitmap.Width, BackgroundBitmap.Height); offScreenGraphics = Graphics.FromImage(offscreenBitmap); if (BackgroundBitmap != null) { offScreenGraphics.DrawImage(BackgroundBitmap, 0, 0, BackgroundBitmap.Width, BackgroundBitmap.Height); } DrawCloseButton(offScreenGraphics); DrawText(offScreenGraphics); grfx.DrawImage(offscreenBitmap, 0, 0); }
Since I wanted to keep only managed code, I used the Screen.GetWorkingArea(WorkAreaRectangle)
function instead of using unmanaged code to get the taskbar position. As a result, I made the popup always appear at the bottom of WorkAreaRectangle
whichever position the taskbar has.
I didn't find any C# managed equivalent to the Win32 function ShowWindow(SW_SHOWNOACTIVATE)
to make the popup, not steal the focus of the active window.
OnMouseUp
handler. TaskbarNotifier
has been done by Patrick Vanden Driessche in VB.NET ShowWindow(SW_SHOWNOACTIVATE)
, to prevent the popup from stealing the focus.