天天看點

C#實踐開發_Winform 系列三:飄動窗體飄動窗體前言一、結果呈現二、源碼三、總結

飄動窗體

文章目錄

  • 飄動窗體
  • 前言
  • 一、結果呈現
    • 1. 界面設計
    • 2. 運作結果呈現
  • 二、源碼
    • 1.Form.cs
    • 2.Form.Designer.cs
  • 三、總結

前言

C#實踐開發_Winform 系列第三篇:飄動窗體,掌握l定時器控件使用。

一、結果呈現

1. 界面設計

窗體界面設計:四個Timer定時器控件(設定Interval值為100,即每隔0.1秒觸發事件timer_Tick()),四個label标簽(顯示功能)。

C#實踐開發_Winform 系列三:飄動窗體飄動窗體前言一、結果呈現二、源碼三、總結

2. 運作結果呈現

C#實踐開發_Winform 系列三:飄動窗體飄動窗體前言一、結果呈現二、源碼三、總結

二、源碼

1.Form.cs

代碼如下(示例):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test_4_2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Point p = new Point(0, 240);	//設定窗體初始位置
            this.DesktopLocation = p;
            timer1.Enabled = true;
        }

        private void timer1_Trick(object sender, System.EventArgs e)
        {
            Point p = new Point(this.DesktopLocation.X + 1, this.DesktopLocation.Y + 1);
            this.DesktopLocation = p;
            if (p.X == 470 && p.Y == 710)
            {
                timer1.Enabled = false;
                timer2.Enabled = true;
                timer3.Enabled = false;
                timer4.Enabled = false;

                label1.Visible = false;
                label2.Visible = true;
                label3.Visible = false;
                label4.Visible = false;
            }
        }

        private void timer2_Trick(object sender, System.EventArgs e)
        {
            Point p = new Point(this.DesktopLocation.X + 1, this.DesktopLocation.Y - 1);
            this.DesktopLocation = p;
            if (p.X == 940 && p.Y == 240)
            {
                timer1.Enabled = false;
                timer2.Enabled = false;
                timer3.Enabled = true;
                timer4.Enabled = false;

                label1.Visible = false;
                label2.Visible = false;
                label3.Visible = true;
                label4.Visible = false;
            }
        }
        private void timer3_Trick(object sender, System.EventArgs e)
        {
            Point p = new Point(this.DesktopLocation.X -1, this.DesktopLocation.Y - 1);
            this.DesktopLocation = p;
            if (p.X == 470 && p.Y == -230)
            {
                timer1.Enabled = false;
                timer2.Enabled = false;
                timer3.Enabled = false;
                timer4.Enabled = true;

                label1.Visible = false;
                label2.Visible = false;
                label3.Visible = false;
                label4.Visible = true;
            }
        }

        private void timer4_Trick(object sender, System.EventArgs e)
        {
            Point p = new Point(this.DesktopLocation.X - 1, this.DesktopLocation.Y + 1);
            this.DesktopLocation = p;
            if (p.X == 0 && p.Y == 240)
            {
                timer1.Enabled = true;
                timer2.Enabled = false;
                timer3.Enabled = false;
                timer4.Enabled = false;

                label1.Visible = true;
                label2.Visible = false;
                label3.Visible = false;
                label4.Visible = false;
            }
        }
    }
}

           

2.Form.Designer.cs

代碼如下(示例):

namespace test_4_2
{
    partial class Form2
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.timer2 = new System.Windows.Forms.Timer(this.components);
            this.timer3 = new System.Windows.Forms.Timer(this.components);
            this.timer4 = new System.Windows.Forms.Timer(this.components);
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.Interval = 1;
            this.timer1.Tick += new System.EventHandler(this.timer1_Trick);
            // 
            // timer2
            // 
            this.timer2.Interval = 1;
            this.timer2.Tick += new System.EventHandler(this.timer2_Trick);
            // 
            // timer3
            // 
            this.timer3.Interval = 1;
            this.timer3.Tick += new System.EventHandler(this.timer3_Trick);
            // 
            // timer4
            // 
            this.timer4.Interval = 1;
            this.timer4.Tick += new System.EventHandler(this.timer4_Trick);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("宋體", 25.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            this.label1.Location = new System.Drawing.Point(141, 154);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(191, 43);
            this.label1.TabIndex = 0;
            this.label1.Text = "飄動窗體";
            this.label1.Click += new System.EventHandler(this.timer1_Trick);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("宋體", 25.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label2.ForeColor = System.Drawing.Color.Red;
            this.label2.Location = new System.Drawing.Point(260, 230);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(191, 43);
            this.label2.TabIndex = 1;
            this.label2.Text = "飄動窗體";
            this.label2.Visible = false;
            this.label2.Click += new System.EventHandler(this.timer2_Trick);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("宋體", 25.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label3.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
            this.label3.Location = new System.Drawing.Point(405, 154);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(191, 43);
            this.label3.TabIndex = 2;
            this.label3.Text = "飄動窗體";
            this.label3.Visible = false;
            this.label3.Click += new System.EventHandler(this.timer3_Trick);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Font = new System.Drawing.Font("宋體", 25.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label4.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
            this.label4.Location = new System.Drawing.Point(260, 81);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(191, 43);
            this.label4.TabIndex = 3;
            this.label4.Text = "飄動窗體";
            this.label4.Visible = false;
            this.label4.Click += new System.EventHandler(this.timer4_Trick);
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(751, 413);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.Load += new System.EventHandler(this.Form2_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.Timer timer2;
        private System.Windows.Forms.Timer timer3;
        private System.Windows.Forms.Timer timer4;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
    }
}

           

三、總結

設計思路: 首先加載窗體的時候給窗體設定一個顯示的初始位置,再利用Timer控件,實作窗體飄動效果。

項目源碼位址: (Form2)

連結:https://pan.baidu.com/s/1SEh3EF7Vf1ZFpAzKWZYdmA

提取碼:muda

示例參考來源: Visual C#.net基礎與應用教程(夏靈活 羅菁 主編)