zoukankan      html  css  js  c++  java
  • WinForm 窗口缩放动画效果

    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);
    

  • 相关阅读:
    NopCommerce 增加 Customer Settings
    NopCommerce 增加 Customer Attributes
    [转]How to add new table in NopCommerce
    [转]教你一招
    [转]Oracle 修改或者删除临时表 ORA-14452: 试图创建, 更改或删除正在使用的临时表中的索引
    NopCommerce 发布时 Could not load file or assembly 'file:///...Autofac.3.5.2lib et40Autofac.dll' or one of its dependencies
    NopCommerce 关于Customer的会员类别及会员价处理 的尝试途径
    诸葛亮-诫子书
    [书目20161206]小狗钱钱的人生整理术
    [转]NopCommerce How to code my own payment method
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/7340595.html
Copyright © 2011-2022 走看看