程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> 讓窗體飄動起來--C#中Timer組件用法

讓窗體飄動起來--C#中Timer組件用法

編輯:C#基礎知識

  Timer組件是也是一個WinForm組件了,和其他的WinForm組件的最大區別是:Timer組件是不可見的,而其他大部分的組件都是都是可見的,可以設計的。Timer組件也被封裝在名稱空間System.Windows.Forms中,其主要作用是當Timer組件啟動後,每隔一個固定時間段,觸發相同的事件。Timer組件在程序設計中是一個比較常用的組件,雖然屬性、事件都很少,但在有些地方使用它會產生意想不到的效果。

  本文介紹的程序,是用Visual C#做的一個窗體飄動的程序,這其中就大量的使用了Timer組件。下面就來介紹一下,這個程序的設計和運行的環境。

  一. 本文程序設計和運行的軟件環境:

  (1).微軟公司視窗2000服務器版

  (2)..Net FrameWork SDK Beta 2

  二. 程序設計的思路以及關鍵步驟的解決方法:

  其實要使得程序的窗體飄動起來,其實思路是比較簡單的。首先是當加載窗體的時候,給窗體設定一個顯示的初始位置。然後通過在窗體中定義的二個Timer組件,其中一個叫Timer1,其作用是控制窗體從左往右飄動(當然如果你願意,你也可以改為從上往下飄動,或者其他的飄動方式。),另外一個Timer2是控制窗體從右往左飄動(同樣你也可以改為其他飄動方式)。當然這二個Timer組件不能同時啟動,在本文的程序中,是先設定Timer1組件啟動的,當此Timer1啟動後,每隔0.01秒,都會在觸發的事件中給窗體的左上角的橫坐標都加上"1",這時我們看到的結果是窗體從左往右不斷移動,當移動到一定的位置後,Timer1停止。Timer2啟動,每隔0.01秒,在觸發定義的事件中給窗體的左上角的橫坐標都減去"1",這時我們看到的結果是窗體從右往左不斷移動。當移動到一定位置後,Timer1啟動,Timer2停止,如此反覆,這樣窗體也就飄動起來了。要實現上述思路,必須解決好以下問題。

  (1).如何設定窗體的初始位置:

  設定窗體的初始位置,是在事件Form1_Load()中進行的。此事件是當窗體加載的時候觸發的。Form有一個DesktopLocation屬性,這個屬性是設定窗體的左上角的二維位置。在程序中是通過Point結構變量來設定此屬性的值,具體如下:

  

//設定窗體起初飄動的位置,位置為屏幕的坐標的(0,240)
private void Form1_Load ( object sender , System.EventArgs e )
{
Point p = new Point ( 0 , 240 ) ;
this.DesktopLocation = p ;
}

  (2). 如何實現窗體從左往右飄動:

  設定Timer1的Interval值為"10",就是當Timer1啟動後,每隔0.01秒觸發的事件是Timer1_Tick(),在這個事件中編寫給窗體左上角的橫坐標不斷加"1"的代碼,就可以了,具體如下:

  

private void timer1_Tick(object sender, System.EventArgs e)
{
{ //窗體的左上角橫坐標隨著timer1不斷加一
Point p = new Point ( this.DesktopLocation.X + 1 , this.DesktopLocation.Y ) ;
this.DesktopLocation = p ;
if ( p.X == 550 )
{
timer1.Enabled = false ;
timer2.Enabled = true ;
}
}

  (3). 如何實現窗體從右往左飄動:

  代碼設計和從左往右飄動差不多,主要的區別是減"1"而不是加"1"了,具體如下:

  

//當窗體左上角位置的橫坐標為-150時,timer2停止,timer1啟動
private void timer2_Tick(object sender, System.EventArgs e)
{ file://窗體的左上角橫坐標隨著timer2不斷減一
Point p = new Point ( this.DesktopLocation.X - 1 , this.DesktopLocation.Y ) ;
this.DesktopLocation = p ;
if ( p.X == - 150 )
{
timer1.Enabled = true ;
timer2.Enabled = false ;
}
}
三. 用Visual C#編寫窗體飄動程序的源代碼:

  通過上面的介紹,不難寫出窗體飄動的程序源代碼。如下:

  

using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
namespace floatingForm
{
public class Form1 : Form
{
private Timer timer1 ;
private Timer timer2 ;
private Label label1 ;
private Button button1 ;
private System.ComponentModel.IContainer components ;
public Form1 ( )
{
file://初始化窗體中的各個組件
InitializeComponent ( ) ;
}
file://清除在程序中使用過的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose( disposing ) ;
}
private void InitializeComponent ( )
{
this.components = new System.ComponentModel.Container ( ) ;
this.timer1 = new Timer ( this.components ) ;
this.timer2 = new Timer ( this.components ) ;
this.label1 = new Label ( ) ;
this.button1 = new Button ( ) ;
this.SuspendLayout ( ) ;
this.timer1.Enabled = true ;
this.timer1.Interval = 10 ;
this.timer1.Tick += new System.EventHandler ( this.timer1_Tick ) ;
this.timer2.Enabled = false ;
this.timer2.Interval = 10 ;
this.timer2.Tick += new System.EventHandler ( this.timer2_Tick ) ;
this.button1.Font = new Font ( "宋體" , 10 ) ;
this.button1.Location = new Point ( 1 , 8 ) ;
this.button1.Name = "button1" ;
this.button1.Size = new Size ( 80 , 25 ) ;
this.button1.TabIndex = 0 ;
this.button1.Text = "停止飄動" ;
this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
this.label1.Font = new Font ( "宋體" , 22F , FontStyle.Bold , GraphicsUnit.Point , ( ( System.Byte ) ( 0 ) ) ) ;
this.label1.Location = new Point ( 8 , 38 ) ;
this.label1.Name = "label1" ;
this.label1.Size = new Size ( 344 , 40 ) ;
this.label1.TabIndex = 1 ;
this.label1.Text = "用Visual C#做的飄動的窗體!" ;
this.AutoScaleBaseSize = new Size ( 5 , 13 ) ;
this.ClientSize = new Size ( 352 , 70 ) ;
this.Controls.Add (this.label1 ) ;
this.Controls.Add (this.button1 ) ;
this.Name = "Form1" ;
this.Text = "用Visual C#做的飄動的窗體!";
this.Load += new System.EventHandler ( this.Form1_Load ) ;
this.ResumeLayout ( false ) ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
file://設定窗體起初飄動的位置
private void Form1_Load ( object sender , System.EventArgs e )
{
Point p = new Point ( 0 , 240 ) ;
this.DesktopLocation = p ;
}
file://當窗體左上角位置的橫坐標為550時,timer1停止,timer2啟動
private void timer1_Tick(object sender, System.EventArgs e)
{
file://窗體的左上角橫坐標隨著timer1不斷加一
Point p = new Point ( this.DesktopLocation.X + 1 , this.DesktopLocation.Y ) ;
this.DesktopLocation = p ;
if ( p.X == 550 )
{
timer1.Enabled = false ;
timer2.Enabled = true ;
}
}
file://當窗體左上角位置的橫坐標為-150時,timer2停止,timer1啟動
private void timer2_Tick(object sender, System.EventArgs e)
{ file://窗體的左上角橫坐標隨著timer2不斷減一
Point p = new Point ( this.DesktopLocation.X - 1 , this.DesktopLocation.Y ) ;
this.DesktopLocation = p ;
if ( p.X == - 150 )
{
timer1.Enabled = true ;
timer2.Enabled = false ;
}
}
file://停止所有的timer
private void button1_Click(object sender, System.EventArgs e)
{
timer1.Stop ( ) ;
timer2.Stop ( ) ;
}
}
}

  四. 總結:

  恰到好處的使用Timer組件往往會有出其不意的效果。由於本文的主要目的是介紹Timer組件的使用方法,程序功能還不是十分強大,感興趣的讀者,可以試著按照下面的思路進行修改,看看是否可以讓窗體上下飄動,讓窗體不定規則的飄動。當然如果你更有興趣,也可以把窗體的邊框和最大化、最小化等按鈕給隱去,放一個好看的圖片充滿整個窗體,再讓他飄動起來,這樣效果就更令人驚訝了。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved