使用過MSN的朋友應該都知道, 當有新訊息時, 右下角會跳出一個提示小方框
這邊用簡單的程式碼來達到類似的效果, 當然這只是陽春版, 只有簡單的訊息提示而已
實作效果: 訊息方框由右下角緩慢上升 > 靜止數秒 > 緩慢下降 > 關閉提示視窗
程式碼片段如下:
// 建立訊息提示方框物件
public MsgNotify(String sender)
{
InitializeComponent();
this.m_message = sender;
this.ShowInTaskbar = false;
this.tNotifierUP.Tick += new EventHandler(positionUP);
this.tNotifierStall.Tick += new EventHandler(stall);
this.tNotifierDown.Tick += new EventHandler(positionDN);
this.tNotifierClose.Tick += new EventHandler(closeNotifier);
this.tNotifierUP.Start();
}
// 提示框上升
private void positionUP(Object myObject, EventArgs myEventArgs)
{
if (this.Location.Y <= SystemInformation.WorkingArea.Bottom - this.Height)
{
this.tNotifierUP.Stop();
this.tNotifierStall.Start();
}
this.Location = new Point(this.Location.X, this.Location.Y - 5);
}
// 提示框下降
private void positionDN(Object myObject, EventArgs myEventArgs)
{
if (this.Location.Y >= SystemInformation.WorkingArea.Bottom)
{
this.tNotifierDown.Stop();
this.tNotifierClose.Start();
}
this.Location = new Point(this.Location.X, this.Location.Y + 5);
}
// 提示框暫留
private void stall(Object myObject, EventArgs myEventArgs)
{
this.tNotifierDown.Start();
}
private void closeNotifier(Object myObject, EventArgs myEventArgs)
{
this.Close();
}
// 滑鼠事件
private void MsgNotify_MouseEnter(object sender, EventArgs e)
{
this.Location = new Point(SystemInformation.WorkingArea.Right - this.Width,
SystemInformation.WorkingArea.Bottom - this.Height);
this.tNotifierStall.Stop();
this.tNotifierDown.Stop();
this.tNotifierClose.Stop();
}
// 滑鼠事件
private void MsgNotify_MouseLeave(object sender, EventArgs e)
{
this.tNotifierStall.Start();
}
這是最最陽春的提示效果, 程式碼不難, 細節就不多做註解囉

this.m_message this.tNotifierUP this.tNotifierStall this.tNotifierDown this.tNotifierClose this.tNotifierUP 您好! 可以請敎一下,上面的變數的型態或者是用哪個元件嗎? 有點不太了解
原來是Timer 小弟真是受敎了 這麼好的教學文章 真是太感謝大大了
不客氣~ 計時的部分 因為方法不只一種 當初文章也就只列出概略說明啦 勉強算是pseudocode吧 (?
您好 ,看了上面的訪客留言,還是看不太懂呢,請問 // 建立訊息提示方框物件 這一串是放在一開始表單建立的這個裡面嗎,懇請解答 namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } } }