C#如何通过动画将按钮控件从右向左移动?
我有一系列带有位置和图像的按钮,然后我想看到它从右向左移动,比如说间隔2秒。这是我的相关代码C#如何通过动画将按钮控件从右向左移动?,c#,winforms,C#,Winforms,我有一系列带有位置和图像的按钮,然后我想看到它从右向左移动,比如说间隔2秒。这是我的相关代码 public partial class Form1 : Form { Button[] buttonanimal = new Button[21]; public Form1() { InitializeComponent(); for(int i=1;i<=20;i++) { butt
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;
}
}
}
}
公共部分类表单1:表单
{
按钮[]buttonanimal=新按钮[21];
公共表格1()
{
初始化组件();
对于(int i=1;i=1&i,可以使用设置为2秒(2000ms)间隔的,并更新事件处理程序中的按钮位置
下面是一个您可以调整代码的示例,显示了如何在屏幕上水平移动一个名为myButton
的按钮:
类内范围(表单的全局范围):
在窗体的Load
事件处理程序中:
timer.Interval = 2000; //set interval to 2 seconds
timer.Tick += Timer_Tick; //wire up event handler;
timer.Start(); //start the timer
以您的形式:
//Declare and instantiate a Timer
Timer timer = new 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();
}
}
我会为动画创建一个界面和一些类。然后处理这些动画对象的数组(或列表)。这样,部件之间的耦合非常低
一个简单的界面可能看起来像:
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);
}
}
然后,可以处理控件的类的一个非常基本的示例如下所示:
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();
}
公共部分类表单1:表单
{
IAnimator[]animators=新的cControl Animator[21];
公共表格1()
{
初始化组件();
对于(int i=1;i如果你想要动画移动到wpf。就这么简单。否则你必须手动实现它。@Carl wpf vs WinForms与在屏幕上以设定的间隔移动按钮无关;OP只需要一个计时器@CoolBots。当你必须编写这段代码时,它会有所不同(有问题)xaml中有几行代码负责处理与视图相关的逻辑。@Carl您没有抓住要点-问题是关于一个特定的设置;您本质上是建议放弃并重新编写它……顺便问一下,什么是“代码块”?这几行代码,不管您使用什么来实现it@carl“在其他平台中重写”但这并不是问题的真正解决方案。我们必须假设他无法做到这一点,除非另有说明。我如何使用此界面/类来设置按钮向各个方向移动?一些向左,一些向右,一些对角?…我是否需要为每个移动创建一个子类并覆盖TimerTick
(我想应该声明为虚拟的
)?如果是这样的话,您的系统虽然复杂,但似乎相当有限。此外,您是否正在为每个动画对象创建一个<代码>计时器代码>实例?20个对象,20个计时器?…这个答案只显示了一个起点。您决定为每种运动创建一个不同的类,或者创建一个具有“移动路径”组件的通用类因此没有任何限制。是的,这些方法应该是虚拟的,以便为进一步的继承(更改)做好准备。我希望看到移动路径组件的实现。对于需要其他操作的动画,比如移动时也会更改颜色的按钮,这不是“路径”呢?以及每个对象问题的计时器
?如果不进行子分类,您的系统将无法使用。尽管界面想法很好;实现它的类存在严重缺陷。哇,请放慢速度,一条注释中有许多问题。作为移动路径组件的想法,您可以创建一个包含起点、终点和calul函数的类按时间差计算中间点。当然,你可以为彩色动画实现一个单独的类,或者将其放在同一个类中。你喜欢什么。你应该使用子类来组织。当使用许多动画时,计时器可能会成为一个问题,但示例看起来不是这样。即使在这种情况下,也要改进它。因此,在最后,这只是一个起点。你的意思是什么?