前言
说到特效,就得谈"动"这个字,在Winform中想要动起来,大部分可以靠Timer来实现(你要说我靠循环也能实现一样的效果,我也无话可说),但基本上也就限制在一些比较基础的效果了。不过,也没关系,谁让这是Winform呢?
下面描述了三种窗口的效果。分别是淡入淡出、变大变小、缓升缓降。主要通过结合Timer与透明度、大小、以及位置等来实现。
开发环境:.NET Framework版本:4.8
开发工具:Visual Studio 2022
实现步骤
淡入淡出
public Form1()
{
InitializeComponent();
Opacity = 0;
timer1.Interval = 10;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (isShow)
{
if (Height < height)
{
Height += 1;
}
else
{
timer1.Stop();
}
}
else
{
if (ClientSize.Height > 0)
{
Height -= 1;
}
else
{
timer1.Stop();
Close();
}
}
}
变大变小
public Form2()
{
InitializeComponent();
height = Height;
Size = new Size(Width, 0);
timer1.Interval = 10;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (isShow)
{
if (Height < height)
{
Height += 1;
}
else
{
timer1.Stop();
}
}
else
{
if (ClientSize.Height > 0)
{
Height -= 1;
}
else
{
timer1.Stop();
Close();
}
}
}
缓升缓降
public Form3()
{
InitializeComponent();
timer1.Interval = 10;
}
private void Form3_Load(object sender, EventArgs e)
{
Location = new Point(screenRect.Width - Width, screenRect.Height);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (isShow)
{
if (Location.Y > screenRect.Height-Height)
{
Location = new Point(Location.X, Location.Y - 1);
}
else
{
timer1.Stop();
}
}
else
{
if (Location.Y < screenRect.Height )
{
Location = new Point(Location.X, Location.Y + 1);
}
else
{
timer1.Stop();
Close();
}
}
}
实现效果
☛☛☛点击此处下载源码☚☚☚