C#如何通过动画从右向左移动按钮控件?

时间:2016-06-01 07:26:41

标签: c# winforms

我有一系列带位置和图像的按钮,然后我想看到它从右向左移动,让我们说2秒间隔。这是我的相关代码。

    public partial class Form1 : Form
{
    Button[] buttonanimal = new Button[21];


    public Form1()
    {
        InitializeComponent();

        for(int i=1;i<=20;i++)
        {
            buttonanimal[i] = new Button();
            buttonprop(buttonanimal[i]);

            if(i>=1&i<=4)
            {
                Point[] points = new Point[4];

                points[0] = new Point(100, 100);
                points[1] = new Point(400, 100);
                points[2] = new Point(700, 100);
                points[3] = new Point(1000, 100);

                buttonanimal[i].Visible = true;
                buttonanimal[i].Location = points[i - 1];
            }
        }
    }

    private void buttonprop(Button buttons)
    {
        buttons.Size = new Size(100, 100);
        buttons.Visible = false;
        Controls.Add(buttons);
    }


    private void ButtonNext_Click(object sender, EventArgs e)
    {
        Point[] points = new Point[4];

        points[0] = new Point(100, 100);
        points[1] = new Point(400, 100);
        points[2] = new Point(700, 100);
        points[3] = new Point(1000, 100);

        for (int i=1;i<=20;i++)
        {
            if(buttonanimal[i].Visible==true)
            {
                buttonanimal[i].Visible = false;

                buttonanimal[i + 1].Visible = true;
                buttonanimal[i + 2].Visible = true;
                buttonanimal[i + 3].Visible = true;
                buttonanimal[i + 4].Visible = true;

                buttonanimal[i + 1].Location = points[i - 1];
                buttonanimal[i + 2].Location = points[i - 2];
                buttonanimal[i + 3].Location = points[i - 3];
                buttonanimal[i + 4].Location = points[i - 4];

                break;
            }
        }
    }
}

我在开始时有4个可见按钮。当您按下ButtonNext时。左侧的第一个可见按钮(按钮1)将消失,它将由按钮2替换,依此类推。我也想看到4个按钮从右向左移动。

2 个答案:

答案 0 :(得分:1)

您可以使用设置为2秒(2000毫秒)间隔的Timer,并在Tick事件的事件处理程序中更新按钮位置。

以下是您可以适应代码的示例,展示了如何在屏幕上水平移动一个名为myButton的按钮:

在课程范围内(全局到您的表格):

//Declare and instantiate a Timer
Timer timer = new Timer();

在表单的Load事件处理程序中:

timer.Interval = 2000;  //set interval to 2 seconds
timer.Tick += Timer_Tick;  //wire up event handler;
timer.Start();  //start the timer

以您的形式:

private void Timer_Tick(object sender, EventArgs e)
{
    //Move the button by 1 pixel
    myButton.Location = new Point(myButton.Location.X + 1, myButton.Location.Y)

    //Stop the timer once the button reaches the right edge
    if(myButton.Location.X + myButton.Width >= ClientSize.Width)
    {
        timer.Stop();
    }
}

答案 1 :(得分:0)

我会为Animations创建一个接口和一些类。然后处理这些动画对象的数组(或列表)。这样,部件之间的耦合非常低。

简单的界面可能如下所示:

interface IAnimator
{
    void Start();
    void Stop();
}

然后,可以处理控件的类的一个非常基本的示例可能如下所示:

public class CControlAnimator: IAnimator
{
    public Control control { get; set; }
    private Timer timer;
    // Data needed for animation

    public CControlAnimator()
    {
        timer = new Timer();
        timer.Tick += TimerTick;
    }

    public virtual void Start()
    {
        timer.Enabled = true;
    }

    public virtual void Stop()
    {
        timer.Enabled = false;
    }

    public void TimerTick(object sender, EventArgs e)
    {
        // Do the animation
        if (control != null)
        {
            control.Location = new System.Drawing.Point(control.Location.X + 2, control.Location.Y);
        }
    }

现在您可以使用它:

public partial class Form1 : Form
{
    IAnimator[] animators = new CControlAnimator[21];

    public Form1()
    {
        InitializeComponent();

        for (int i = 1; i <= 20; i++)
        {
            CControlAnimator animator = new CControlAnimator();
            animators[i] = animator;
            animator.control = new Button();
            // ...more init
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int i = 5;
        animators[i].Start();
    }

动画师应该知道concreate动画的样子,因为你使用界面,你可以创建许多不同的混合动画师并以相同的方式处理它们......