Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在timer C Winforms中移动图片框_C#_Winforms_Timer_2d_Picturebox - Fatal编程技术网

C# 在timer C Winforms中移动图片框

C# 在timer C Winforms中移动图片框,c#,winforms,timer,2d,picturebox,C#,Winforms,Timer,2d,Picturebox,好的,所以请保持答案非常直接,我必须说我对C非常陌生,我不知道很多东西。不用再多说我的问题了。 我试图用计时器在屏幕上水平移动一个图片框。计时器必须无限移动。我已经尝试了我目前所知道的所有C语言,并且四处搜索了很多,但是没有任何东西能够回答我的确切问题,这正是我所需要的,因为我对C语言的了解较少。在过去的两周里,我主要从事图形方面的工作,而其余的工作都是为了让它发挥作用,所以我的游戏中没有代码。这是因为任何工作,我需要这个部分的工作。我的游戏是2D自上而下。非常感谢您的帮助 感谢您抽出时间阅读

好的,所以请保持答案非常直接,我必须说我对C非常陌生,我不知道很多东西。不用再多说我的问题了。 我试图用计时器在屏幕上水平移动一个图片框。计时器必须无限移动。我已经尝试了我目前所知道的所有C语言,并且四处搜索了很多,但是没有任何东西能够回答我的确切问题,这正是我所需要的,因为我对C语言的了解较少。在过去的两周里,我主要从事图形方面的工作,而其余的工作都是为了让它发挥作用,所以我的游戏中没有代码。这是因为任何工作,我需要这个部分的工作。我的游戏是2D自上而下。非常感谢您的帮助

感谢您抽出时间阅读

编辑


不需要更多的答案,谢谢你的回答,这对我帮助很大。

使用pictureBox.Location=new Pointx,y或设置pictureBox.Left/Top/Right。您可以将x和y定义为变量,并使用默认值初始化它们。定时器滴答声时增加x

样本1:

public partial class Form1 : Form
{
    private Random _random 

    public Form1()
    {
        InitializeComponent();
        _random = new Random();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        int x = _random.Next(0, 500);
        int y = _random.Next(0, 500);
        pictureBox1.Top += y;
        pictureBox1.Left += x;
    }
}
样本2:

private void timer1_Tick(object sender, EventArgs e)
{
  this.SuspendLayout();
  pictureBox.Location = new Point(picust.Location.X + 10, picust.Location.Y);
  this.ResumeLayout();
}

将标题为LEFT和RIGHT的两个按钮添加到表单中,并编写以下代码。 它可能会给你一个想法,如何做简单的移动动画

public partial class Form1 : Form
{
    int difference = 0;
    Timer timer = new Timer();
    public Form1()
    {
        InitializeComponent();
        timer.Interval = 15;
        timer.Tick += timer_Tick;
        timer.Start();
    }

    void timer_Tick(object sender, EventArgs e)
    {
        pictureBox1.Left += difference;
    }

    private void btnLeft_Click(object sender, EventArgs e)
    {
        difference = -2;
    }

    private void btnRight_Click(object sender, EventArgs e)
    {
        difference = 2;
    }
}

请尝试以下代码,它将起作用:

private void timer1_Tick(object sender, EventArgs e)
{
    int width = this.Width; // get the width of Form.

    if(pictureBox1.Location.X > width - pictureBox1.Width) //to check condition if pic box is touch the boundroy of form width
    {
        pictureBox1.Location = new Point(1, pictureBox1.Location.Y); // pic box is set to the new point. here 1 is indicate of X coordinate.
    }
    else
    {
        pictureBox1.Location = new Point(pictureBox1.Location.X + 100, pictureBox1.Location.Y); // to move picture box from x coordinate by 100 Point.
    }

}
//试试这个//

picturebox1.位置=0,0