C#中常用窗口特效的实现代码
作者:Csharp 小记
这篇文章主要为大家详细介绍了C#中三个常用的窗口特效的实现,分别是淡入淡出、变大变小、缓升缓降,感兴趣的小伙伴可以跟随小编一起学习一下
前言
说到特效,就得谈"动"这个字,在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();
}
}
}实现效果

到此这篇关于C#中常用窗口特效的实现代码的文章就介绍到这了,更多相关C#窗口特效内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
