讓窗體飄動起來--C#中Timer組件用法
2024-07-21 02:19:20
供稿:網友
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組件的使用方法,程序功能還不是十分強大,感興趣的讀者,可以試著按照下面的思路進行修改,看看是否可以讓窗體上下飄動,讓窗體不定規則的飄動。當然如果你更有興趣,也可以把窗體的邊框和最大化、最小化等按鈕給隱去,放一個好看的圖片充滿整個窗體,再讓他飄動起來,這樣效果就更令人驚訝了。