如何检测图片框是否超出表单宽度

本文关键字:是否 表单 何检测 检测 | 更新日期: 2023-09-27 18:37:11

大家好,我已经搜索了一个答案,但我还没有找到对我有帮助的特定答案。所以我问这个问题。

我的问题是你如何检测你的图片框是否移动到窗口或表单之外。

我的计时器的每个滴答声都会:

picturebox.Left += 10;
if (picturebox.Left > this.Width)
{
    picturebox.Left = 0;
}

但是此代码仅转到右侧,并且仅检测图片框是否在窗体的右侧消失。

尝试过做一些事情,比如说我正在向左移动图片框,它从屏幕中消失了,这是我得到的代码,这不会给出任何错误,但它会如此快速地移动我的图像。

picturebox.Left -= 10;
if (picturebox.Left > this.width || picturebox.Left < this.Width)
{
    picturebox.Left = 0;
}

这段代码也对我不起作用:

picturebox.Right < this.Width  

请帮忙,谢谢

如何检测图片框是否超出表单宽度

这是一种方法...

    private int direction = -1;
    private void button1_Click(object sender, EventArgs e)
    {
        direction = direction * -1;
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        picturebox.Left += direction * 10;
        if (!this.ClientRectangle.IntersectsWith(picturebox.Bounds))
        {
            if (direction == -1)
                picturebox.Left = this.ClientRectangle.Width;
            else
                picturebox.Left = -picturebox.Width;
        }
    }

试试这个。

private bool IsControlInsideClientArea(Control c)
{
    return this.ClientRectangle.Contains(this.RectangleToClient(c.RectangleToScreen(c.ClientRectangle)));
}