天天看点

WinForm窗体缩放动画

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

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

<a target="_blank" href="http://blog.51cto.com/attachment/201105/160103738.png"></a>

主要的代码如下:

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 &lt; size.Width ? 1 : -1;  

                int yDirection = frm.Height &lt; 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 &gt;= 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) &lt;= Math.Abs(step)) return false;  

            return (step &gt; 0 &amp;&amp; currentWidth &lt; targetWidth) ||  

                   (step &lt; 0 &amp;&amp; currentWidth &gt; targetWidth);   

源代码下载 :

<a target="_blank" href="http://blog.51cto.com/attachment/201105/2621421_1306396994.rar"></a>

<a href="http://down.51cto.com/data/2358230" target="_blank">附件:http://down.51cto.com/data/2358230</a>

    本文转自xshf12345 51CTO博客,原文链接:http://blog.51cto.com/alexis/574746,如需转载请自行联系原作者

继续阅读