WinForm自带的窗体大小发生改变的时候,当内存不够的时候,会出现界面停滞的现象,会出现许多的条条纹纹,给人很不好的感觉,这里提供一个WinForm窗体缩放时会有一个渐变的动画效果给大家。

思路是这样的,在特定的时间段内,如果缩放的宽度的距离不在步骤之内,则逐渐逐渐增加宽度,以达到动画的效果。

主要的代码如下:

 
  
  1. private static void RunTransformation(object parameters)  
  2.         {  
  3.             Form frm = (Form)((object[])parameters)[0];  
  4.             if (frm.InvokeRequired)  
  5.             {  
  6.                 RunTransformationDelegate del = new RunTransformationDelegate(RunTransformation);  
  7.                 frm.Invoke(del, parameters);  
  8.             }  
  9.             else  
  10.             {  
  11.                 //动画的变量参数  
  12.                 double FPS = 300.0;  
  13.                 long interval = (long)(Stopwatch.Frequency / FPS);  
  14.                 long ticks1 = 0;  
  15.                 long ticks2 = 0;  
  16.  
  17.                 //传进来的新的窗体的大小  
  18.                 Size size = (Size)((object[])parameters)[1];  
  19.  
  20.                 int xDiff = Math.Abs(frm.Width - size.Width);  
  21.                 int yDiff = Math.Abs(frm.Height - size.Height);  
  22.  
  23.                 int step = 10;  
  24.  
  25.                 int xDirection = frm.Width < size.Width ? 1 : -1;  
  26.                 int yDirection = frm.Height < size.Height ? 1 : -1;  
  27.  
  28.                 int xStep = step * xDirection;  
  29.                 int yStep = step * yDirection;  
  30.  
  31.                 //要调整的窗体的宽度是否在步长之内  
  32.                 bool widthOff = IsWidthOff(frm.Width, size.Width, xStep);  
  33.                 //要调整的窗体的高度是否在步长之内  
  34.                 bool heightOff = IsHeightOff(frm.Height, size.Height, yStep);  
  35.  
  36.  
  37.                 while (widthOff || heightOff)  
  38.                 {  
  39.                     //获取当前的时间戳  
  40.                     ticks2 = Stopwatch.GetTimestamp();  
  41.                     //允许调整大小仅在有足够的时间来刷新窗体的时候  
  42.                     if (ticks2 >= ticks1 + interval)   
  43.                     {  
  44.                         //调整窗体的大小  
  45.                         if (widthOff)  
  46.                             frm.Width += xStep;  
  47.  
  48.                         if (heightOff)  
  49.                             frm.Height += yStep;  
  50.  
  51.                         widthOff = IsWidthOff(frm.Width, size.Width, xStep);  
  52.                         heightOff = IsHeightOff(frm.Height, size.Height, yStep);  
  53.  
  54.                         //允许窗体刷新  
  55.                         Application.DoEvents();  
  56.  
  57.                         //保存当前的时间戳  
  58.                         ticks1 = Stopwatch.GetTimestamp();  
  59.                     }  
  60.  
  61.                     Thread.Sleep(1);  
  62.                 }  
  63.  
  64.             }  
  65.         } 

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

 
  
  1. private static bool IsWidthOff(int currentWidth, int targetWidth, int step)  
  2.         {  
  3.             //目标宽度与当前宽度是否在步长之内,如果是,返回false  
  4.             if (Math.Abs(currentWidth - targetWidth) <= Math.Abs(step)) return false;  
  5.  
  6.             return (step > 0 && currentWidth < targetWidth) ||  
  7.                    (step < 0 && currentWidth > targetWidth);   
  8.         } 

源代码下载 :