PictureBox 从窗体 c# 出来

PictureBox goes out from the form c#

我需要在表单中移动一个图片框。图片框是Fantasma1.

这是我在计时器(移动图片框)计时时所做的事情:

private void Move_Tick(object sender, EventArgs e)
    {
        Completamento.Increment(1); 

        while (Fantasma1.Location.X < MAX.X)
              Fantasma1.Location = new System.Drawing.Point(Fantasma1.Location.X + 1, Fantasma1.Location.Y);

        Move.Stop();

        MessageBox.Show("Location: " + Fantasma1.Location.X + ";" + Fantasma1.Location.Y + ", larghezza del form: " + this.Width + ", dimensione della picture: " + Fantasma1.Width);
    }

在我做过的表单的构造函数中:

MAX.X = this.Width - Fantasma1.Size.Width;

我认为这个解决方案是正确的,但是我的图片框从表单中消失了。 谁能帮帮我?

首先,你的 Fantasma 将 运行 结束而你却看不到它移动,因为:

while (Fantasma1.Location.X < MAX.X)
          Fantasma1.Location = new System.Drawing.Point(Fantasma1.Location.X + 1, Fantasma1.Location.Y);

那个循环会立即将你的幽灵移到尽头,没有任何可见的变化,我想你想要的是通过 Move_Tick 调用移动一个像素,如果达到极限则停止计时器,并且你刚刚在第一次滴答时停止了计时器,这是另一个错误。

因此,如果您希望幽灵在每个刻度上移动一个像素并且不离开屏幕,那么这应该可以解决问题:

private void Move_Tick(object sender, EventArgs e)
{
    int max = this.Width - Fantasma1.Width;

    if(Fantasma.Left < max)
        Fantasma1.Left++;
    else
        Move.Stop();
}

在你的情况下,测试最大位置的方法是正确的,但我几乎可以肯定你试图在图片框加载图片之前获取尺寸(并且你设置了自动调整大小以允许图片框调整它的大小等于它的内容)所以为了避免错误我在公式上添加了最大计算,它是一个减法,它不消耗任何*过程。

*:几乎任何一行代码都需要花费 cpu,但减法可能不到一纳秒,只是不明显。