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 < 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 bool IsHeightOff(int currentHeight, int targetHeight, int step)
{
//目标高度与当前高度是否在步长之内,假设是,返回false
if (Math.Abs(currentHeight - targetHeight) <= Math.Abs(step)) return false; return (step > 0 && currentHeight < targetHeight) ||
(step < 0 && currentHeight > targetHeight);
}
}
}
调用方法:
Random random = new Random();
int width = random.Next(100, 500);//随机数产生窗口的宽
int height = random.Next(50, 800);//随机数产生窗口的高
FormTransform.TransformSize(this, width, height);

WinForm 窗口缩放动画效果的更多相关文章

  1. UILabel的缩放动画效果

    UILabel的缩放动画效果 效果图 源码 https://github.com/YouXianMing/Animations // // ScaleLabel.h // Animations // ...

  2. AndroidUI 视图动画-缩放动画效果 (ScaleAnimation)

    放动画效果,可以使用ScaleAnimation: <Button android:id="@+id/btnScale2" android:layout_width=&quo ...

  3. Android动画效果之Tween Animation(补间动画)

    前言: 最近公司项目下个版本迭代里面设计了很多动画效果,在以往的项目中开发中也会经常用到动画,所以在公司下个版本迭代开始之前,抽空总结一下Android动画.今天主要总结Tween Animation ...

  4. Android 动画效果 及 自定义动画

    1. View动画-透明动画效果2. View动画-旋转动画效果3. View动画-移动动画效果4. View动画-缩放动画效果5. View动画-动画效果混合6. View动画-动画效果侦听7. 自 ...

  5. AndroidUI 布局动画-为列表添加布局动画效果

    新建一个Android project ,使MainActivity 继承自 ListActivity: public class MainActivity extends ListActivity ...

  6. AndroidUI 视图动画-混合动画效果 (AnimationSet)/动画效果监听

    在前面介绍了几种动画效果:透明动画效果(AlphsAnimation).移动动画效果(TranslateAnimation).旋转动画效果(RotateAnimation).缩放动画效果(ScaleA ...

  7. Swift - UITableView展开缩放动画

    Swift - UITableView展开缩放动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // HeaderViewTapA ...

  8. POP缩放动画

    POP缩放动画 效果 源码 https://github.com/YouXianMing/Animations // // SpringScaleViewController.m // Animati ...

  9. UITableView的headerView展开缩放动画

    UITableView的headerView展开缩放动画 效果 源码 https://github.com/YouXianMing/Animations // // HeaderViewTapAnim ...

随机推荐

  1. java自定义类

    引用数据类型(类) 引用数据类型分类 提到引用数据类型(类),其实我们对它并不陌生,之前使用过的Scanner类.Random类. 我们可以把类的类型为两种: 第一种,Java为我们提供好的类,如Sc ...

  2. Docker应用系列(二)| 构建Zookeeper集群

    本示例基于Centos 7,在阿里云的三台机器上部署zookeeper集群,假设目前使用的账号为release,拥有sudo权限. 由于Docker官方镜像下载较慢,可以开启阿里云的Docker镜像下 ...

  3. BASE64Decoder BASE64Encoder jar包问题

    操作 对项目右击--->build path--->configure build path---> 选中默认jre OK,操作完毕, import sun.misc.BASE64D ...

  4. Python实现图片转文字并翻译至剪切板

    一.环境搭建: 1.PySimpleGUI: pip3 install pysimplegui 2.pytesseract需要有tesseract环境才行: 1. 先搭建tesseract: brew ...

  5. FastReport.Net使用:[10]报表栏目说明

    报表栏目说明 报表标题(Report Title):在每个报表的开始时打印. 报表合计区(Report Summary):在报表结尾时打印,显示在最后一行数据后,页脚前. 页眉(Page Header ...

  6. 20162327WJH四则运算第二周总结

    学号 20162327 <程序设计与数据结构>四则运算第二次实验报告 1.需求分析 1.本周我们进行了四则运算的后续完善,因为学习的比较欠缺,所以我负责比较简单的部分,就是只包含一个运算符 ...

  7. dubbo基础(初学习dubbo)

    1. 扩展   Soap是webService协议.是http+xml. Rest ful是http+json.相对于soap来说rest ful就是轻量的,因为==.   Rpc与soa区别? Rp ...

  8. bzoj 1674: [Usaco2005]Part Acquisition -- dijkstra(堆优化)

    1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec  Memory Limit: 64 MB Description The cows have b ...

  9. BeanFactoryPostProcessor和BeanPostProcessor

    1. BeanFactoryPostProcessor调用(见AbstractApplicationContext.refresh): >> 创建DefaultListableBeanFa ...

  10. Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈

    B. Mike and Feet Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pro ...