首先,建立一個WINFORM的工程,建立一個窗體,代碼如下:
// C# TaskbarNotifier Class v1.0
// by John O'Byrne - 02 december 2002
// 01 april 2003 : Small fix in the OnMouseUp handler
// 11 january 2003 : Patrick Vanden Driessche <pvdd@devbrains.be> added a few enhancements
// Small Enhancements/Bugfix
// Small bugfix: When Content text measures larger than the corresponding ContentRectangle
// the focus rectangle was not correctly drawn. This has been solved.
// Added KeepVisibleOnMouseOver
// Added ReShowOnMouseOver
// Added If the Title or Content are too long to fit in the corresponding Rectangles,
// the text is truncateed and the ellipses are appended (StringTrimming).
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CustomUIControls
{
/// <summary>
/// TaskbarNotifier allows to display MSN style/Skinned instant messaging popups
/// </summary>
public class TaskbarNotifier : System.Windows.Forms.Form
{
#region TaskbarNotifier Protected Members
protected Bitmap BackgroundBitmap = null;
protected Bitmap CloseBitmap = null;
protected Point CloseBitmapLocation;
protected Size CloseBitmapSize;
protected Rectangle RealTitleRectangle;
protected Rectangle RealContentRectangle;
protected Rectangle WorkAreaRectangle;
protected Timer timer = new Timer();
protected TaskbarStates taskbarState = TaskbarStates.hidden;
protected string titleText;
protected string contentText;
protected Color normalTitleColor = Color.FromArgb(255,0,0);
protected Color hoverTitleColor = Color.FromArgb(255,0,0);
protected Color normalContentColor = Color.FromArgb(0,0,0);
protected Color hoverContentColor = Color.FromArgb(0,0,0x66);
protected Font normalTitleFont = new Font("Arial",12,FontStyle.Regular,GraphicsUnit.Pixel);
protected Font hoverTitleFont = new Font("Arial",12,FontStyle.Bold,GraphicsUnit.Pixel);
protected Font normalContentFont = new Font("Arial",11,FontStyle.Regular,GraphicsUnit.Pixel);
protected Font hoverContentFont = new Font("Arial",11,FontStyle.Regular,GraphicsUnit.Pixel);
protected int nShowEvents;
protected int nHideEvents;
protected int nVisibleEvents;
protected int nIncrementShow;
protected int nIncrementHide;
protected bool bIsMouseOverPopup = false;
protected bool bIsMouseOverClose = false;
protected bool bIsMouseOverContent = false;
protected bool bIsMouseOverTitle = false;
protected bool bIsMouseDown = false;
protected bool bKeepVisibleOnMouseOver = true; // Added Rev 002
protected bool bReShowOnMouseOver = false; // Added Rev 002
#endregion
#region TaskbarNotifier Public Members
public Rectangle TitleRectangle;
public Rectangle ContentRectangle;
public bool TitleClickable = false;
public bool ContentClickable = true;
public bool CloseClickable = true;
public bool EnableSelectionRectangle = true;
public event EventHandler CloseClick = null;
public event EventHandler TitleClick = null;
public event EventHandler ContentClick = null;
#endregion
#region TaskbarNotifier Enums
/// <summary>
/// List of the different popup animation status
/// </summary>
public enum TaskbarStates
{
hidden = 0,
appearing = 1,
visible = 2,
disappearing = 3
}
#endregion
#region TaskbarNotifier Constructor
/// <summary>
/// The Constructor for TaskbarNotifier
/// </summary>
public TaskbarNotifier()
{
// // Window Style
// FormBorderStyle = FormBorderStyle.None;
// WindowState = FormWindowState.Minimized;
// base.Show();
// base.Hide();
WindowState = FormWindowState.Normal;
ShowInTaskbar=false;
ShowInTaskbar = true;
// TopMost = true;
// this.Hide();
MaximizeBox = false;
MinimizeBox = false;
ControlBox = false;
// TaskbarNotifier.ActiveForm.GotFocus+=new EventHandler(ActiveForm_GotFocus);
// //TaskbarNotifier.ActiveForm.LostFocus+=new EventHandler(ActiveForm_GotFocus);
//
// timer.Enabled = true;
// timer.Tick += new EventHandler(OnTimer);
// Window Style
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Minimized;
base.Show();
base.Hide();
WindowState = FormWindowState.Normal;
// ShowInTaskbar = false;
TopMost = true;
TaskbarNotifier.ActiveForm.ShowInTaskbar=true;
MaximizeBox = false;
MinimizeBox = false;
ControlBox = false;
TaskbarNotifier.ActiveForm.GotFocus+=new EventHandler(ActiveForm_GotFocus);
timer.Enabled = true;
timer.Tick += new EventHandler(OnTimer);
}
#endregion
private void ActiveForm_GotFocus(object sender, System.EventArgs e)
{
if(taskbarState==TaskbarStates.hidden)
{
taskbarState = TaskbarStates.appearing;
timer.Start();
}
// else
// {
//
// }
}
#region TaskbarNotifier Properties
/// <summary>
/// Get the current TaskbarState (hidden, showing, visible, hiding)
/// </summary>
public TaskbarStates TaskbarState
{
get
{
return taskbarState;
}
}
/// <summary>
/// Get/Set the popup Title Text
/// </summary>
public string TitleText
{
get
{
return titleText;
}
set
{
titleText=value;
Refresh();
}
}
/// <summary>
/// Get/Set the popup Content Text
/// </summary>
public string ContentText
{
get
{
return contentText;
}
set
{
contentText=value;
Refresh();
}
}
/// <summary>
/// Get/Set the Normal Title Color
/// </summary>
public Color NormalTitleColor
{
get
{
return normalTitleColor;
}
set
{
normalTitleColor = value;
Refresh();
}
}
/// <summary>
/// Get/Set the Hover Title Color
/// </summary>
public Color HoverTitleColor
{
get
{
return hoverTitleColor;
}
set
{
hoverTitleColor = value;
Refresh();
}
}
/// <summary>
/// Get/Set the Normal Content Color
/// </summary>
public Color NormalContentColor
{
get
{
return normalContentColor;
}
set
{
normalContentColor = value;
Refresh();
}
}
/// <summary>
/// Get/Set the Hover Content Color
/// </summary>
public Color HoverContentColor
{
get
{
return hoverContentColor;
}
set
{
hoverContentColor = value;
Refresh();
}
}
/// <summary>
/// Get/Set the Normal Title Font
/// </summary>
public Font NormalTitleFont
{
get
{
return normalTitleFont;
}
set
{
normalTitleFont = value;
Refresh();
}
}
/// <summary>
/// Get/Set the Hover Title Font
/// </summary>
public Font HoverTitleFont
{
get
{
return hoverTitleFont;
}
set
{
hoverTitleFont = value;
Refresh();
}
}
/// <summary>
/// Get/Set the Normal Content Font
/// </summary>
public Font NormalContentFont
{
get
{
return normalContentFont;
}
set
{
normalContentFont = value;
Refresh();
}
}
/// <summary>
/// Get/Set the Hover Content Font
/// </summary>
public Font HoverContentFont
{
get
{
return hoverContentFont;
}
set
{
hoverContentFont = value;
Refresh();
}
}
/// <summary>
/// Indicates if the popup should remain visible when the mouse pointer is over it.
/// Added Rev 002
/// </summary>
public bool KeepVisibleOnMousOver
{
get
{
return bKeepVisibleOnMouseOver;
}
set
{
bKeepVisibleOnMouseOver=value;
}
}
/// <summary>
/// Indicates if the popup should appear again when mouse moves over it while it's disappearing.
/// Added Rev 002
/// </summary>
public bool ReShowOnMouseOver
{
get
{
return bReShowOnMouseOver;
}
set
{
bReShowOnMouseOver=value;
}
}
#endregion
#region TaskbarNotifier Public Methods
[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);
/// <summary>
/// Displays the popup for a certain amount of time
/// </summary>
/// <param name="strTitle">The string which will be shown as the title of the popup</param>
/// <param name="strContent">The string which will be shown as the content of the popup</param>
/// <param name="nTimeToShow">Duration of the showing animation (in milliseconds)</param>
/// <param name="nTimeToStay">Duration of the visible state before collapsing (in milliseconds)</param>
/// <param name="nTimeToHide">Duration of the hiding animation (in milliseconds)</param>
/// <returns>Nothing</returns>
public void Show(string strTitle, string strContent, int nTimeToShow, int nTimeToStay, int nTimeToHide)
{
WorkAreaRectangle = Screen.GetWorkingArea(WorkAreaRectangle);
titleText = strTitle;
contentText = strContent;
nVisibleEvents = nTimeToStay;
CalculateMouseRectangles();
// We calculate the pixel increment and the timer value for the showing animation
int nEvents;
if (nTimeToShow > 10)
{
nEvents = Math.Min((nTimeToShow / 10), BackgroundBitmap.Height);
nShowEvents = nTimeToShow / nEvents;
nIncrementShow = BackgroundBitmap.Height / nEvents;
}
else
{
nShowEvents = 10;
nIncrementShow = BackgroundBitmap.Height;
}
// We calculate the pixel increment and the timer value for the hiding animation
if( nTimeToHide > 10)
{
nEvents = Math.Min((nTimeToHide / 10), BackgroundBitmap.Height);
nHideEvents = nTimeToHide / nEvents;
nIncrementHide = BackgroundBitmap.Height / nEvents;
}
else
{
nHideEvents = 10;
nIncrementHide = BackgroundBitmap.Height;
}
switch (taskbarState)
{
case TaskbarStates.hidden:
taskbarState = TaskbarStates.appearing;
SetBounds(WorkAreaRectangle.Right-BackgroundBitmap.Width-17, WorkAreaRectangle.Bottom-1, BackgroundBitmap.Width, 0);
timer.Interval = nShowEvents;
timer.Start();
// We Show the popup without stealing focus
ShowWindow(this.Handle, 4);
break;
case TaskbarStates.appearing:
Refresh();
break;
case TaskbarStates.visible:
timer.Stop();
timer.Interval = nVisibleEvents;
timer.Start();
Refresh();
break;
case TaskbarStates.disappearing:
timer.Stop();
taskbarState = TaskbarStates.visible;
SetBounds(WorkAreaRectangle.Right-BackgroundBitmap.Width-17, WorkAreaRectangle.Bottom-BackgroundBitmap.Height-1, BackgroundBitmap.Width, BackgroundBitmap.Height);
timer.Interval = nVisibleEvents;
timer.Start();
Refresh();
break;
}
}
/// <summary>
/// Hides the popup
/// </summary>
/// <returns>Nothing</returns>
public new void Hide()
{
if (taskbarState != TaskbarStates.hidden)
{
timer.Stop();
taskbarState = TaskbarStates.hidden;
// base.Hide();
}
}
public void Hide1()
{
if (taskbarState != TaskbarStates.hidden)
{
timer.Stop();
taskbarState = TaskbarStates.hidden;
base.Hide();
base.Close();
base.Dispose();
}
}
/// <summary>
/// Sets the background bitmap and its transparency color
/// </summary>
/// <param name="strFilename">Path of the Background Bitmap on the disk</param>
/// <param name="transparencyColor">Color of the Bitmap which won't be visible</param>
/// <returns>Nothing</returns>
public void SetBackgroundBitmap(string strFilename, Color transparencyColor)
{
BackgroundBitmap = new Bitmap(strFilename);
Width = BackgroundBitmap.Width;
Height = BackgroundBitmap.Height;
Region = BitmapToRegion(BackgroundBitmap, transparencyColor);
}
/// <summary>
/// Sets the background bitmap and its transparency color
/// </summary>
/// <param name="image">Image/Bitmap object which represents the Background Bitmap</param>
/// <param name="transparencyColor">Color of the Bitmap which won't be visible</param>
/// <returns>Nothing</returns>
public void SetBackgroundBitmap(Image image, Color transparencyColor)
{
BackgroundBitmap = new Bitmap(image);
Width = BackgroundBitmap.Width;
Height = BackgroundBitmap.Height;
Region = BitmapToRegion(BackgroundBitmap, transparencyColor);
}
/// <summary>
/// Sets the 3-State Close Button bitmap, its transparency color and its coordinates
/// </summary>
/// <param name="strFilename">Path of the 3-state Close button Bitmap on the disk (width must a multiple of 3)</param>
/// <param name="transparencyColor">Color of the Bitmap which won't be visible</param>
/// <param name="position">Location of the close button on the popup</param>
/// <returns>Nothing</returns>
public void SetCloseBitmap(string strFilename, Color transparencyColor, Point position)
{
CloseBitmap = new Bitmap(strFilename);
CloseBitmap.MakeTransparent(transparencyColor);
CloseBitmapSize = new Size(CloseBitmap.Width/3, CloseBitmap.Height);
CloseBitmapLocation = position;
}
/// <summary>
/// Sets the 3-State Close Button bitmap, its transparency color and its coordinates
/// </summary>
/// <param name="image">Image/Bitmap object which represents the 3-state Close button Bitmap (width must be a multiple of 3)</param>
/// <param name="transparencyColor">Color of the Bitmap which won't be visible</param>
/// /// <param name="position">Location of the close button on the popup</param>
/// <returns>Nothing</returns>
public void SetCloseBitmap(Image image, Color transparencyColor, Point position)
{
CloseBitmap = new Bitmap(image);
CloseBitmap.MakeTransparent(transparencyColor);
CloseBitmapSize = new Size(CloseBitmap.Width/3, CloseBitmap.Height);
CloseBitmapLocation = position;
}
#endregion
#region TaskbarNotifier Protected Methods
protected void DrawCloseButton(Graphics grfx)
{
if (CloseBitmap != null)
{
Rectangle rectDest = new Rectangle(CloseBitmapLocation, CloseBitmapSize);
Rectangle rectSrc;
if (bIsMouseOverClose)
{
if (bIsMouseDown)
rectSrc = new Rectangle(new Point(CloseBitmapSize.Width*2, 0), CloseBitmapSize);
else
rectSrc = new Rectangle(new Point(CloseBitmapSize.Width, 0), CloseBitmapSize);
}
else
rectSrc = new Rectangle(new Point(0, 0), CloseBitmapSize);
grfx.DrawImage(CloseBitmap, rectDest, rectSrc, GraphicsUnit.Pixel);
}
}
protected void DrawText(Graphics grfx)
{
if (titleText != null && titleText.Length != 0)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.NoWrap;
sf.Trimming = StringTrimming.EllipsisCharacter; // Added Rev 002
if (bIsMouseOverTitle)
grfx.DrawString(titleText, hoverTitleFont, new SolidBrush(hoverTitleColor), TitleRectangle, sf);
else
grfx.DrawString(titleText, normalTitleFont, new SolidBrush(normalTitleColor), TitleRectangle, sf);
}
if (contentText != null && contentText.Length != 0)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
sf.Trimming = StringTrimming.Word; // Added Rev 002
if (bIsMouseOverContent)
{
grfx.DrawString(contentText, hoverContentFont, new SolidBrush(hoverContentColor), ContentRectangle, sf);
if (EnableSelectionRectangle)
ControlPaint.DrawBorder3D(grfx, RealContentRectangle, Border3DStyle.Etched, Border3DSide.Top | Border3DSide.Bottom | Border3DSide.Left | Border3DSide.Right);
}
else
grfx.DrawString(contentText, normalContentFont, new SolidBrush(normalContentColor), ContentRectangle, sf);
}
}
protected void CalculateMouseRectangles()
{
Graphics grfx = CreateGraphics();
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
SizeF sizefTitle = grfx.MeasureString(titleText, hoverTitleFont, TitleRectangle.Width, sf);
SizeF sizefContent = grfx.MeasureString(contentText, hoverContentFont, ContentRectangle.Width, sf);
grfx.Dispose();
// Added Rev 002
//We should check if the title size really fits inside the pre-defined title rectangle
if (sizefTitle.Height > TitleRectangle.Height)
{
RealTitleRectangle = new Rectangle(TitleRectangle.Left, TitleRectangle.Top, TitleRectangle.Width , TitleRectangle.Height );
}
else
{
RealTitleRectangle = new Rectangle(TitleRectangle.Left, TitleRectangle.Top, (int)sizefTitle.Width, (int)sizefTitle.Height);
}
RealTitleRectangle.Inflate(0,2);
// Added Rev 002
//We should check if the Content size really fits inside the pre-defined Content rectangle
if (sizefContent.Height > ContentRectangle.Height)
{
RealContentRectangle = new Rectangle((ContentRectangle.Width-(int)sizefContent.Width)/2+ContentRectangle.Left, ContentRectangle.Top, (int)sizefContent.Width, ContentRectangle.Height );
}
else
{
RealContentRectangle = new Rectangle((ContentRectangle.Width-(int)sizefContent.Width)/2+ContentRectangle.Left, (ContentRectangle.Height-(int)sizefContent.Height)/2+ContentRectangle.Top, (int)sizefContent.Width, (int)sizefContent.Height);
}
RealContentRectangle.Inflate(0,2);
}
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;
}
#endregion
#region TaskbarNotifier Events Overrides
protected void OnTimer(Object obj, EventArgs ea)
{
switch (taskbarState)
{
case TaskbarStates.appearing:
if (Height < BackgroundBitmap.Height)
SetBounds(Left, Top-nIncrementShow ,Width, Height + nIncrementShow);
else
{
timer.Stop();
Height = BackgroundBitmap.Height;
timer.Interval = nVisibleEvents;
taskbarState = TaskbarStates.visible;
timer.Start();
}
break;
case TaskbarStates.visible:
timer.Stop();
timer.Interval = nHideEvents;
// Added Rev 002
if ((bKeepVisibleOnMouseOver && !bIsMouseOverPopup ) || (!bKeepVisibleOnMouseOver))
{
taskbarState = TaskbarStates.disappearing;
}
//taskbarState = TaskbarStates.disappearing; // Rev 002
// taskbarState=TaskbarStates.appearing;
timer.Start();
break;
case TaskbarStates.disappearing:
// Added Rev 002
if (bReShowOnMouseOver && bIsMouseOverPopup)
{
taskbarState = TaskbarStates.appearing;
}
else
{
if (Top < WorkAreaRectangle.Bottom)
SetBounds(Left, Top + nIncrementHide, Width, Height - nIncrementHide);
else
Hide();
}
break;
}
}
protected override void OnMouseEnter(EventArgs ea)
{
base.OnMouseEnter(ea);
bIsMouseOverPopup = true;
Refresh();
}
protected override void OnMouseLeave(EventArgs ea)
{
base.OnMouseLeave(ea);
bIsMouseOverPopup = false;
bIsMouseOverClose = false;
bIsMouseOverTitle = false;
bIsMouseOverContent = false;
Refresh();
}
protected override void OnMouseMove(MouseEventArgs mea)
{
base.OnMouseMove(mea);
bool bContentModified = false;
if ( (mea.X > CloseBitmapLocation.X) && (mea.X < CloseBitmapLocation.X + CloseBitmapSize.Width) && (mea.Y > CloseBitmapLocation.Y) && (mea.Y < CloseBitmapLocation.Y + CloseBitmapSize.Height) && CloseClickable )
{
if (!bIsMouseOverClose)
{
bIsMouseOverClose = true;
bIsMouseOverTitle = false;
bIsMouseOverContent = false;
Cursor = Cursors.Hand;
bContentModified = true;
}
}
else if (RealContentRectangle.Contains(new Point(mea.X, mea.Y)) && ContentClickable)
{
if (!bIsMouseOverContent)
{
bIsMouseOverClose = false;
bIsMouseOverTitle = false;
bIsMouseOverContent = true;
Cursor = Cursors.Hand;
bContentModified = true;
}
}
else if (RealTitleRectangle.Contains(new Point(mea.X, mea.Y)) && TitleClickable)
{
if (!bIsMouseOverTitle)
{
bIsMouseOverClose = false;
bIsMouseOverTitle = true;
bIsMouseOverContent = false;
Cursor = Cursors.Hand;
bContentModified = true;
}
}
else
{
if (bIsMouseOverClose || bIsMouseOverTitle || bIsMouseOverContent)
bContentModified = true;
bIsMouseOverClose = false;
bIsMouseOverTitle = false;
bIsMouseOverContent = false;
Cursor = Cursors.Default;
}
if (bContentModified)
Refresh();
}
protected override void OnMouseDown(MouseEventArgs mea)
{
base.OnMouseDown(mea);
bIsMouseDown = true;
if (bIsMouseOverClose)
Refresh();
}
protected override void OnMouseUp(MouseEventArgs mea)
{
base.OnMouseUp(mea);
bIsMouseDown = false;
if (bIsMouseOverClose)
{
Hide1();
if (CloseClick != null)
CloseClick(this, new EventArgs());
}
else if (bIsMouseOverTitle)
{
Hide1();
if (TitleClick != null)
TitleClick(this, new EventArgs());
}
else if (bIsMouseOverContent)
{
Hide1();
if (ContentClick != null)
ContentClick(this, new EventArgs());
}
}
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);
}
#endregion
private void InitializeComponent()
{
//
// TaskbarNotifier
//
// this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.AutoScaleBaseSize = new System.Drawing.Size(1, 1);
// this.ClientSize = new System.Drawing.Size(292, 266);
this.ClientSize = new System.Drawing.Size(1, 1);
this.Name = "TaskbarNotifier";
}
}
}
然後,再建立一個WINFORM,代碼如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CustomUIControls;
namespace TaskbarNotifierDemo
{
/// <summary>
/// Description r廥um嶪 de Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button ButtonShowPopup1;
private System.Windows.Forms.Button ButtonShowPopup2;
private System.Windows.Forms.Button ButtonShowPopup3; // Added Rev 002
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.CheckBox checkBoxSelectionRectangle;
private System.Windows.Forms.TextBox textBoxTitle;
private System.Windows.Forms.TextBox textBoxContent;
private System.Windows.Forms.TextBox textBoxDelayShowing;
private System.Windows.Forms.TextBox textBoxDelayStaying;
private System.Windows.Forms.TextBox textBoxDelayHiding;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.CheckBox checkBoxTitleClickable;
private System.Windows.Forms.CheckBox checkBoxContentClickable;
private System.Windows.Forms.CheckBox checkBoxCloseClickable;
private System.Windows.Forms.CheckBox checkBoxReShowOnMouseOver; // Added Rev 002
private System.Windows.Forms.CheckBox checkBoxKeepVisibleOnMouseOver; // Added Rev 002
TaskbarNotifier taskbarNotifier1;
TaskbarNotifier taskbarNotifier2;
private System.Windows.Forms.NotifyIcon notifyIcon1;
TaskbarNotifier taskbarNotifier3;
private ContextMenu notifyiconMnu ;
public Form1()
{
//
// Requis pour la prise en charge du Concepteur Windows Forms
//
InitializeComponent();
textBoxContent.Text="This is a sample content, it can spread on multiple lines";
textBoxTitle.Text="Title";
textBoxDelayShowing.Text="500";
textBoxDelayStaying.Text="3000";
textBoxDelayHiding.Text="500";
checkBoxSelectionRectangle.Checked=true;
checkBoxTitleClickable.Checked=false;
checkBoxContentClickable.Checked=true;
checkBoxCloseClickable.Checked=true;
checkBoxKeepVisibleOnMouseOver.Checked = true; // Added Rev 002
checkBoxReShowOnMouseOver.Checked = false; // Added Rev 002
taskbarNotifier1=new TaskbarNotifier();
taskbarNotifier1.SetBackgroundBitmap(new Bitmap(GetType(),"skin.bmp"),Color.FromArgb(255,0,255));
taskbarNotifier1.SetCloseBitmap(new Bitmap(GetType(),"close.bmp"),Color.FromArgb(255,0,255),new Point(127,8));
taskbarNotifier1.TitleRectangle=new Rectangle(40,9,70,25);
taskbarNotifier1.ContentRectangle=new Rectangle(8,41,133,68);
taskbarNotifier1.TitleClick+=new EventHandler(TitleClick);
taskbarNotifier1.ContentClick+=new EventHandler(ContentClick);
taskbarNotifier1.CloseClick+=new EventHandler(CloseClick);
taskbarNotifier2=new TaskbarNotifier();
taskbarNotifier2.SetBackgroundBitmap(new Bitmap(GetType(),"skin2.bmp"),Color.FromArgb(255,0,255));
taskbarNotifier2.SetCloseBitmap(new Bitmap(GetType(),"close2.bmp"),Color.FromArgb(255,0,255),new Point(300,44));
taskbarNotifier2.TitleRectangle=new Rectangle(123,50,176,16);
taskbarNotifier2.ContentRectangle=new Rectangle(116,67,197,52);
taskbarNotifier2.TitleClick+=new EventHandler(TitleClick);
taskbarNotifier2.ContentClick+=new EventHandler(ContentClick);
taskbarNotifier2.CloseClick+=new EventHandler(CloseClick);
// Added Rev 002
taskbarNotifier3=new TaskbarNotifier();
taskbarNotifier3.SetBackgroundBitmap(new Bitmap(GetType(),"skin3.bmp"),Color.FromArgb(255,0,255));
taskbarNotifier3.SetCloseBitmap(new Bitmap(GetType(),"close.bmp"),Color.FromArgb(255,0,255),new Point(280,57));
taskbarNotifier3.TitleRectangle=new Rectangle(150, 57, 125, 28);
taskbarNotifier3.ContentRectangle=new Rectangle(75, 92, 215, 55);
taskbarNotifier3.TitleClick+=new EventHandler(TitleClick);
taskbarNotifier3.ContentClick+=new EventHandler(ContentClick);
taskbarNotifier3.CloseClick+=new EventHandler(CloseClick);
Initializenotifyicon();
}
/// <summary>
/// Nettoyage des ressources utilis嶪s.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// M彋hode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette m彋hode avec l'嶮iteur de code.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.ButtonShowPopup1 = new System.Windows.Forms.Button();
this.ButtonShowPopup2 = new System.Windows.Forms.Button();
this.checkBoxSelectionRectangle = new System.Windows.Forms.CheckBox();
this.textBoxTitle = new System.Windows.Forms.TextBox();
this.textBoxContent = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBoxDelayShowing = new System.Windows.Forms.TextBox();
this.textBoxDelayStaying = new System.Windows.Forms.TextBox();
this.textBoxDelayHiding = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.checkBoxReShowOnMouseOver = new System.Windows.Forms.CheckBox();
this.checkBoxKeepVisibleOnMouseOver = new System.Windows.Forms.CheckBox();
this.checkBoxCloseClickable = new System.Windows.Forms.CheckBox();
this.checkBoxContentClickable = new System.Windows.Forms.CheckBox();
this.checkBoxTitleClickable = new System.Windows.Forms.CheckBox();
this.ButtonShowPopup3 = new System.Windows.Forms.Button();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// ButtonShowPopup1
//
this.ButtonShowPopup1.Location = new System.Drawing.Point(8, 436);
this.ButtonShowPopup1.Name = "ButtonShowPopup1";
this.ButtonShowPopup1.Size = new System.Drawing.Size(88, 31);
this.ButtonShowPopup1.TabIndex = 0;
this.ButtonShowPopup1.Text = "Show popup 1";
this.ButtonShowPopup1.Click += new System.EventHandler(this.ButtonShowPopup1_Click);
//
// ButtonShowPopup2
//
this.ButtonShowPopup2.Location = new System.Drawing.Point(108, 436);
this.ButtonShowPopup2.Name = "ButtonShowPopup2";
this.ButtonShowPopup2.Size = new System.Drawing.Size(88, 31);
this.ButtonShowPopup2.TabIndex = 1;
this.ButtonShowPopup2.Text = "Show popup 2";
this.ButtonShowPopup2.Click += new System.EventHandler(this.ButtonShowPopup2_Click);
//
// checkBoxSelectionRectangle
//
this.checkBoxSelectionRectangle.Location = new System.Drawing.Point(128, 63);
this.checkBoxSelectionRectangle.Name = "checkBoxSelectionRectangle";
this.checkBoxSelectionRectangle.Size = new System.Drawing.Size(160, 22);
this.checkBoxSelectionRectangle.TabIndex = 2;
this.checkBoxSelectionRectangle.Text = "Show Selection Rectangle";
//
// textBoxTitle
//
this.textBoxTitle.Location = new System.Drawing.Point(64, 43);
this.textBoxTitle.Name = "textBoxTitle";
this.textBoxTitle.Size = new System.Drawing.Size(224, 22);
this.textBoxTitle.TabIndex = 3;
this.textBoxTitle.Text = "textBoxTitle";
//
// textBoxContent
//
this.textBoxContent.Location = new System.Drawing.Point(64, 85);
this.textBoxContent.Name = "textBoxContent";
this.textBoxContent.Size = new System.Drawing.Size(224, 22);
this.textBoxContent.TabIndex = 4;
this.textBoxContent.Text = "textBoxContent";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 43);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(40, 20);
this.label1.TabIndex = 5;
this.label1.Text = "Title";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 85);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 21);
this.label2.TabIndex = 6;
this.label2.Text = "Content";
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(8, 10);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(296, 118);
this.groupBox1.TabIndex = 7;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Text";
//
// label5
//
this.label5.Location = new System.Drawing.Point(208, 171);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(80, 21);
this.label5.TabIndex = 13;
this.label5.Text = "Delay Hiding";
//
// label4
//
this.label4.Location = new System.Drawing.Point(120, 171);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 21);
this.label4.TabIndex = 12;
this.label4.Text = "Delay Staying";
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 171);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(80, 21);
this.label3.TabIndex = 11;
this.label3.Text = "Delay Showing";
//
// textBoxDelayShowing
//
this.textBoxDelayShowing.Location = new System.Drawing.Point(32, 202);
this.textBoxDelayShowing.Name = "textBoxDelayShowing";
this.textBoxDelayShowing.Size = new System.Drawing.Size(56, 22);
this.textBoxDelayShowing.TabIndex = 10;
this.textBoxDelayShowing.Text = "textBoxDelayShowing";
//
// textBoxDelayStaying
//
this.textBoxDelayStaying.Location = new System.Drawing.Point(128, 202);
this.textBoxDelayStaying.Name = "textBoxDelayStaying";
this.textBoxDelayStaying.Size = new System.Drawing.Size(56, 22);
this.textBoxDelayStaying.TabIndex = 9;
this.textBoxDelayStaying.Text = "textBoxDelayStaying";
//
// textBoxDelayHiding
//
this.textBoxDelayHiding.Location = new System.Drawing.Point(216, 202);
this.textBoxDelayHiding.Name = "textBoxDelayHiding";
this.textBoxDelayHiding.Size = new System.Drawing.Size(56, 22);
this.textBoxDelayHiding.TabIndex = 8;
this.textBoxDelayHiding.Text = "textBoxDelayHiding";
//
// groupBox2
//
this.groupBox2.Location = new System.Drawing.Point(8, 138);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(296, 118);
this.groupBox2.TabIndex = 14;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Animation Delays (ms)";
//
// groupBox3
//
this.groupBox3.Controls.Add(this.checkBoxReShowOnMouseOver);
this.groupBox3.Controls.Add(this.checkBoxKeepVisibleOnMouseOver);
this.groupBox3.Controls.Add(this.checkBoxCloseClickable);
this.groupBox3.Controls.Add(this.checkBoxContentClickable);
this.groupBox3.Controls.Add(this.checkBoxTitleClickable);
this.groupBox3.Controls.Add(this.checkBoxSelectionRectangle);
this.groupBox3.Location = new System.Drawing.Point(8, 267);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(296, 159);
this.groupBox3.TabIndex = 15;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Options";
//
// checkBoxReShowOnMouseOver
//
this.checkBoxReShowOnMouseOver.Location = new System.Drawing.Point(16, 122);
this.checkBoxReShowOnMouseOver.Name = "checkBoxReShowOnMouseOver";
this.checkBoxReShowOnMouseOver.Size = new System.Drawing.Size(268, 22);
this.checkBoxReShowOnMouseOver.TabIndex = 7;
this.checkBoxReShowOnMouseOver.Text = "Re-show when Mouse over window when hiding";
//
// checkBoxKeepVisibleOnMouseOver
//
this.checkBoxKeepVisibleOnMouseOver.Location = new System.Drawing.Point(16, 96);
this.checkBoxKeepVisibleOnMouseOver.Name = "checkBoxKeepVisibleOnMouseOver";
this.checkBoxKeepVisibleOnMouseOver.Size = new System.Drawing.Size(268, 22);
this.checkBoxKeepVisibleOnMouseOver.TabIndex = 6;
this.checkBoxKeepVisibleOnMouseOver.Text = "Keep Visible when Mouse over window";
//
// checkBoxCloseClickable
//
this.checkBoxCloseClickable.Location = new System.Drawing.Point(16, 63);
this.checkBoxCloseClickable.Name = "checkBoxCloseClickable";
this.checkBoxCloseClickable.Size = new System.Drawing.Size(104, 22);
this.checkBoxCloseClickable.TabIndex = 3;
this.checkBoxCloseClickable.Text = "Close Clickable";
//
// checkBoxContentClickable
//
this.checkBoxContentClickable.Location = new System.Drawing.Point(128, 32);
this.checkBoxContentClickable.Name = "checkBoxContentClickable";
this.checkBoxContentClickable.Size = new System.Drawing.Size(112, 21);
this.checkBoxContentClickable.TabIndex = 1;
this.checkBoxContentClickable.Text = "Content Clickable";
//
// checkBoxTitleClickable
//
this.checkBoxTitleClickable.Location = new System.Drawing.Point(16, 32);
this.checkBoxTitleClickable.Name = "checkBoxTitleClickable";
this.checkBoxTitleClickable.Size = new System.Drawing.Size(96, 21);
this.checkBoxTitleClickable.TabIndex = 0;
this.checkBoxTitleClickable.Text = "Title Clickable";
//
// ButtonShowPopup3
//
this.ButtonShowPopup3.Location = new System.Drawing.Point(208, 436);
this.ButtonShowPopup3.Name = "ButtonShowPopup3";
this.ButtonShowPopup3.Size = new System.Drawing.Size(88, 31);
this.ButtonShowPopup3.TabIndex = 20;
this.ButtonShowPopup3.Text = "Show popup 3";
this.ButtonShowPopup3.Click += new System.EventHandler(this.ButtonShowPopup3_Click);
//
// notifyIcon1
//
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "Message";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(320, 478);
this.Controls.Add(this.ButtonShowPopup3);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBoxDelayShowing);
this.Controls.Add(this.textBoxDelayStaying);
this.Controls.Add(this.textBoxDelayHiding);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBoxContent);
this.Controls.Add(this.textBoxTitle);
this.Controls.Add(this.ButtonShowPopup2);
this.Controls.Add(this.ButtonShowPopup1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox2);
this.MaximizeBox = false;
this.Name = "Form1";
this.ShowInTaskbar = false;
this.Text = "C# TaskbarNotifier Demo";
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void Initializenotifyicon( )
{
System.Windows.Forms.MenuItem[] mnuItms=new MenuItem[3];
mnuItms[0]=new MenuItem();
mnuItms[0].Text="abcd";
mnuItms[0].Click+=new EventHandler(this.showmessage);
mnuItms[1]=new MenuItem("-");
mnuItms[2]=new MenuItem();
mnuItms[2].Text="退出系統";
mnuItms[2].Click+=new EventHandler(this.ExitSelect);
mnuItms[2].DefaultItem=true;
notifyiconMnu=new ContextMenu (mnuItms);
notifyIcon1.ContextMenu=notifyiconMnu;
}
public void showmessage ( object sender, System.EventArgs e )
{
MessageBox.Show ( "C#做的托盤程序!");
}
public void ExitSelect ( object sender , System.EventArgs e )
{
notifyIcon1.Visible=false;
this.Close();
Application.Exit();
}
/// <summary>
/// Point d'entr嶪 principal de l'application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
static bool IsNumeric(string str)
{
if (str==null || str.Length==0)
return false;
foreach(char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
}
private void ButtonShowPopup1_Click(object sender, System.EventArgs e)
{
if (textBoxTitle.Text.Length==0 || textBoxContent.Text.Length==0)
{
MessageBox.Show("Enter a title and a content Text");
return;
}
if (!IsNumeric(textBoxDelayShowing.Text) || !IsNumeric(textBoxDelayStaying.Text) || !IsNumeric(textBoxDelayHiding.Text))
{
MessageBox.Show("Enter valid Delays (integers)");
return;
}
taskbarNotifier1.CloseClickable=checkBoxCloseClickable.Checked;
taskbarNotifier1.TitleClickable=checkBoxTitleClickable.Checked;
taskbarNotifier1.ContentClickable=checkBoxContentClickable.Checked;
taskbarNotifier1.EnableSelectionRectangle=checkBoxSelectionRectangle.Checked;
taskbarNotifier1.KeepVisibleOnMousOver=checkBoxKeepVisibleOnMouseOver.Checked; // Added Rev 002
taskbarNotifier1.ReShowOnMouseOver=checkBoxReShowOnMouseOver.Checked; // Added Rev 002
taskbarNotifier1.Show(textBoxTitle.Text,textBoxContent.Text,Int32.Parse(textBoxDelayShowing.Text),Int32.Parse(textBoxDelayStaying.Text),Int32.Parse(textBoxDelayHiding.Text));
}
private void ButtonShowPopup2_Click(object sender, System.EventArgs e)
{
// if (textBoxTitle.Text.Length==0 || textBoxContent.Text.Length==0)
// {
// MessageBox.Show("Enter a title and a content Text");
// return;
// }
// if (!IsNumeric(textBoxDelayShowing.Text) || !IsNumeric(textBoxDelayStaying.Text) || !IsNumeric(textBoxDelayHiding.Text))
// {
// MessageBox.Show("Enter valid Delays (integers)");
// return;
// }
//
// taskbarNotifier2.CloseClickable=checkBoxCloseClickable.Checked;
// taskbarNotifier2.TitleClickable=checkBoxTitleClickable.Checked;
// taskbarNotifier2.ContentClickable=checkBoxContentClickable.Checked;
// taskbarNotifier2.EnableSelectionRectangle=checkBoxSelectionRectangle.Checked;
// taskbarNotifier2.KeepVisibleOnMousOver=checkBoxKeepVisibleOnMouseOver.Checked; // Added Rev 002
// taskbarNotifier2.ReShowOnMouseOver=checkBoxReShowOnMouseOver.Checked; // Added Rev 002
// taskbarNotifier2.Show(textBoxTitle.Text,textBoxContent.Text,Int32.Parse(textBoxDelayShowing.Text),Int32.Parse(textBoxDelayStaying.Text),Int32.Parse(textBoxDelayHiding.Text));
TaskbarNotifier taskbarNotifierx=new TaskbarNotifier();
taskbarNotifierx.SetBackgroundBitmap(new Bitmap(GetType(),"skin2.bmp"),Color.FromArgb(255,0,255));
taskbarNotifierx.SetCloseBitmap(new Bitmap(GetType(),"close2.bmp"),Color.FromArgb(255,0,255),new Point(300,44));
taskbarNotifierx.TitleRectangle=new Rectangle(123,50,176,16);
taskbarNotifierx.ContentRectangle=new Rectangle(116,67,197,52);
taskbarNotifierx.TitleClick+=new EventHandler(TitleClick);
taskbarNotifierx.ContentClick+=new EventHandler(ContentClick);
taskbarNotifierx.CloseClick+=new EventHandler(CloseClick);
taskbarNotifierx.CloseClickable=checkBoxCloseClickable.Checked;
taskbarNotifierx.TitleClickable=checkBoxTitleClickable.Checked;
taskbarNotifierx.ContentClickable=checkBoxContentClickable.Checked;
taskbarNotifierx.EnableSelectionRectangle=checkBoxSelectionRectangle.Checked;
taskbarNotifierx.KeepVisibleOnMousOver=checkBoxKeepVisibleOnMouseOver.Checked; // Added Rev 002
taskbarNotifierx.ReShowOnMouseOver=checkBoxReShowOnMouseOver.Checked; // Added Rev 002
taskbarNotifierx.Show(textBoxTitle.Text,textBoxContent.Text,Int32.Parse(textBoxDelayShowing.Text),Int32.Parse(textBoxDelayStaying.Text),Int32.Parse(textBoxDelayHiding.Text));
}
// Added Rev 002
private void ButtonShowPopup3_Click(object sender, System.EventArgs e)
{
if (textBoxTitle.Text.Length==0 || textBoxContent.Text.Length==0)
{
MessageBox.Show("Enter a title and a content Text");
return;
}
if (!IsNumeric(textBoxDelayShowing.Text) || !IsNumeric(textBoxDelayStaying.Text) || !IsNumeric(textBoxDelayHiding.Text))
{
MessageBox.Show("Enter valid Delays (integers)");
return;
}
taskbarNotifier3.CloseClickable=checkBoxCloseClickable.Checked;
taskbarNotifier3.TitleClickable=checkBoxTitleClickable.Checked;
taskbarNotifier3.ContentClickable=checkBoxContentClickable.Checked;
taskbarNotifier3.EnableSelectionRectangle=checkBoxSelectionRectangle.Checked;
taskbarNotifier3.KeepVisibleOnMousOver=checkBoxKeepVisibleOnMouseOver.Checked; // Added Rev 002
taskbarNotifier3.ReShowOnMouseOver=checkBoxReShowOnMouseOver.Checked; // Added Rev 002
taskbarNotifier3.Show(textBoxTitle.Text,textBoxContent.Text,Int32.Parse(textBoxDelayShowing.Text),Int32.Parse(textBoxDelayStaying.Text),Int32.Parse(textBoxDelayHiding.Text));
}
void CloseClick(object obj,EventArgs ea)
{
//MessageBox.Show("Closed was Clicked");
}
void TitleClick(object obj,EventArgs ea)
{
//MessageBox.Show("Title was Clicked");
MessageBox.Show(textBoxContent.Text +"/n",textBoxTitle.Text);
}
void ContentClick(object obj,EventArgs ea)
{
//MessageBox.Show("Content was Clicked");
MessageBox.Show(textBoxContent.Text +"/n",textBoxTitle.Text);
}
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{
this.Show();
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;
this.Activate();
}
}
}
注意,在建立TaskbarNotifier類時,畫Rectangle時,可以根據你的不同的圖片調整大小,盡量畫的好看些就行了,找圖片的時候,也可以找個好看些的圖片。
以上隻用於WINFORM,WEBFORM的就很多了,網絡上找到很容易!