西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)其它知識 → WinForm窗體縮放實現(xiàn)代碼

WinForm窗體縮放實現(xiàn)代碼

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:本站整理時間:2011/1/19 1:05:34字體大。A-A+

作者:佚名點擊:304次評論:0次標簽: WinForm

  • 類型:編程輔助大小:3.3M語言:中文 評分:4.4
  • 標簽:
立即下載
 WinForm自帶的窗體大小發(fā)生改變的時候,當內(nèi)存不夠的時候,會出現(xiàn)界面停滯的現(xiàn)象,會出現(xiàn)許多的條條紋紋,給人很不好的感覺,這里提供一個WinForm窗體縮放時會有一個漸變的動畫效果給大家。
思路是這樣的,在特定的時間段內(nèi),如果縮放的寬度的距離不在步驟之內(nèi),則逐漸逐漸增加寬度,以達到動畫的效果。


主要的代碼如下:

代碼
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;

//傳進來的新的窗體的大小
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)
{
//獲取當前的時間戳
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();

//保存當前的時間戳
ticks1 = Stopwatch.GetTimestamp();
}

Thread.Sleep(1);
}

}
}


目標寬度與當前寬度是否在步長之內(nèi)

private static bool IsWidthOff(int currentWidth, int targetWidth, int step)
{
//目標寬度與當前寬度是否在步長之內(nèi),如果是,返回false
if (Math.Abs(currentWidth - targetWidth) <= Math.Abs(step)) return false;

return (step > 0 && currentWidth < targetWidth) ||
(step < 0 && currentWidth > targetWidth);
}

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)