C # animation display effect of the control,

Source: Internet
Author: User

C # animation display effect of the control,

1. Reason:

There is a prompt for the task to be completed in the project, which needs to be displayed with an animated effect, such:

This is written by Delphi for the old project, and is implemented by c #. This effect is designed. This is not difficult, but it is only a small skill, but for ease of expansion, write a static class implementation.

 

2. Animation class

The Code is as follows:

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

It is very short, and its implementation controls have effects from four sides, such:

 

3. Final Implementation:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.