①彈出信息框后慢慢下降消失
在主窗體中新增按鈕重命名為btnShowAndDisappearMessages,在click事件中寫如下代碼:
private void btnShowAndDisappearMessages_Click(object sender, EventArgs e)
{
Messages ms = new Messages();//要彈出的消息框
Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - ms.Width, Screen.PrimaryScreen.WorkingArea.Height);
ms.PointToClient(p);
ms.Location = p;
ms.Show();
for (int i = 0; i < ms.Height; i++)
{
ms.Location = new Point(p.X, p.Y - i);
System.Threading.Thread.Sleep(10);
}
}
在Messages窗體中新增timer計時器,設定Interval為2000,並將Enable屬性設置為true,當此彈出框load過2秒后timer1開始工作,在timer1事件中寫如下代碼:
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
for (int i = 0; i <= this.Height; i++)
{
Point p = new Point(this.Location.X, this.Location.Y + i);//彈出框向下移動消失
this.PointToScreen(p);//即時轉換成屏幕坐標
this.Location = p;// new Point(this.Location.X, this.Location.Y + 1);
System.Threading.Thread.Sleep(10);//線程睡眠時間調的越小向下消失的速度越快。
}
this.Close();
this.Dispose();
}
②彈出信息框后漸漸變淡消失,鼠標移上去后再顯示
在主窗體新增按鈕並重命名為btnMouseShowMessages寫如下代碼:
private void btnMouseShowMessages_Click(object sender, EventArgs e)
{
MouseShowMessages mm = new MouseShowMessages();//要彈出的消息框
Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - mm.Width, Screen.PrimaryScreen.WorkingArea.Height);
mm.PointToClient(p);
mm.Location = p;
mm.Show();
for (int i = 0; i < mm.Height; i++)
{
mm.Location = new Point(p.X, p.Y - i);
System.Threading.Thread.Sleep(10);
}
}
在MouseShowMessages窗體上新增兩個timer控件。
其中timer1的事件代碼如下:(控制信息框消失)
private void timer1_Tick(object sender, EventArgs e)
{
timer2.Enabled = false;//停止timer2計時器,
if (this.Opacity > 0 && this.Opacity <= 1)//開始執行彈出窗漸漸透明
{
this.Opacity = this.Opacity - 0.1;//透明頻度
}
if (System.Windows.Forms.Control.MousePosition.X >= this.Location.X && System.Windows.Forms.Control.MousePosition.Y >= this.Location.Y)//每次都判斷鼠標是否是在彈出窗上,使用鼠標在屏幕上的坐標跟彈出窗體的屏幕坐標做比較。
{
timer2.Enabled = true;//如果鼠標在彈出窗上的時候,timer2開始工作
timer1.Enabled = false;//timer1停止工作。
}
if (this.Opacity == 0)//當透明度==0的時候,關閉彈出窗以釋放資源。
{
this.Close();
this.Dispose();
}
}
timer2的事件代碼如下:(控制檢測鼠標位置)
/// <summary>
/// 判斷鼠標是不是還在彈出框上,如果不是則timer1又可以開始工作了
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer2_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;//timer1停止工作
this.Opacity = 1;//彈出窗透明度設置為1,完全不透明
if (System.Windows.Forms.Control.MousePosition.X < this.Location.X && System.Windows.Forms.Control.MousePosition.Y < this.Location.Y)//如下
{
timer1.Enabled = true;
timer2.Enabled = false;
}
}
處理速度可以根據timer控件的Interval屬性設置。