winform實現的仿Msn移動提示資訊視窗
來源:互聯網
上載者:User
有些軟體在某個特定的時間會顯示一個提示表單,這個表單不是直接顯示的,而是慢慢從視窗的最下方向上移動,直至表單完全顯示就不再移動。當我們點擊“確定”按鈕之後,表單由從螢幕上逐漸下移,直至完全從螢幕上完全不顯示。這也是本文討論的表單效果之一:winform實現的移動提示資訊視窗。
每個Control類都有一個Location屬性,它是一個Point值,這個值表示控制項的左上方的座標值,利用這個座標值,我們可以設定表單的位置。程式核心代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ThreadDemo
{
/// <summary>
/// 說明:這是資訊提示視窗,運行程式的時候,這個視窗會緩慢從螢幕下方
/// 向上移動,知道提示資訊視窗完全顯示;
/// 當點擊“確定”按鈕之後,這個視窗又會緩慢從螢幕地區移出
/// </summary>
public partial class NoteForm : Form
{
private int screenWidth;//螢幕寬度
private int screenHeight;//螢幕高度
private bool finished=false;//是否完全顯示提示視窗
public NoteForm()
{
InitializeComponent();
screenHeight = Screen.PrimaryScreen.Bounds.Height;
screenWidth = Screen.PrimaryScreen.Bounds.Width;
//設定提示視窗座標在螢幕可顯示地區之外
Location = new Point(screenWidth-Width, screenHeight);
}
private void NoteForm_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!finished)//如果提示視窗沒有完全顯示
{
//如果提示視窗的縱座標與提示視窗的高度之和大於螢幕高度
if (Location.Y + Height >= screenHeight)
{
Location = new Point(Location.X, Location.Y - 5);
}
}
else//如果提示視窗已經完成了顯示,並且點擊了確定按鈕
{
//如果提示視窗沒有完全從螢幕上消失
if (Location.Y < screenHeight)
{
Location = new Point(Location.X, Location.Y + 5);
}
}
}
private void btnOK_Click(object sender, EventArgs e)
{
//設定完成了顯示,以便讓提示控制項移出螢幕可顯示地區
finished = true;
}
}
}