WinForm窗体缩放动画
WinForm自带的窗体大小发生改变的时候,当内存不够的时候,会出现界面停滞的现象,会出现许多的条条纹纹,给人很不好的感觉,这里提供一个WinForm窗体缩放时会有一个渐变的动画效果给大家。
代码
代码
思路是这样的,在特定的时间段内,如果缩放的宽度的距离不在步骤之内,则逐渐逐渐增加宽度,以达到动画的效果。
主要的代码如下:

private static void RunTransformation(object parameters)
{
Form frm = (Form)((object[])parameters)[0];
if (frm.InvokeRequired)
{
RunTransformationDelegate del = new RunTransformationDelegate(RunTransformation);
frm.Invoke(del, parameters);
}
else
{
//动画的变量参数
double FPS = 300.0;
long interval = (long)(Stopwatch.Frequency / FPS);
long ticks1 = 0;
long ticks2 = 0;
//传进来的新的窗体的大小
Size size = (Size)((object[])parameters)[1];
int xDiff = Math.Abs(frm.Width - size.Width);
int yDiff = Math.Abs(frm.Height - size.Height);
int step = 10;
int xDirection = frm.Width < size.Width ? 1 : -1;
int yDirection = frm.Height < size.Height ? 1 : -1;
int xStep = step * xDirection;
int yStep = step * yDirection;
//要调整的窗体的宽度是否在步长之内
bool widthOff = IsWidthOff(frm.Width, size.Width, xStep);
//要调整的窗体的高度是否在步长之内
bool heightOff = IsHeightOff(frm.Height, size.Height, yStep);
while (widthOff || heightOff)
{
//获取当前的时间戳
ticks2 = Stopwatch.GetTimestamp();
//允许调整大小仅在有足够的时间来刷新窗体的时候
if (ticks2 >= ticks1 + interval)
{
//调整窗体的大小
if (widthOff)
frm.Width += xStep;
if (heightOff)
frm.Height += yStep;
widthOff = IsWidthOff(frm.Width, size.Width, xStep);
heightOff = IsHeightOff(frm.Height, size.Height, yStep);
//允许窗体刷新
Application.DoEvents();
//保存当前的时间戳
ticks1 = Stopwatch.GetTimestamp();
}
Thread.Sleep(1);
}
}
}
{
Form frm = (Form)((object[])parameters)[0];
if (frm.InvokeRequired)
{
RunTransformationDelegate del = new RunTransformationDelegate(RunTransformation);
frm.Invoke(del, parameters);
}
else
{
//动画的变量参数
double FPS = 300.0;
long interval = (long)(Stopwatch.Frequency / FPS);
long ticks1 = 0;
long ticks2 = 0;
//传进来的新的窗体的大小
Size size = (Size)((object[])parameters)[1];
int xDiff = Math.Abs(frm.Width - size.Width);
int yDiff = Math.Abs(frm.Height - size.Height);
int step = 10;
int xDirection = frm.Width < size.Width ? 1 : -1;
int yDirection = frm.Height < size.Height ? 1 : -1;
int xStep = step * xDirection;
int yStep = step * yDirection;
//要调整的窗体的宽度是否在步长之内
bool widthOff = IsWidthOff(frm.Width, size.Width, xStep);
//要调整的窗体的高度是否在步长之内
bool heightOff = IsHeightOff(frm.Height, size.Height, yStep);
while (widthOff || heightOff)
{
//获取当前的时间戳
ticks2 = Stopwatch.GetTimestamp();
//允许调整大小仅在有足够的时间来刷新窗体的时候
if (ticks2 >= ticks1 + interval)
{
//调整窗体的大小
if (widthOff)
frm.Width += xStep;
if (heightOff)
frm.Height += yStep;
widthOff = IsWidthOff(frm.Width, size.Width, xStep);
heightOff = IsHeightOff(frm.Height, size.Height, yStep);
//允许窗体刷新
Application.DoEvents();
//保存当前的时间戳
ticks1 = Stopwatch.GetTimestamp();
}
Thread.Sleep(1);
}
}
}
目标宽度与当前宽度是否在步长之内

private static bool IsWidthOff(int currentWidth, int targetWidth, int step)
{
//目标宽度与当前宽度是否在步长之内,如果是,返回false
if (Math.Abs(currentWidth - targetWidth) <= Math.Abs(step)) return false;
return (step > 0 && currentWidth < targetWidth) ||
(step < 0 && currentWidth > targetWidth);
}
{
//目标宽度与当前宽度是否在步长之内,如果是,返回false
if (Math.Abs(currentWidth - targetWidth) <= Math.Abs(step)) return false;
return (step > 0 && currentWidth < targetWidth) ||
(step < 0 && currentWidth > targetWidth);
}
源代码下载 :
如果您喜欢我的文章,您可以通过支付宝对我进行捐助,您的支持是我最大的动力https://me.alipay.com/alexis
作者:Alexis
出处:http://www.cnblogs.com/alexis/
关于作者:专注于Windows Phone 7、Silverlight、Web前端(jQuery)。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过shuifengxuATgmail.com 联系我,非常感谢。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架