[转]C# 在窗口右下角弹出广告的代码,渐变显示与自动消失

[ 代码如下 ]:

在窗体里需要添加一个timer控件,在此命名为timer1,并在timer控制的Tick事件中写入timer1_Tick的相关代码

 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 namespace MySoft
9 {
10 public partial class Msg_form : Form
11 {
12 private bool display = true; //定义窗体是显示(true)还是消失(false)
13 private int S_width = 0; //定义屏幕宽度
14 private int S_height = 0; //定义屏幕高度
15 private double count = 0; //定义一个用于延时的计数器
16 public Msg_form()
17 {
18 InitializeComponent();
19
20 //指定窗体显示位置
21 Rectangle ScreenArea = System.Windows.Forms.Screen.GetWorkingArea(this);
22 S_width = ScreenArea.Width; //获取屏幕宽度
23 S_height = ScreenArea.Height; //获取屏幕高度
24 //控制窗体渐变出现效果
25 this.timer1.Enabled = true;//获取当前运行时间
26 this.Opacity = 0.0;//获取当前窗体的透明度级别;
27 }
28 //窗体渐变出现效果
29 private void timer1_Tick(object sender, EventArgs e)
30 {
31 if (display)
32 {
33 if (this.Opacity < 1)
34 {
35 this.Opacity = this.Opacity + 0.1;//窗体以0.1的速度渐变显示
36 }
37 else
38 {
39 count = count + 0.1;
40 if ( count >= 5 ) display = false; //当完全显示后,延时5秒自动渐变消失
41 }
42 }
43 else
44 {
45 if (this.Opacity > 0)
46 {
47 this.Opacity = this.Opacity - 0.1;//窗体以0.1的速度渐变消失
48 }
49 else
50 {
51 this.timer1.Enabled = false;//时间为false
52 Close();//关闭窗体
53 }
54 }
55 if (this.Opacity <= 1)
56 {
57 //指定窗体显示在右下角
58 this.Location = new System.Drawing.Point(S_width - 300, S_height - Convert.ToInt32(160 * this.Opacity));
59 }
60 }
61 }
62 }