| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1055 人关注过本帖
标题:C#编程技巧集之--让窗体飘动起来--C#中Timer组件用法[转]
只看楼主 加入收藏
冰封谷主
Rank: 4
等 级:贵宾
威 望:10
帖 子:226
专家分:20
注 册:2005-1-7
收藏
 问题点数:0 回复次数:5 
C#编程技巧集之--让窗体飘动起来--C#中Timer组件用法[转]
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 ;
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 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 ( )
{
= new ( ) ;
this.timer1 = new Timer ( ) ;
this.timer2 = new Timer ( ) ;
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组件的使用方法,程序功能还不是十分强大,感兴趣的读者,可以试着按照下面的思路进行修改,看看是否可以让窗体上下飘动,让窗体不定规则的飘动。当然如果你更有兴趣,也可以把窗体的边框和最大化、最小化等按钮给隐去,放一个好看的图片充满整个窗体,再让他飘动起来,这样效果就更令人惊讶了。
搜索更多相关主题的帖子: Timer 组件 窗体 用法 技巧 
2005-03-31 22:02
sixsir
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-5-30
收藏
得分:0 
学习
2007-06-06 09:12
jasonxie
Rank: 1
等 级:新手上路
威 望:2
帖 子:225
专家分:0
注 册:2007-3-19
收藏
得分:0 
我顶,我又学到了一样东西也,呵呵,窗口背景换成了一个我喜欢的明星,嘻嘻。好看

你是我心内的一首歌,不要只是个过客;在我生命留下一首歌,无论结局会如何.
2007-06-06 11:01
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
得分:0 
很经典,不过你试过用获的随机数的那个函数,我忘了来做任意飘动的窗体没有?

真的想象风一样去流浪!
2007-07-04 11:23
随风逐流
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:passerby
等 级:版主
威 望:8
帖 子:4054
专家分:271
注 册:2007-6-13
收藏
得分:0 

涨知识了 谢啦


[url=http://www./html/6/6694/]极道金丹[/url][url=http://www./html/2/2849/]九阴九阳[/url][url=http://www./html/2/2596/]凡人修仙传[/url]
2007-07-04 12:07
yarak_ma
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2007-5-15
收藏
得分:0 

牛,原来可以这样玩!

2007-07-04 14:59
快速回复:C#编程技巧集之--让窗体飘动起来--C#中Timer组件用法[转]
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.067108 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved