Advertisement
Guest User

winsizer

a guest
Apr 8th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. //using System.Text;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. using System.Drawing;
  7. using System.Diagnostics;
  8.  
  9. namespace AnimatedTransform
  10. {
  11.     static class FormTransform
  12.     {
  13.         public static AutoResetEvent resizeEnded = new AutoResetEvent(false);
  14.         //public static bool resizeEnded = false;
  15.  
  16.         public static void TransformSize(Form frm, int newWidth, int newHeight, bool center)
  17.         {
  18.             TransformSize(frm, new Size(newWidth, newHeight), center);
  19.         }
  20.  
  21.         public static void TransformSize(Form frm, Size newSize, bool center=false)
  22.         {
  23.             ParameterizedThreadStart threadStart = new ParameterizedThreadStart(RunTransformation);
  24.             Thread transformThread = new Thread(threadStart);
  25.             transformThread.Start(new object[] { frm, newSize, center });
  26.         }
  27.  
  28.         private delegate void RunTransformationDelegate(object paramaters);
  29.         private static void RunTransformation(object parameters)
  30.         {
  31.             Form frm = (Form)((object[])parameters)[0];
  32.             if (frm.InvokeRequired)
  33.             {
  34.                 RunTransformationDelegate del = new RunTransformationDelegate(RunTransformation);
  35.                 frm.Invoke(del, parameters);
  36.             }
  37.             else
  38.             {
  39.                 //Animation variables
  40.                 double FPS = 100.0;
  41.                 long interval = (long)(Stopwatch.Frequency / FPS);
  42.                 long ticks1 = 0;
  43.                 long ticks2 = 0;
  44.  
  45.                 //Dimension transform variables
  46.                 Size size = (Size)((object[])parameters)[1];
  47.                 bool center = (bool)((object[])parameters)[2];
  48.  
  49.                 int xDiff = Math.Abs(frm.Width - size.Width);
  50.                 int yDiff = Math.Abs(frm.Height - size.Height);
  51.  
  52.                 int step = 60;
  53.  
  54.                 int xDirection = frm.Width < size.Width ? 1 : -1;
  55.                 int yDirection = frm.Height < size.Height ? 1 : -1;
  56.  
  57.                 int xStep = step * xDirection;
  58.                 int yStep = step * yDirection;
  59.  
  60.                 int rsxStep = ((step / 2) * xDirection) * -1;
  61.                 int rsyStep = ((step / 2) * yDirection) * -1;
  62.  
  63.                 bool widthOff = IsWidthOff(frm.Width, size.Width, xStep);
  64.                 bool heightOff = IsHeightOff(frm.Height, size.Height, yStep);
  65.  
  66.  
  67.                 while (widthOff || heightOff)
  68.                 {
  69.                     //Get current timestamp
  70.                     ticks2 = Stopwatch.GetTimestamp();
  71.  
  72.                     if (ticks2 >= ticks1 + interval) //only run logic if enough time has passed "between frames"
  73.                     {
  74.                         //Adjust the Form dimensions
  75.                         if (widthOff)
  76.                         {
  77.                             frm.Width += xStep;
  78.                             if (center)
  79.                             {
  80.                                 if (xDirection > 0 && frm.Left <= 0)
  81.                                 {
  82.                                     frm.Left = 0;
  83.                                 }
  84.                                 else
  85.                                 {
  86.                                     frm.Left += rsxStep;
  87.                                 }
  88.                             }
  89.                         }
  90.  
  91.  
  92.                         if (heightOff)
  93.                         {
  94.                             frm.Height += yStep;
  95.                             if (center)
  96.                             {
  97.                                 if (yDirection > 0 && frm.Top <= 0)
  98.                                 {
  99.                                     frm.Top = 0;
  100.                                 }
  101.                                 else
  102.                                 {
  103.                                     frm.Top += rsyStep;
  104.                                 }
  105.                             }
  106.                         }
  107.                        
  108.  
  109.                         widthOff = IsWidthOff(frm.Width, size.Width, xStep);
  110.                         heightOff = IsHeightOff(frm.Height, size.Height, yStep);
  111.  
  112.                         //Allows the Form to refresh
  113.                         Application.DoEvents();
  114.  
  115.                         //Save current timestamp
  116.                         ticks1 = Stopwatch.GetTimestamp();
  117.                     }
  118.  
  119.                     Thread.Sleep(1);
  120.                 }
  121.                 frm.Width = size.Width; // maradék bepotlása
  122.                 frm.Height = size.Height; // maradék bepótlása
  123.                 resizeEnded.Set();
  124.             }
  125.         }
  126.  
  127.         private static bool IsWidthOff(int currentWidth, int targetWidth, int step)
  128.         {
  129.             //Do avoid uneven jumps, do not change the width if it is
  130.             //within the step amount
  131.             if (Math.Abs(currentWidth - targetWidth) <= Math.Abs(step)) return false;
  132.  
  133.             return (step > 0 && currentWidth < targetWidth) || //increasing direction - keep going if still too small
  134.                    (step < 0 && currentWidth > targetWidth); //decreasing direction - keep going if still too large
  135.         }
  136.  
  137.         private static bool IsHeightOff(int currentHeight, int targetHeight, int step)
  138.         {
  139.             //Do avoid uneven jumps, do not change the height if it is
  140.             //within the step amount
  141.             if (Math.Abs(currentHeight - targetHeight) <= Math.Abs(step)) return false;
  142.  
  143.  
  144.             return (step > 0 && currentHeight < targetHeight) || //increasing direction - keep going if still too small
  145.                    (step < 0 && currentHeight > targetHeight); //decreasing direction - keep going if still too large
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement