winform 实现动画效果。

网页中我们借助jquery等框架可以实现很炫的动画效果,故想在winform中也实现同样的效果之一:滑入效果。

借助这个类即可:

public static class Animation
    {
        private static readonly int MoveStep = 25;
        private static System.Windows.Forms.Timer tmrAnim = null;
        private static Control control = null;
        private static AnchorStyles direction = AnchorStyles.None;
        private static Size destSize;

        private static void InitTimer()
        {
            if (tmrAnim == null)
            {
                tmrAnim = new System.Windows.Forms.Timer();
                tmrAnim.Interval = 25;
                tmrAnim.Tick += new System.EventHandler(tmrAnim_Tick);
            }
        }

        private static void tmrAnim_Tick(object sender, System.EventArgs e)
        {
            int newValue = 0;
            int offSet = 0;
            switch (direction)
            {
                case AnchorStyles.Left:
                case AnchorStyles.Right:
                    newValue = control.Width + MoveStep;
                    if (newValue > destSize.Width)
                    {
                        tmrAnim.Stop();
                        newValue = destSize.Width;
                    }

                    offSet = newValue - control.Width;
                    control.Width += offSet;
                    if (direction == AnchorStyles.Left)
                        control.Left -= offSet;
                    break;
                case AnchorStyles.Top:
                case AnchorStyles.Bottom:
                    newValue = control.Height + MoveStep;
                    if (newValue > destSize.Height)
                    {
                        tmrAnim.Stop();
                        newValue = destSize.Height;
                    }

                    offSet = newValue - control.Height;
                    control.Height += offSet;
                    if (direction == AnchorStyles.Top)
                        control.Top -= offSet;
                    break;
            }
        }

        public static void ShowControl(Control control, bool visible, AnchorStyles direction = AnchorStyles.None)
        {
            if (direction == AnchorStyles.None)
            {
                control.Visible = visible;
                return;
            }

            if (!visible)
            {
                if (tmrAnim != null)
                    tmrAnim.Stop();
                control.Hide();
            }
            else
            {
                InitTimer();

                if (Animation.control != control && destSize.IsEmpty)
                {
                    destSize = new Size(control.Width, control.Height);
                }
                Animation.control = control;
                Animation.direction = direction;
                switch (direction)
                {
                    case AnchorStyles.Left:
                    case AnchorStyles.Right:
                        if (direction == AnchorStyles.Left)
                            control.Left += control.Width;
                        control.Width = 0;
                        break;
                    case AnchorStyles.Top:
                    case AnchorStyles.Bottom:
                        if (direction == AnchorStyles.Top)
                            control.Top += control.Height;
                        control.Height = 0;
                        break;
                }
                control.Show();
                tmrAnim.Start();
            }
        }
    }

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Winform实现动画效果可以借助一些框架和代码来实现。一个常见的动画效果是滑入效果。你可以使用类似jQuery的框架,如使用C#的Winform框架中的Animation类来实现滑入效果。 下面是一个代码示例,展示了如何使用Animation类来实现滑入效果: ```csharp if (Animation.control != control && destSize.IsEmpty) { destSize = new Size(control.Width, control.Height); } Animation.control = control; Animation.direction = direction; switch (direction) { case AnchorStyles.Left: case AnchorStyles.Right: if (direction == AnchorStyles.Left) control.Left = control.Width; control.Width = 0; break; case AnchorStyles.Top: case AnchorStyles.Bottom: if (direction == AnchorStyles.Top) control.Top = control.Height; control.Height = 0; break; } control.Show(); tmrAnim.Start(); ``` 这段代码中,destSize变量用于存储控件的目标大小。根据滑入的方向,通过改变控件的位置和大小来实现滑入效果。最后,调用control.Show()方法显示控件,并通过tmrAnim.Start()方法启动动画。 在开发过程中,你可以根据需要适当地使用动画效果来提升用户体验。虽然网上有很多关于Winform动画效果的代码和示例,但需要注意选择可靠的资源来确保代码的运行正常。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [winform 实现动画效果。](https://blog.csdn.net/ligaogang/article/details/83617281)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Winform窗体动画效果](https://download.csdn.net/download/shanxing_123/8333163)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值