为什么我的计算不起作用?

时间:2013-01-23 08:11:39

标签: c# mouse mousemove

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

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

1 个答案:

答案 0 :(得分: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中使用。