2013-03-08 136 views
1

我有一个PictureBox,其图片作为应用程序的背景,使所有Anchors都设置好,因此可以使用该窗体调整大小。在这个PictureBox上,我创建了许多其他的东西,现在只有矩形。我在一些X和Y坐标上创建它们,这很好。添加图片以显示我正在尝试做的事情。创建的矩形实际上是小淡蓝色方块。 enter image description here如何在PictureBox调整大小时使矩形移动

但是,当我调整形式,例如我最大化,矩形停留在同一坐标,这当然AR其他地方的时刻(包括图像中的一部分,以节省空间):enter image description here 我的问题是 - 在调整大小的过程中,如何使矩形“粘”到与原来位置相同的位置?注意 - 他们必须稍后移动,如每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(); 
+0

挂钩到调整大小事件,并重新计算每次调整大小触发?也复制粘贴“设置”矩形位置的代码。它的重要性在于看你相关的设置 – squelos 2013-03-08 23:32:45

+0

位置是用绝对坐标值设置的。将在一秒钟内添加一些代码。 – 2013-03-08 23:33:34

+0

但绝对是什么?绝对的窗口还是屏幕?有很多绘画方法。 – squelos 2013-03-08 23:36:48

回答

3

一个选项可能是重新绘制与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) 
+0

其实这在我的情况下会比建议的百分比更有用。非常感谢! – 2013-03-08 23:46:24

1

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

用于离:

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

希望能帮助你。

相关问题