您的位置:首页 > 编程语言 > C#

C# 窗体缓缓上升与下降

2013-10-11 09:25 169 查看
using System;
using System.Windows.Forms;

namespace Calc {
public partial class FloatForm : Form {
public FloatForm() {
InitializeComponent();
}

private void FloatForm_Load(object sender, EventArgs e) {
this.Width = 400;
this.Height = 300;
this.TopMost = true;
this.Opacity = 0.5;

//初始化窗口到屏幕右下角
this.Top = Screen.PrimaryScreen.WorkingArea.Top + Screen.PrimaryScreen.WorkingArea.Height;
this.Left = Screen.PrimaryScreen.WorkingArea.Left + Screen.PrimaryScreen.WorkingArea.Width - this.Width;
}

private void FloatForm_Shown(object sender, EventArgs e) {
this.timer1.Interval = 10;
this.timer1.Tick += new EventHandler(timer1_Tick);
this.timer1.Start();
}

void timer1_Tick(object sender, EventArgs e) {
if (this.Top + this.Height/2> Screen.PrimaryScreen.WorkingArea.Top + Screen.PrimaryScreen.WorkingArea.Height/2) {
this.Top -= 2;  //每次移动2个像素,可修改
this.Opacity += 0.002;
Application.DoEvents();
}
else {
this.timer1.Stop();
this.Opacity = 1;
}
}

private void FloatForm_FormClosing(object sender, FormClosingEventArgs e) {

if (this.Tag != null && (int)this.Tag == 100) {
e.Cancel = false;
return;
}

e.Cancel = true;
if (this.timer1.Enabled) return;  //如果是在关闭进程中,不处理

this.timer1.Tick -= timer1_Tick;
this.timer1.Tick += new EventHandler(timer1_Tick2);
this.timer1.Start();
}

void timer1_Tick2(object sender, EventArgs e) {
if (this.Top < Screen.PrimaryScreen.WorkingArea.Top + Screen.PrimaryScreen.WorkingArea.Height) {
this.Top += 2;  //每次移动2个像素,可修改
this.Opacity -= 0.002;
Application.DoEvents();
}
else {
this.timer1.Stop();
this.Tag = 100;
this.Close();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: