【06年博文搬家】用C#实现仿MSN提示框
(1)首先新建一个类库项目,命名为MessageNotify。
(2)在项目中添加一个窗体,命名为MessageNotifyForm,设置如下属性:
ShowInTaskbar:Fasle;
TopMost:True;
FormBorderStyle:FixedToolWindow;
增加三个Timer控件,分别命名为TimerUp,TimerStay,TimerDown,将其Interval设为10。
在窗体上增加一个label控件,命名为lblContent。
(3)MessageNotifyForm的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace KoalaStudio.ModuleLibrary
{
internal partial class MessageNotifyForm : Form
{
//提示框高度
private int _HeightMax;
/// 提示框高度
public int HeightMax
{
set { _HeightMax = value; }
}
/// 提示框停留时间(单位:秒)
public int StayTime
{
set { this.TimerStay.Interval = value ; }
}
/// 显示窗体
public void ShowMessage(string text)
{
this.lblContent.Text = text;
this.TimerUp.Enabled = true;
this.Height = 0;
this.Show();
}
/// 显示窗体
public void ShowMessage(string text, string caption)
{
this.Text = caption ;
this.lblContent.Text = text;
this.TimerUp.Enabled = true;
this.Height = 0;
this.Show();
}
/// 向上移动窗体
private void MoveUp()
{
if (this.Height < _HeightMax)
{
this.Height += 3;
this.Location = new Point(Location.X, Location.Y - 3);
}
else
{
this.TimerUp.Enabled = false;
this.TimerStay.Enabled = true;
}
}
/// 向下移动窗体
private void MoveDown()
{
if (this.Height > 0)
{
this.Height -= 3;
this.Location = new Point(Location.X, Location.Y + 3);
}
else
{
this.TimerDown.Enabled = false;
this.Close();
}
}
public MessageNotifyForm()
{
InitializeComponent();
}
private void MessageNotify_Load(object sender, EventArgs e)
{
//根据不同的屏幕分辨率来确定窗体的初始位置
Screen[] screens = Screen.AllScreens;
Screen screen = screens[0];
this.Location = new Point(screen.WorkingArea.Width - this.Width - 2, screen.WorkingArea.Height - 30);
}
private void TimerUp_Tick(object sender, EventArgs e)
{
this.MoveUp();
}
private void TimerStay_Tick(object sender, EventArgs e)
{
this.TimerStay.Enabled = false;
this.TimerDown.Enabled = true;
}
private void TimerDown_Tick(object sender, EventArgs e)
{
this.MoveDown();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace KoalaStudio.ModuleLibrary
{
internal partial class MessageNotifyForm : Form
{
//提示框高度
private int _HeightMax;
/// 提示框高度
public int HeightMax
{
set { _HeightMax = value; }
}
/// 提示框停留时间(单位:秒)
public int StayTime
{
set { this.TimerStay.Interval = value ; }
}
/// 显示窗体
public void ShowMessage(string text)
{
this.lblContent.Text = text;
this.TimerUp.Enabled = true;
this.Height = 0;
this.Show();
}
/// 显示窗体
public void ShowMessage(string text, string caption)
{
this.Text = caption ;
this.lblContent.Text = text;
this.TimerUp.Enabled = true;
this.Height = 0;
this.Show();
}
/// 向上移动窗体
private void MoveUp()
{
if (this.Height < _HeightMax)
{
this.Height += 3;
this.Location = new Point(Location.X, Location.Y - 3);
}
else
{
this.TimerUp.Enabled = false;
this.TimerStay.Enabled = true;
}
}
/// 向下移动窗体
private void MoveDown()
{
if (this.Height > 0)
{
this.Height -= 3;
this.Location = new Point(Location.X, Location.Y + 3);
}
else
{
this.TimerDown.Enabled = false;
this.Close();
}
}
public MessageNotifyForm()
{
InitializeComponent();
}
private void MessageNotify_Load(object sender, EventArgs e)
{
//根据不同的屏幕分辨率来确定窗体的初始位置
Screen[] screens = Screen.AllScreens;
Screen screen = screens[0];
this.Location = new Point(screen.WorkingArea.Width - this.Width - 2, screen.WorkingArea.Height - 30);
}
private void TimerUp_Tick(object sender, EventArgs e)
{
this.MoveUp();
}
private void TimerStay_Tick(object sender, EventArgs e)
{
this.TimerStay.Enabled = false;
this.TimerDown.Enabled = true;
}
private void TimerDown_Tick(object sender, EventArgs e)
{
this.MoveDown();
}
}
}
(4)添加一个类文件,命名为MessageNotify,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace KoalaStudio.ModuleLibrary
{
public class MessageNotify
{
//当前显示的提示框的数量
private static int _Count = 0;
//提示框宽度
private static int _WidthMax = 180;
//提示框高度
private static int _HeightMax = 120;
//提示框停留时间(单位:毫秒)
private static int _StayTime = 5000;
/// 提示框宽度
public static int WidthMax
{
set { _WidthMax = value; }
get { return _WidthMax; }
}
/// 提示框高度
public static int HeightMax
{
set { _HeightMax = value; }
get { return _HeightMax; }
}
/// 提示框停留时间(单位:秒)
public static int StayTime
{
set { _StayTime = value; }
get { return _StayTime / 1000; }
}
public static void Show(string text)
{
MessageNotifyForm messageNotifyForm = new MessageNotifyForm();
messageNotifyForm.Width = _WidthMax;
messageNotifyForm.HeightMax = _HeightMax;
messageNotifyForm.StayTime = _StayTime;
messageNotifyForm.ShowMessage(text);
}
public static void Show(string text, string caption)
{
MessageNotifyForm messageNotifyForm = new MessageNotifyForm();
messageNotifyForm.Width = _WidthMax;
messageNotifyForm.HeightMax = _HeightMax;
messageNotifyForm.StayTime = _StayTime;
messageNotifyForm.ShowMessage(text, caption);
}
}
}
using System.Collections.Generic;
using System.Text;
namespace KoalaStudio.ModuleLibrary
{
public class MessageNotify
{
//当前显示的提示框的数量
private static int _Count = 0;
//提示框宽度
private static int _WidthMax = 180;
//提示框高度
private static int _HeightMax = 120;
//提示框停留时间(单位:毫秒)
private static int _StayTime = 5000;
/// 提示框宽度
public static int WidthMax
{
set { _WidthMax = value; }
get { return _WidthMax; }
}
/// 提示框高度
public static int HeightMax
{
set { _HeightMax = value; }
get { return _HeightMax; }
}
/// 提示框停留时间(单位:秒)
public static int StayTime
{
set { _StayTime = value; }
get { return _StayTime / 1000; }
}
public static void Show(string text)
{
MessageNotifyForm messageNotifyForm = new MessageNotifyForm();
messageNotifyForm.Width = _WidthMax;
messageNotifyForm.HeightMax = _HeightMax;
messageNotifyForm.StayTime = _StayTime;
messageNotifyForm.ShowMessage(text);
}
public static void Show(string text, string caption)
{
MessageNotifyForm messageNotifyForm = new MessageNotifyForm();
messageNotifyForm.Width = _WidthMax;
messageNotifyForm.HeightMax = _HeightMax;
messageNotifyForm.StayTime = _StayTime;
messageNotifyForm.ShowMessage(text, caption);
}
}
}
(5)将项目生成为dll文件,在应用中引用后即可直接调用,格式如下:
MessageNotify.Show("在窗体中显示的提示信息");
MessageNotify.Show("在窗体中显示的提示信息","在标题栏中显示的标题");
作者:行一山人
出处:http://www.cnblogs.com/benbenkoala/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。