飘动窗体
前言
C#实践开发_Winform 系列第三篇:飘动窗体,掌握l定时器控件使用。一、结果呈现
1. 界面设计
窗体界面设计:四个Timer定时器控件(设定Interval值为100,即每隔0.1秒触发事件timer_Tick()),四个label标签(显示功能)。2. 运行结果呈现
二、源码
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