麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C# > 正文

C#中Timer控件的運用實例

2023-05-10 18:49:24
字體:
來源:轉載
供稿:網友

本文使用Timer控件制作了一個窗體飄動的程序。

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

定義兩個Timer控件,一個命名為timer1,另外一個命名為timer2。  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

      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
{
      Point p = new Point ( this.DesktopLocation.X - 1 , this.DesktopLocation.Y ) ; 
      this.DesktopLocation = p
      if ( p.X == - 150 ) 
      { 
           timer1.Enabled = true
           timer2.Enabled = false
      } 

全部源代碼如下:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication1
{
    ///<summary>
    /// Form1 的摘要說明。
    /// 作者: Shadow
    /// 2006-8-3
    ///</summary>

    public class Form1 : System.Windows.Forms.Form
    {
        private Timer timer1 ;
        private Timer timer2 ;
        private Label label1 ;
        private Button button1 ;
        private System.Windows.Forms.Button button2;
        private System.ComponentModel.IContainer components ;
        private string remarkStatus = "";

        public Form1 ( )
        {
            InitializeComponent ( ) ;
        }

        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();
            System.Resources.ResourceManager resources =
                new System.Resources.ResourceManager(typeof(Form1));
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.timer2 = new System.Windows.Forms.Timer(this.components);
            this.label1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();

            //
            // timer1
            //

            this.timer1.Enabled = true;
            this.timer1.Interval = 10;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

            //
            // timer2
            //

            this.timer2.Interval = 10;
            this.timer2.Tick += new System.EventHandler(this.timer2_Tick);

            //
            // label1
            //

            this.label1.BackColor = System.Drawing.Color.Transparent;
            this.label1.Font = new System.Drawing.Font("華文行楷", 21.75F,
                System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
                ((System.Byte)(134)));
            this.label1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)),
                ((System.Byte)(0)), ((System.Byte)(192)));
            this.label1.Location = new System.Drawing.Point(16, 88);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(438, 39);
            this.label1.TabIndex = 1;
            this.label1.Text = "窗 體 動 起 來 嘍!";
            this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;

            //
            // button1
            //

            this.button1.BackColor = System.Drawing.Color.FromArgb(
                ((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
            this.button1.Font = new System.Drawing.Font("宋體", 10F);
            this.button1.ForeColor = System.Drawing.Color.Blue;
            this.button1.Location = new System.Drawing.Point(96, 24);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(96, 27);
            this.button1.TabIndex = 0;
            this.button1.Text = "停止飄動";
            this.button1.Click += new System.EventHandler(this.button1_Click);

            //
            // button2
            //

            this.button2.BackColor = System.Drawing.Color.FromArgb(
                ((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
            this.button2.Font = new System.Drawing.Font("宋體", 10F);
            this.button2.ForeColor = System.Drawing.Color.Blue;
            this.button2.Location = new System.Drawing.Point(368, 24);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(96, 27);
            this.button2.TabIndex = 2;
            this.button2.Text = "開始飄動";
            this.button2.Click += new System.EventHandler(this.button2_Click);

            //
            // Form1
            //

            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.BackColor = System.Drawing.Color.Silver;
            this.BackgroundImage = (
                (System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.ClientSize = new System.Drawing.Size(488, 165);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "Form1";
            this.Text = " Shadow用C#做的飄動的窗體!";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
        }

        static void Main ( )
        {
            Application.Run ( new Form1 ( ) ) ;
        }

        private void Form1_Load ( object sender , System.EventArgs e )
        {
            Point p = new Point ( 0 , 240 ) ;
            this.DesktopLocation = p ;
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            Point p = new Point ( this.DesktopLocation.X + 1 , this.DesktopLocation.Y ) ;
            this.DesktopLocation = p ;
            if ( p.X == 550 )
            {
                timer1.Enabled = false ;
                timer2.Enabled = true ;
            }
        }

        private void timer2_Tick(object sender, System.EventArgs e)
        {
            Point p = new Point ( this.DesktopLocation.X - 1 , this.DesktopLocation.Y ) ;
            this.DesktopLocation = p ;
            if ( p.X == - 150 )
            {
                timer1.Enabled = true ;
                timer2.Enabled = false ;
            }
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            if(timer1.Enabled == true)
                remarkStatus = "timer1";
            else if(timer2.Enabled == true)
                remarkStatus = "timer2";

            timer1.Stop( );
            timer2.Stop( );
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            if(remarkStatus == "timer1")
            {
                timer1.Start( );
                timer2.Stop( );
            }
            else
            {
                timer1.Stop( );
                timer2.Start( );
            }
        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一级免费观看 | 精品在线观看一区 | 国产成人自拍av | 亚洲乱搞 | 99在线啪 | 一级成人在线 | 久久久久久久免费精品 | 成人毛片网站 | 九九热久久免费视频 | freexxx69性欧美hd | 国产免费区 | 永久免费不卡在线观看黄网站 | 国产精品99久久久久久久女警 | 91精品国产九九九久久久亚洲 | 成年性羞羞视频免费观看无限 | 久久精品视频69 | 国产二区三区视频 | 国产精品av久久久久久网址 | 成人综合免费视频 | 一区二区三区欧美日韩 | 九九综合九九 | 99亚洲视频 | 一级片久久免费 | 成年人小视频在线观看 | 69性欧美高清影院 | 国产一区二区不卡视频 | 久久国产秒 | 欧美激情精品久久久久久黑人 | 久草在线综合 | 亚洲精品7777| 2021免费日韩视频网 | 精品一区二区三区在线观看国产 | 青青国产在线视频 | 一区二区久久电影 | 久久99精品久久久久久园产越南 | 色播视频在线播放 | 黄色影院在线 | 亚洲一级成人 | 91一区二区在线观看 | 久久久aa | 3级毛片|