• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

WinForm窗体缩放实现代码

时间:2015年04月02日 15:38:30    来源:魔法猪系统重装大师官网    人气:2925
 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);
}

}
}


目标宽度与当前宽度是否在步长之内

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);
}
WinForm,窗体,缩放,实现,代码,WinForm,自带
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部