如何限制Picturebox移动到屏幕

时间:2015-01-07 05:57:37

标签: c# winforms timer picturebox

我有一个应用程序,只要满足条件,它就会有一个由计时器控制的移动Picturebox。所有这一切都很好。但是,我很想将移动的Picturebox限制在屏幕内。

我的代码在这里:

public Random r = new Random();

private void OutsideBusinessHoursTimer_Tick(object sender, EventArgs e)
{
  int x = r.Next(0, 925);
  int y = r.Next(0, 445);
  this.pbLifebrokerInactive.Top = y;
  this.pbLifebrokerInactive.Left = x;
}

我怎样才能做到最好?我也可以在Timer事件中执行此操作吗?谢谢你的耐心! :)

1 个答案:

答案 0 :(得分:1)

不确定这是否属于您之后的情况,但这会将其限制在您的表单区域:

    private void OutsideBusinessHoursTimer_Tick(object sender, EventArgs e)
    {
        int x = r.Next(0, this.ClientRectangle.Width - this.pbLifebrokerInactive.Width);
        int y = r.Next(0, this.ClientRectangle.Height - this.pbLifebrokerInactive.Height);
        this.pbLifebrokerInactive.Location = new Point(x, y);
    }