winform左右滑動

public static class FormTransform { public static void TransformSize(Form frm, int newWidth, int newHeight) { TransformSize(frm, new Size(newWidth, newHeight)); } public static void TransformSize(Control ctl, int newWidth, int newHeight) { TransformSize(ctl, new Size(newWidth, newHeight)); } public static void TransformSize(object 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 boder = (Form)((object[])parameters)[0];
            Panel boder = (Panel)((object[])parameters)[0]; if (boder.InvokeRequired) { RunTransformationDelegate del = new RunTransformationDelegate(RunTransformation); boder.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(boder.Width - size.Width); int yDiff = Math.Abs(boder.Height - size.Height); int step = 10; int xDirection = boder.Width < size.Width ? 1 : -1; int yDirection = boder.Height < size.Height ? 1 : -1; int xStep = step * xDirection; int yStep = step * yDirection; //要調整的窗體的寬度是否在步長以內
                bool widthOff = IsWidthOff(boder.Width, size.Width, xStep); //要調整的窗體的高度是否在步長以內
                bool heightOff = IsHeightOff(boder.Height, size.Height, yStep); while (widthOff || heightOff) { //獲取當前的時間戳
                    ticks2 = Stopwatch.GetTimestamp(); //容許調整大小僅在有足夠的時間來刷新窗體的時候
                    if (ticks2 >= ticks1 + interval) { //調整窗體的大小
                        if (widthOff) boder.Width += xStep; if (heightOff) boder.Height += yStep; widthOff = IsWidthOff(boder.Width, size.Width, xStep); heightOff = IsHeightOff(boder.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); } }
//調用代碼
FormTransform.TransformSize(this, width, height);
相關文章
相關標籤/搜索