Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animated Panel in C#

I am trying to add a panel when a button click. My code is below and I did it. But now I am trying to put on my panel other buttons etc and when you click the first button and the panel slide in there aren't any of my new buttons.

//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);
        photosflag=0;
           
 private void photosbutton_Click(object sender, EventArgs e)
        {
            if (photosflag == 0)
            {
                object O = Controller.Properties.Resources.ResourceManager.GetObject("photospressed");
                photosbutton.Image = (System.Drawing.Image)O;
                photosflag = 1;
                int ylocation = photosbutton.Location.Y;
                //Set the Location
                photospanel.Location = new Point(101, ylocation);

                //Animate form
                AnimateWindow(photospanel.Handle, 500, AW_SLIDE | AW_HOR_POSITIVE);
                
                
            }
            else
            {
                object O = Controller.Properties.Resources.ResourceManager.GetObject("photos");
                photosbutton.Image = (System.Drawing.Image)O;
                photosflag = 0;
                photospanel.Visible = false;

            }
           
           
        }

In the photos panel, I have three picture boxes. But when the panel shows up (slide-in) the picture boxes there aren't exist.

like image 413
NickName Avatar asked Feb 12 '23 06:02

NickName


1 Answers

Okay - here is a really simple example that doesn't depend on the AnimateWindow API:

Add a timer control to your form. On mine, I set the interval to 10 (milliseconds). You can play with this value to smooth out the animation as necessary

I have the button and panel (not visible) on the form

I declared the following private members on the form - they are the start X position of the panel, the end position, and the number of pixels to move per increment - again, tweak to affect speed/smoothness/etc

private int _startLeft = -200;  // start position of the panel
private int _endLeft = 10;      // end position of the panel
private int _stepSize = 10;     // pixels to move

Then on the button click, I enable the timer:

animationTimer.Enabled = true;

Finally, the code in the timer tick event makes the panel visible, moves it into place, and disables itself when done:

private void animationTimer_Tick(object sender, EventArgs e)
{
    // if just starting, move to start location and make visible
    if (!photosPanel.Visible)
    {
        photosPanel.Left = _startLeft;
        photosPanel.Visible = true;
    }

    // incrementally move
    photosPanel.Left += _stepSize;
    // make sure we didn't over shoot
    if (photosPanel.Left > _endLeft) photosPanel.Left = _endLeft;

    // have we arrived?
    if (photosPanel.Left == _endLeft)
    {
        animationTimer.Enabled = false;
    }            
}
like image 178
snow_FFFFFF Avatar answered Feb 13 '23 20:02

snow_FFFFFF