user1007435
user1007435

Reputation:

How to change form with animation?

How to change form with animation(like fading,Cutl Up,...) in win application?

I have two form Form1,Form2. I'm in form1 and there is a button on it and i want to change form when button clicked.

//button click
Form2 f2=new Form2();
this.hide();
f2.show();

Upvotes: 3

Views: 3345

Answers (2)

fardjad
fardjad

Reputation: 20424

You can use AnimateWindow(), define a new form class and override OnLoad() to show the form with your desired effects.

// Console Application
// Load System.Windows.Forms reference.

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);

class SlidingForm: System.Windows.Forms.Form 
{
    const int AW_ACTIVATE = 0X20000;
    const int AW_SLIDE = 0X40000;
    const int AW_HOR_POSITIVE = 0X1;

    protected override void OnLoad(System.EventArgs e)
    {
        AnimateWindow(this.Handle, 1000, AW_ACTIVATE | AW_SLIDE | AW_HOR_POSITIVE);
        base.OnLoad(e);
    }
}

void Main()
{
    var frm = new SlidingForm().ShowDialog;
}

Sample code to hide a PictureBox control as requested:

private void button1_Click(object sender, EventArgs e)
{
   const int AW_HIDE = 0x00010000;
   const int AW_SLIDE = 0x00040000;
   const int AW_HOR_NEGATIVE = 0x00000002;
   const int AW_HOR_POSITIVE = 0x00000001;
   // Toggle show/hide
   AnimateWindow(pictureBox1.Handle, 1000, (pictureBox1.Visible) ? (AW_HIDE | AW_HOR_NEGATIVE) : (AW_HOR_POSITIVE) | AW_SLIDE);
   pictureBox1.Visible ^= true;
}

Upvotes: 3

pyrocumulus
pyrocumulus

Reputation: 9300

Without going into the specifics of the code itself, I found this question which handles a similar subject: Simple animation in WinForms.

That question leads me to a library called dot-net-transitions which I think does exactly what you want.

Upvotes: 0

Related Questions