C#中的动画面板

时间:2014-12-31 18:18:31

标签: c# winforms winapi button panel

我试图在点击按钮时添加面板。我的代码在下面,我做到了。但现在我正试图在我的面板上放置其他按钮等,当你点击第一个按钮和面板幻灯片时,我没有任何新按钮。

//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;

            }


        }

在相片面板中,我有三个相框。但是当面板显示(滑入)图片框时,就不存在了。

2 个答案:

答案 0 :(得分:7)

好的 - 这是一个非常简单的例子,它不依赖于AnimateWindow API:

为表单添加计时器控件。在我的,我将间隔设置为10(毫秒)。您可以使用此值来根据需要平滑动画

表格上有按钮和面板(不可见)

我在表单上声明了以下私有成员 - 它们是面板的起始X位置,结束位置和每个增量移动的像素数 - 再次,调整以影响速度/平滑度等等

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

然后在按钮上单击,我启用计时器:

animationTimer.Enabled = true;

最后,计时器刻度事件中的代码使面板可见,将其移动到位,并在完成后自行禁用:

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;
    }            
}

答案 1 :(得分:0)

我知道这是一个老话题,但是有一个简单的解决方法可以解决照片不显示的问题。该面板最初设置为visible = false,并且由于AnimateWindow实际上并未显示完整的控件,因此请确保在调用AnimateWindow之后将control.Visible = true设置为true。 所以在代码行之后:

//Animate form

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

// just add this:

photopanel.Visible = true;

// or in one line

Photopanel.Visible = AnimateWindow(photospanel.Handle, 500, AW_SLIDE | AW_HOR_POSITIVE);