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

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

編輯:關於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