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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 首先,你需要在Visual Studio中创建一个Windows Forms应用程序,然后添加一个窗体,然后在窗体上添加一些控件,如文本框、按钮等,以便用户输入用户名和密码。 接下来,你需要编写代码,以便实现登录动画的功能,这可以通过使用窗体上的控件,如文本框和按钮来实现,你可以添加动画效果来更好地表现登录过程。 ### 回答2: 使用Winform编写一个登录动画可以通过以下步骤实现: 1. 打开Visual Studio,创建一个新的Winform应用程序项目。 2. 在主窗体上添加一个PictureBox控件,作为登录动画的容器。 3. 在项目中添加一个GIF动画文件,将其设置为PictureBox的Image属性,以显示动画效果。 4. 在窗体的Load事件中,启动一个计时器控件,并设置其Tick事件处理程序。 5. 在计时器的Tick事件处理程序中,逐渐改变PictureBox控件的透明度属性,实现逐渐淡入或淡出的效果。可以使用定时器的Interval属性控制透明度的变化速度。 6. 在登录按钮的Click事件处理程序中,停止计时器,并进行登录验证操作。 7. 如果验证成功,则进行登录成功的操作;否则,根据需要进行错误信息提示。 8. 可以通过增加动画效果,如登录框的缩放、平移等,来提升动画的美观度和交互性。 以上是一个简单的用Winform编写登录动画的步骤,可以根据实际需求进行修改和扩展。 ### 回答3: Winform 是一种用于开发 Windows 窗体应用程序的框架,可以使用该框架来创建各种窗体和控件。下面是一个用 Winform 写的登陆动画实现过程: 首先,我们需要创建一个新的 Windows 窗体应用程序项目。在窗体上添加一个 Label 控件,作为登陆界面的标题。然后在窗体上添加一个 PictureBox 控件,作为登陆动画的展示区域。接下来,我们可以通过 PictureBox 的 Image 属性来设置登陆动画的帧序列。 在加载窗体时,我们可以使用 Timer 控件来触发动画的刷新。在 Timer 的 Tick 事件中,通过修改 PictureBox 控件的 Image 属性来切换动画的帧。这可以使用一个 List 或者数组来存储所有帧的图像,并使用一个变量来记录当前显示的帧索引。每次 Timer 触发时,将索引递增,并将对应索引的图像设置为 PictureBox 的 Image 属性值。在达到最后一帧时,可以将索引重置为第一帧,实现循环播放的效果。 为了增加动画效果,我们还可以在登陆界面上添加一些控件,如 ProgressBar、Label 等,来模拟登陆过程中的一些交互和进度展示。 最后,在完成动画实现后,可以添加一些其他逻辑,如验证用户输入的用户名和密码是否正确,并在登陆成功或失败时作出相应的提示。 以上就是用 Winform 写一个简单的登陆动画实现过程。当然,根据实际需求和个人创意,我们还可以通过改进和扩展来实现更加复杂和炫酷的登陆动画效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值