WinForm 窗体缩放动画效果
2020-12-13 04:02
标签:窗体缩放 winform c# WinForm 窗体缩放动画效果,搜素材,soscw.com WinForm 窗体缩放动画效果 标签:窗体缩放 winform c# 原文地址:http://blog.csdn.net/xiaoweiserver/article/details/37399383
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
namespace AnimatedTransform
{
static class FormTransform
{
public static void TransformSize(Form frm, int newWidth, int newHeight)
{
TransformSize(frm, new Size(newWidth, newHeight));
}
public static void TransformSize(Form frm, Size newSize)
{
ParameterizedThreadStart threadStart = new ParameterizedThreadStart(RunTransformation);
Thread transformThread = new Thread(threadStart);
transformThread.Start(new object[] { frm, newSize });
}
private delegate void RunTransformationDelegate(object paramaters);
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 = 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) 0 && currentWidth targetWidth);
}
private static bool IsHeightOff(int currentHeight, int targetHeight, int step)
{
//目标高度与当前高度是否在步长之内,如果是,返回false
if (Math.Abs(currentHeight - targetHeight) 0 && currentHeight targetHeight);
}
}
}
调用方法:
Random random = new Random();
int width = random.Next(100, 500);//随机数产生窗体的宽
int height = random.Next(50, 800);//随机数产生窗体的高
FormTransform.TransformSize(this, width, height);