C# 中可以设置窗体的Opacity属性来设置窗体的透明程度,默认值为1.0 就是不透明
最小可以设置到0.01, 再小就看不到东西了。
偶然自己做了一下,现今拿出来给有兴趣的人参考下。。。
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
//淡入淡出的状态
private int state;
private double inspeed;
private double outspeed;
public Form2()
{
InitializeComponent();
timer1.Enabled = false;
inspeed = 10;
outspeed = 10;
}
/// <summary>
/// 淡入的速度属性0-100
/// </summary>
public double InSpeed
{
get
{
return inspeed;
}
set
{
inspeed = value;
}
}
/// <summary>
/// 淡出的速度属性0-100
/// </summary>
public double OutSpeed
{
get
{
return outspeed;
}
set
{
outspeed = value;
}
}
private void Form2_Load(object sender, EventArgs e)
{
state = 0;
this.timer1.Enabled = true;
this.Opacity = 0;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (state >= 0)
{
this.Opacity += inspeed / 100;
if (this.Opacity == 1)
{
timer1.Enabled = false;
}
}
else if (state < 0)
{
this.Opacity -= outspeed / 100;
if (this.Opacity == 0)
{
this.Close();
timer1.Enabled = false;
}
}
}
/// <summary>
/// 窗体关闭
/// </summary>
public void FormClose()
{
state = -1;
timer1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
FormClose();
}
}
}