Animated Screen In Windows Forms

Introduction

In this article, we will learn how to create an animated loading screen in windows form application. We can also say that animation is a picture or figure which are manipulated to appear as moving images. This will help us to create animated windows forms. adding animation in forms will make it different from other simple forms. Let's have a look at the steps for the animated screen in windows forms application.

Step 1

First of all open visual studio and then click on create new project.

Adding Animated screen using windows forms

Step 2 

After clicking create a new project, a new window will pop up in this new window you have to click on windows form application and it will help to select the templates of the project and then click on next button.

Adding Animated screen using windows forms

Step 3 

In this step you have to select the name of the project, location of the project, and the .Net framework of your choice after that click on create.

Adding Animated screen using windows forms

Step 4

After selecting the name of the project similar window will pop up as shown below and then click on solution explorer then right-click on the name that you have decided for the project as I have decided is demo.

Adding Animated screen using windows forms

Step 5

Solution explorer will open the given below menu in the menu you have to select add menu and in add menu select new item.   

   

Step 6

In this window, Select User Control(Windows Forms) option and in name section you can change the name after that click on add button.

Adding Animated screen using windows forms

Step 7

After clicking add button a new window will open in this window you have to double click and then given below window will pop up.

Adding Animated screen using windows forms

Step 8

And then write the code for creating animated screen and it contains color of the screen, size of the screen, and border radius. For creating a rectangle you have to add a namespace called system.Drawing.Drawing2D. after that write given below code.

protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
    GraphicsPath gp = new GraphicsPath();
    gp.AddArc(new Rectangle(0, 0, size, size), 180, 90);
    gp.AddArc(new Rectangle(Width - size, 0, size, size), -90, 90);
    gp.AddArc(new Rectangle(Width - size, Height - size, size, size), 0, 90);
    gp.AddArc(new Rectangle(0, Height - size, size, size), 90, 90);
    e.Graphics.FillPath(new LinearGradientBrush(ClientRectangle, clor1, clor2, distance), gp);
    e.Graphics.DrawString(txt, Font, new SolidBrush(ForeColor), ClientRectangle, new StringFormat() {
        LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center
    });
    base.OnPaint(e);
}

gp.AddArc will create a curve rectangle and e.graphics.fillpath will add colors to curved rectangle and in last e.graphics.drawstring will add text to the screen, 

Step 9

Next add the colors to the screen for that create property in this get method return the value of the variable name and set method will assign the value.

public Color Color {
    get {
        return clor1;
    }
    set {
        clor1 = value;
        Invalidate();
    }
}
public Color Color1 {
    get {
        return clor2;
    }
    set {
        clor2 = value;
        Invalidate();
    }
}

In above code get method will return the value of clor1 and set method will assign the value of clor1. Similarly, in Color1 get method will return the value of clor2 and set method will assign the value of clor2.  

Step 10

public UserControl1() {
    InitializeComponent();
    DoubleBuffered = true;
    t.Interval = 60;
    t.Start();
    t.Tick += (s, e) => {
        angle = angle % 360 + 1;
    };
    ForeColor = Color.White;
}
public float angle {
    get {
        return distance;
    }
    set {
        distance = value;
        Invalidate();
    }
}
public int Borderradius {
    get {
        return size;
    }
    set {
        size = value;
        Invalidate();
    }
}

In this create an object of timer called t and above code is used to start the timer, interval, and tick (occurs when intervals have elapsed). And to create borderradius in the get method will return the size of the radius and set method will assign the size of the screen.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace animation {
    [DefaultEvent("click")]
    public partial class UserControl1: UserControl {
        int size = 20;
        float distance = 45;
        Color clor1 = Color.Blue, clor2 = Color.Orange;
        Timer t = new Timer();
        string txt = "Loading";
        public UserControl1() {
            InitializeComponent();
            DoubleBuffered = true;
            t.Interval = 60;
            t.Start();
            t.Tick += (s, e) => {
                angle = angle % 360 + 1;
            };
            ForeColor = Color.White;
        }
        public float angle {
            get {
                return distance;
            }
            set {
                distance = value;
                Invalidate();
            }
        }
        public int Borderradius {
            get {
                return size;
            }
            set {
                size = value;
                Invalidate();
            }
        }
        protected override void OnPaint(PaintEventArgs e) {
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            GraphicsPath gp = new GraphicsPath();
            gp.AddArc(new Rectangle(0, 0, size, size), 180, 90);
            gp.AddArc(new Rectangle(Width - size, 0, size, size), -90, 90);
            gp.AddArc(new Rectangle(Width - size, Height - size, size, size), 0, 90);
            gp.AddArc(new Rectangle(0, Height - size, size, size), 90, 90);
            e.Graphics.FillPath(new LinearGradientBrush(ClientRectangle, clor1, clor2, distance), gp);
            e.Graphics.DrawString(txt, Font, new SolidBrush(ForeColor), ClientRectangle, new StringFormat() {
                LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center
            });
            base.OnPaint(e);
        }
        public Color Color {
            get {
                return clor1;
            }
            set {
                clor1 = value;
                Invalidate();
            }
        }
        public Color Color1 {
            get {
                return clor2;
            }
            set {
                clor2 = value;
                Invalidate();
            }
        }
        private void UserControl1_Load(object sender, EventArgs e) {}
    }
}

Conclusion

This article will help to create an animated screen using windows forms appliction in c#. This is all about creating animated forms different from other simple forms.

Thanks, I hope this will help you.


Similar Articles