Sliding Effect in Windows Form Application in C#

No.of Views2508
Bookmarked0 times
Downloads 
Votes0
By  kirtan007   On  22 Jun 2010 08:06:49
Tag : CSharp , Windows Forms
Article will teach you how to achieve sliding effect in windows form
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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. 

Image Loading

Technologies

C#, .NET 2.0/3.5/4/0

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.

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 Formint 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 Locationthis.Location = new Point(LocationMainX+WidthOfMain,locationMainy+10);//Animate formAnimateWindow(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
    {//Constantsconst 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 Formint 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 Locationthis.Location = new Point(LocationMainX+WidthOfMain,locationMainy+10);//Animate formAnimateWindow(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.

Sample Project Source

Download source files -44 kb

 
Sign Up to vote for this article
 
About Author
 
kirtan007
Occupation-
Company-
Member Type-Senior
Location-Not Provided
Joined date-02 Jul 2009
Home Page-http://kirtan.uni.cc
Blog Page-
He completed his Bachelor of Computer Application from Gujarat University 2009 .He is doing Master of Computer Application from Gujarat Technological University right now .. His area of Interests are Web Hacking , C# .net Windows form ,asp.net , WPF ,Silverlight ,SQL Server and Some PHP.
 
 
Other popularSectionarticles
Comments
By:HanishDate Of Posted:7/28/2010 10:22:09 AM
Facing a Problem
How can i use this inside a parent form. I tried it and it works too but i can't get it inside the parent form. Please help!
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top