大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> 让窗体飘动起来--C#中Timer组件用法

让窗体飘动起来--C#中Timer组件用法

时间: 2021-07-31 作者:daque

   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://遏止一切的timerprivate void button1_click(object sender, system.eventargs e){timer1.stop ( ) ;timer2.stop ( ) ;}}}  四. 归纳:  恰如其分的运用timer组件常常会有出乎意料的功效。因为正文的重要手段是引见timer组件的运用本领,步调功效还不是格外宏大,感爱好的读者群,不妨试着依照底下的思绪举行窜改,看看能否不妨让窗体左右飘荡,让窗体大概准则的飘荡。固然即使你更有爱好,也不妨把窗体的边框和最大化、最小化等按钮给隐去,放一个场面的图片充溢所有窗体,再让他飘荡起来,如许功效就更令人诧异了。 

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.