2

这更像是一个为什么这个工作不起作用的问题。我显然必须遗漏一些东西,因为我一生都无法弄清楚为什么计算宽度和高度值会给我与开始时相同的静态值。但是当我替换硬编码值时,它工作得很好。所以很明显我的计算很糟糕,但我似乎无法弄清楚为什么。任何人都可以帮忙吗?如果您需要更多信息,请询问。

 if(objectList[selectedIndex].Selected == true)
 {
      //The width would be mouselocationx - objectlocationx
      //The height would be mouselocationy - objectlocationy
      //Off set the position so it doesnt snap to mouse location
      //WHY THE *%$!! DOESNT THIS WORK!!?!?!?!?!?!?
      //int width = e.X - objectList[selectedIndex].X;
      //int height = e.Y - objectList[selectedIndex].Y;
      //Why is this so hard??
      //Point location = objectList[selectedIndex].Location;
      //location.X = e.X - (e.X - objectList[selectedIndex].Location.X);
      //location.Y = e.Y - (e.Y - objectList[selectedIndex].Location.Y);
      objectList[selectedIndex].X = e.Location.X - 12;
      objectList[selectedIndex].Y = e.Location.Y - 12;
      objectLocationXLabel.Text = "OBJX: " + objectList[selectedIndex].X.ToString();
      objectLocationYLabel.Text = "OBJY: " + objectList[selectedIndex].Y.ToString();
      panel1.Invalidate();
   }
4

1 回答 1

1

完全不清楚你想做什么。但是您的注释代码至少存在两个问题:

location.X = e.X - (e.X - objectList[selectedIndex].Location.X);

相当于

location.X = objectList[selectedIndex].Location.X;

Point是一个值类型。所以当你这样做时:

Point location = objectList[selectedIndex].Location;
location.X = ...;

location已修改,但未修改objectList[selectedIndex].Location

编辑以回应评论:我认为您必须确定MouseDown事件中的偏移量,将其存储在成员中并在MouseMove.

于 2013-01-23T08:22:13.137 回答