Sliding Effect in Windows Form Application


Introduction :

In this article i will teach you how to sliding form.you may have seen some application in which when you click on button Sliding Child Form appear from the main form.I will teach you how to make this kind of sliding form.




Technologies

CShap .net 2.0/3.5

Implementation

For making sliding form effect we need to work with Win32 API functions so to call API's Animation Functions.

So first of all import 

using System.Runtime.InteropServices;


here we are calling WIN32 API function so we need to declare some constant values that will be used by function when calling it
so declare all the constants in global area of the class.

      //Constants

       const int AW_SLIDE = 0X40000;

        const int AW_HOR_POSITIVE = 0X1;

        const int AW_HOR_NEGATIVE = 0X2;

        const int AW_BLEND = 0X80000;

 

        [DllImport("user32")]

        static extern bool AnimateWindow(IntPtr hwnd, int time, int flags);



Write code in overrides OnLoad Event of the form which you want to Slide

 

        protected override void OnLoad(EventArgs e)

        {

            //Load the Form At Position of Main Form

            int WidthOfMain=Application.OpenForms["MainForm"].Width;

            int HeightofMain= Application.OpenForms["MainForm"].Height;

            int LocationMainX=Application.OpenForms["MainForm"].Location.X ;

            int locationMainy=Application.OpenForms["MainForm"].Location.Y;

 

            //Set the Location

            this.Location = new Point(LocationMainX+WidthOfMain,locationMainy+10);

 

            //Animate form

            AnimateWindow(this.Handle, 500, AW_SLIDE |AW_HOR_POSITIVE);

        }



here in code we need to set the Sliding Window Location according to the Main window so we need to Code Some extra lines so that
sliding form can slide from proper place.

and Finally I am showing you how whole code will look alike I have Two Forms MainForm from which i press Slide Button and Another Form which will slide ie Form1

MainForm Code
-------------


namespace SlidingEffect

{

    public partial class MainForm : Form

    {

        public MainForm()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            bool IsOpen = false;

            FormCollection fc = Application.OpenForms;

            foreach (Form f in fc)

            {

                if (f.Name == "Form1")

                {

                    IsOpen = true;

                    f.Focus();

                    break;

                }

            }

 

            if (IsOpen == false)

            {

                Form1 form = new Form1();

                form.Show();

            }

        }

 

    }

}


Form1 Code (form Which will Slide From MainForm)

----------------------------------------------------

 public partial class Form1 : Form

    {

       //Constants

        const int AW_SLIDE = 0X40000;

        const int AW_HOR_POSITIVE = 0X1;

        const int AW_HOR_NEGATIVE = 0X2;

        const int AW_BLEND = 0X80000;

 

        [DllImport("user32")]

        static extern bool AnimateWindow(IntPtr hwnd, int time, int flags);

 

        public Form1()

        {

            InitializeComponent();

        }

 

        protected override void OnLoad(EventArgs e)

        {

            //Load the Form At Position of Main Form

            int WidthOfMain=Application.OpenForms["MainForm"].Width;

            int HeightofMain= Application.OpenForms["MainForm"].Height;

            int LocationMainX=Application.OpenForms["MainForm"].Location.X ;

            int locationMainy=Application.OpenForms["MainForm"].Location.Y;

 

            //Set the Location

            this.Location = new Point(LocationMainX+WidthOfMain,locationMainy+10);

 

            //Animate form

            AnimateWindow(this.Handle, 500, AW_SLIDE |AW_HOR_POSITIVE);

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            this.Close();

        }

    }

}


Conclusion :

Article teaches how to create sliding effect in Windows form application.

Up Next
    Ebook Download
    View all
    Learn
    View all