NEWS

[C#] Chia sẻ hiệu ứng chuyển động trên lập trình winform

[C#] Chia sẻ hiệu ứng chuyển động trên lập trình winform
Đăng bởi: Thảo Meo - Lượt xem: 5704 12:23:48, 07/12/2021C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục chia sẻ đến các bạn hiệu ứng chuyển động trên lập trình C#, winform.

[C#] Animation Effect in winform

Dưới đây là giao diện demo ứng dụng:

animation_effect

Đầu tiên, các bạn cần import thư viện SparkleLibrary, các bạn có thể download thư viện này ở source code cuối bài viết.

Full source code Animation Effect c#:

using BrightIdeasSoftware;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EffectSparkAnimation
{
    public partial class Form1 : Form
    {
        AnimationAdapter controlWithAnimation;
        Animation animation;
        public Form1()
        {
            InitializeComponent();
        }
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            var data = new DataTable();
            data.Columns.Add("id", typeof(string));
            data.Columns.Add("name", typeof(string));
            data.Columns.Add("address", typeof(string));
            data.Rows.Add("MASV001", "Nguyễn Thảo", "Vũng Tàu");
            data.Rows.Add("MASV002", "Nguyễn Đình Tuyên", "Quảng Bình");
            data.Rows.Add("MASV003", "Nguyễn Phương Nhi", "Kiên Giang");
            data.Rows.Add("MASV004", "Nguyễn Thị Cẩm Tú", "TP.HCM");
            data.Rows.Add("MASV005", "Cái Trí Minh", "Đồng Nai");
            data.Rows.Add("MASV006", "Võ Sơn Băng", "Vĩnh Long");
            dataGridView1.DataSource = data;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RunAnimationOnPauseButton();
            RunSpinnerAnimation();
        }

        private void RunAnimationOnPauseButton()
        {
            AnimationAdapter pauseButtonAnimationAdaptor = new AnimationAdapter(this.btnStart);
            Animation pauseButtonAnimation = pauseButtonAnimationAdaptor.Animation;

            Sprite imagesprite = new ImageSprite(Properties.Resources.smallGoldstar);
            imagesprite.Add(0, 3000, Effects.Move(Corner.MiddleLeft, new Point(-20, 0),
                Corner.MiddleRight, new Point(20, 0)));
            imagesprite.Add(0, 3000, Effects.Rotate(0, 680));
            pauseButtonAnimation.Add(0, imagesprite);

            pauseButtonAnimation.Start();
        }

        private void RunShapeAnimation()
        {
            this.animation = this.CreateAnimation();

            Sprite sprite = ShapeSprite.Circle(3.0f, Color.Red);
            sprite.Size = new Size(100, 150);
            sprite.Add(0, 3000, Effects.Move(0.1f, -0.1f, 0.1f, 1.2f));
            sprite.Add(0, 3000, Effects.Rotate(0, 680));
            this.animation.Add(0, sprite);

            sprite = ShapeSprite.Oval(8.0f, Color.DeepPink, Color.Pink);
            sprite.Size = new Size(100, 150);
            sprite.Add(0, 3000, Effects.Move(0.2f, -0.1f, 0.2f, 1.2f));
            sprite.Add(0, 3000, Effects.Rotate(0, 680));
            this.animation.Add(300, sprite);

            sprite = ShapeSprite.FilledRectangle(Color.PeachPuff);
            sprite.Size = new Size(100, 150);
            sprite.Add(0, 3000, Effects.Move(0.3f, -0.1f, 0.3f, 1.2f));
            sprite.Add(0, 3000, Effects.Rotate(0, 680));
            this.animation.Add(600, sprite);

            sprite = ShapeSprite.RoundedRectangle(8.0f, Color.DarkGoldenrod, Color.PaleGoldenrod);
            sprite.Size = new Size(100, 150);
            sprite.Add(0, 3000, Effects.Move(0.7f, -0.1f, 0.7f, 1.2f));
            sprite.Add(0, 3000, Effects.Rotate(0, 680));
            this.animation.Add(950, sprite);

            sprite = ShapeSprite.Square(3.0f, Color.Blue);
            sprite.Add(0, 3000, Effects.Move(0.8f, -0.1f, 0.8f, 1.2f));
            sprite.Size = new Size(100, 150);
            sprite.Add(0, 3000, Effects.Rotate(0, 680));
            this.animation.Add(1150, sprite);

            sprite = ShapeSprite.Triangle(1.0f, Color.Black, Color.Orchid);
            sprite.Add(0, 3000, Effects.Move(0.9f, -0.1f, 0.9f, 1.2f));
            sprite.Size = new Size(100, 150);
            sprite.Add(0, 3000, Effects.Rotate(0, 680));
            this.animation.Add(1350, sprite);

            this.animation.Start();
        }

        private void RunSpinnerAnimation()
        {
            this.animation = this.CreateAnimation();

            Sprite image = new ImageSprite(Properties.Resources.goldstar3);
            image.Add(0, 500, Effects.Move(Corner.BottomCenter, Corner.MiddleCenter));
            image.Add(0, 500, Effects.Rotate(0, 180));
            image.Add(500, 1500, Effects.Rotate(180, 720));
            image.Add(1000, 1000, Effects.Scale(1.0f, 4.0f));
            image.Add(1000, 1000, Effects.Goto(Corner.MiddleCenter));
            image.Add(1000, 1000, Effects.Fade(1.0f, 0.0f));
            this.animation.Add(0, image);

            Sprite text = new TextSprite("Welcome Website LAPTRINHVB.NET!", new Font("Tahoma", 32), Color.Blue, Color.AliceBlue, Color.Red, 3.0f);
            text.Opacity = 0.0f;
            text.FixedLocation = Locators.SpriteAligned(Corner.MiddleCenter);
            text.Add(900, 600, Effects.Fade(0.0f, 1.0f));
            text.Add(1000, 800, Effects.Rotate(180, 1440));
            text.Add(2000, 500, Effects.Scale(1.0f, 0.5f));
            text.Add(3500, 1000, Effects.Scale(0.5f, 4.0f));
            text.Add(3500, 1000, Effects.Fade(1.0f, 0.0f));
            this.animation.Add(0, text);

            //TextSprite text2 = new TextSprite("Ai sẽ là người chiến thắng đây", new Font("Tahoma", 24), Color.AliceBlue);
            //text2.BackColor = Color.DarkSlateGray;
            //text2.FixedLocation = Locators.SpriteAligned(Corner.TopCenter, new Point(20, 20));
            //text2.Add(0, 300, Effects.Fade(0.0f, 1.0f));
            //text2.Add(900, 2700, Effects.TickerBoard("Bạn Thảo là người chiến thắng."));
            //this.animation.Add(0, text2);

            this.animation.Start();
        }

        private Animation CreateAnimation()
        {
            if (this.animation != null)
            {
                this.animation.Stop();
                this.animation.Started -= new EventHandler<StartAnimationEventArgs>(animation_Started);
                this.animation.Stopped -= new EventHandler<StopAnimationEventArgs>(animation_Stopped);
            }

        
            this.controlWithAnimation = new AnimationAdapter(this.dataGridView1);
         
            this.animation = this.controlWithAnimation.Animation;
            this.animation.Started += new EventHandler<StartAnimationEventArgs>(animation_Started);
            this.animation.Stopped += new EventHandler<StopAnimationEventArgs>(animation_Stopped);

            return this.animation;
        }

        private void RunBorderAnimation()
        {
            this.animation = this.CreateAnimation();

            ShapeSprite shape = new ShapeSprite();
            shape.PenWidth = 3.0f;
            shape.Opacity = 0.8f;
            shape.Size = new Size(200, 300);
            shape.Add(0, 2000, Effects.Bounds(Locators.At(200, 300, 100, 200), Locators.AnimationBounds(-50, -50)));
            shape.Add(2000, 1500, Effects.Bounds(Locators.AnimationBounds(-150, -150)));
            shape.Add(3500, 1500, Effects.Bounds(Locators.At(200, 300, 100, 200)));
            this.animation.Add(0, shape);

            Sprite sprite = new ImageSprite(Properties.Resources.redloveheart);
            sprite.Add(0, 500, Effects.Fade(0.0f, 0.8f));
            sprite.Add(0, 5000, Effects.Walk(Locators.AnimationBounds(-100, -100), WalkDirection.Anticlockwise));
            sprite.Add(2200, 2800, Effects.Rotate(0, 680));
            sprite.Add(4500, 500, Effects.Fade(0.8f, 0.0f));
            this.animation.Add(0, sprite);

            Sprite sprite2 = new ImageSprite(Properties.Resources.goldstar3);
            sprite2.Add(0, 5000, Effects.Walk(new SpriteBoundsLocator(shape), Corner.BottomRight, WalkDirection.Clockwise));
            sprite2.Add(0, 5000, Effects.Rotate(0, -360 * 4));
            this.animation.Add(0, sprite2);

            this.animation.Start();
        }

        void animation_Started(object sender, StartAnimationEventArgs e)
        {
            this.UpdateUIState();
        }

        void animation_Stopped(object sender, StopAnimationEventArgs e)
        {
            this.UpdateUIState();
        }
        private void UpdateUIState()
        {
            if (this.InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate { this.UpdateUIState(); });
                return;
            }
            //this.btnStart.Enabled = this.animation.Running;
            //if (this.animation.Running && this.animation.Paused)
            //    this.btnStart.Text = "Un&pause";
            //else
            //    this.btnStart.Text = "&Pause";
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            RunShapeAnimation();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RunBorderAnimation();
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Chia sẻ hiệu ứng chuyển động trên lập trình winform
Đăng bởi: Thảo Meo - Lượt xem: 5704 12:23:48, 07/12/2021C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.