天天看點

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,如需轉載請自行聯系原作者

繼續閱讀