如何让一个矩形移动时,图片框被调整(How to make a rectangle move whe

2019-10-17 19:40发布

我有一个画面作为一个应用程序的背景图片框,具有所有的锚设定,因此它可以与表格调整大小。 在这个图片框,我创造了许多其他的事情,现在只有矩形。 我的一些X创建它们和Y坐标,这很好。 添加图片显示什么,我试图做的。 创建矩形实际上是有点淡蓝色的正方形。

但是,当我调整形式,例如我最大化,矩形停留在同一坐标,这当然AR其他地方的时刻(包括图像中的一部分,以节省空间): 我的问题是 - 我怎样才能使矩形“大棒”与相同的地方,因为它是调整大小时? 注 - 他们将不得不以后移动,每2秒左右的像,所以它不能是绝对静态的。

编辑:这里是一些代码创建矩形

        private void button1_Click(object sender, EventArgs e)
    {
        spawn = "aircraft";
        pictureBox1.Invalidate();
    }
private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        switch (spawn)
        {
            case "aircraft":
                 Point[] points = new Point[2];
                 Point bod = new Point(750, 280);
                 points[0] = bod;    
                 aircraft letadlo = new aircraft(605, 180, "KLM886", 180, e.Graphics);
                 aircrafts[0] = letadlo;
                 letadlo.points = points;
                 break;
                 ...

        public aircraft(int x, int y, string csign, int spd, Graphics g)
    {
        Pen p = new Pen(Color.Turquoise, 2);
        Rectangle r = new Rectangle(x, y, 5, 5);
        g.DrawRectangle(p, r);
        p.Dispose();

Answer 1:

一种选择是重新绘制在新的坐标,这是成正比的PictureBox的改变后大小的矩形。 例如:

oldX, oldY // old coordinates of the rectangle should be saved
oldPictureBoxWidth, oldPictureBoxHeight // should be saved too

//and on the PictureBox Paint event You have the new:
newPictureBoxWidth and newPictureBoxHeight

//the new coordinates of rectangle: (resize ratio is in brackets)
newX = oldX * (newPictureBoxWidth / oldPictureBoxWidth)
newY = oldY * (newPictureBoxHeight / oldPictureBoxHeight)


Answer 2:

我认为你有你的X从顶部的距离,y和底部之间计算%,如果表单重新调整大小,只需使用您%,并再次提请您的矩形!

为前:

X = 100的宽度200,从而100是1/2,因此50%因此,如果调整形式只是计算新的大小和(新尺寸* 50)/ 100

希望能帮助你。



文章来源: How to make a rectangle move when PictureBox is resized
标签: c# graphics