打赏
随笔 - 111,  文章 - 0,  评论 - 36,  阅读 - 29万

1.  缘由:

项目中任务完成有个提示,需要以动画效果展示,其效果当如下图:

此为老项目为Delphi所写,改用c#实现,此效果做些设计。本也不难,小技而已,但为易于扩展,写了个静态类实现。

 

2. Animation动画类

直上代码如下:

复制代码
    public static class Animation
    {
        private static readonly int MoveStep = 25;
        private static 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 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();
            }
        }
    }
复制代码

非常短小,其实现控件由四面出现效果,如下图:

 

3. 最终实现效果图:

 

posted on   楚人无衣  阅读(9903)  评论(3编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示