使用Timer组件制作左右飘动的窗体

 

Frm_Main.cs

View Code
复制代码
 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9 namespace MoveForm
10 {
11 public partial class Frm_Main : Form
12 {
13 public Frm_Main()
14 {
15 InitializeComponent();
16 }
17
18 private void timer1_Tick(object sender, EventArgs e)
19 {
20 Rectangle rect = Screen.GetWorkingArea(this);//获取屏幕大小
21 if (this.Left != (rect.Width - this.Width))
22 {
23 this.Left++;//窗体向右移动
24 this.Top += 1;//窗体向下移动
25 }
26 else
27 {
28 timer1.Enabled = false;//停用Timer组件
29 timer2.Enabled = true;//启用Timer组件
30 }
31 }
32 private void timer2_Tick(object sender, EventArgs e)
33 {
34 Rectangle rect = Screen.GetWorkingArea(this);//获取屏幕大小
35 if (this.Left == 0)
36 {
37 timer2.Enabled = false;//停用Timer组件
38 timer1.Enabled = true;//启用Timer组件
39 }
40 else
41 {
42 this.Left--;//窗体向左移动
43 this.Top -= 1;//窗体向上移动
44 }
45 }
46 }
47 }
复制代码

Frm_Main.designer.cs

View Code
 1 namespace MoveForm
2 {
3 partial class Frm_Main
4 {
5 /// <summary>
6 /// 必需的设计器变量。
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 #region Windows 窗体设计器生成的代码
24
25 /// <summary>
26 /// 设计器支持所需的方法 - 不要
27 /// 使用代码编辑器修改此方法的内容。
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.components = new System.ComponentModel.Container();
32 this.timer1 = new System.Windows.Forms.Timer(this.components);
33 this.timer2 = new System.Windows.Forms.Timer(this.components);
34 this.SuspendLayout();
35 //
36 // timer1
37 //
38 this.timer1.Enabled = true;
39 this.timer1.Interval = 10;
40 this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
41 //
42 // timer2
43 //
44 this.timer2.Interval = 10;
45 this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
46 //
47 // Frm_Main
48 //
49 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
50 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
51 this.BackgroundImage = global::MoveForm.Properties.Resources.星星图;
52 this.ClientSize = new System.Drawing.Size(320, 91);
53 this.Name = "Frm_Main";
54 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
55 this.Text = "使用Timer组件制作左右飘动的窗体";
56 this.ResumeLayout(false);
57
58 }
59
60 #endregion
61
62 private System.Windows.Forms.Timer timer1;
63 private System.Windows.Forms.Timer timer2;
64 }
65 }




作者: 墨明棋妙
出处: http://www.cnblogs.com/ynbt/
关于作者:专注于.Net、WCF和移动互联网开发。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过ynbt_wang@163.com联系我,非常感谢。 。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值