WinForm窗体缩放动画
生活随笔
收集整理的這篇文章主要介紹了
WinForm窗体缩放动画
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
WinForm自帶的窗體大小發生改變的時候,當內存不夠的時候,會出現界面停滯的現象,會出現許多的條條紋紋,給人很不好的感覺,這里提供一個WinForm窗體縮放時會有一個漸變的動畫效果給大家。
????????{
????????????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);?
????????}
思路是這樣的,在特定的時間段內,如果縮放的寬度的距離不在步驟之內,則逐漸逐漸增加寬度,以達到動畫的效果。
?
主要的代碼如下:
代碼 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);?
????????}
?源代碼下載 :
?
轉載于:https://www.cnblogs.com/alexis/archive/2011/01/18/1938695.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的WinForm窗体缩放动画的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: adblock拦截iframe
- 下一篇: C#自动加载DLL