思路是這樣的,在特定的時間段內(nèi),如果縮放的寬度的距離不在步驟之內(nèi),則逐漸逐漸增加寬度,以達(dá)到動畫的效果。

主要的代碼如下:
代碼
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
{
//動畫的變量參數(shù)
double FPS = 300.0;
long interval = (long)(Stopwatch.Frequency / FPS);
long ticks1 = 0;
long ticks2 = 0;
//傳進(jìn)來的新的窗體的大小
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;
//要調(diào)整的窗體的寬度是否在步長之內(nèi)
bool widthOff = IsWidthOff(frm.Width, size.Width, xStep);
//要調(diào)整的窗體的高度是否在步長之內(nèi)
bool heightOff = IsHeightOff(frm.Height, size.Height, yStep);
while (widthOff || heightOff)
{
//獲取當(dāng)前的時間戳
ticks2 = Stopwatch.GetTimestamp();
//允許調(diào)整大小僅在有足夠的時間來刷新窗體的時候
if (ticks2 >= ticks1 + interval)
{
//調(diào)整窗體的大小
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();
//保存當(dāng)前的時間戳
ticks1 = Stopwatch.GetTimestamp();
}
Thread.Sleep(1);
}
}
}
目標(biāo)寬度與當(dāng)前寬度是否在步長之內(nèi)
private static bool IsWidthOff(int currentWidth, int targetWidth, int step)
{
//目標(biāo)寬度與當(dāng)前寬度是否在步長之內(nèi),如果是,返回false
if (Math.Abs(currentWidth - targetWidth) <= Math.Abs(step)) return false;
return (step > 0 && currentWidth < targetWidth) ||
(step < 0 && currentWidth > targetWidth);
}