簡體   English   中英

如何限制Picturebox在屏幕上的移動

[英]How to limit Picturebox movement to screen

我有一個應用程序,只要有條件滿足,它就有一個由計時器控制的動態Picturebox。 所有這些都很好。 但是,我希望將移動的Picturebox限制在屏幕內。

我的代碼在這里:

公共隨機數r =新的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事件中執行此操作嗎? 謝謝你的耐心! :)

不確定這是否是您的要求,但這會將其限制在表單的區域:

    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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM