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

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

最佳答案

根本不清楚您想做什么。但是您的注释代码至少有两个问题:

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 中使用它。

关于c# - 为什么我的计算不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14475061/

相关文章:

Javascript:捕获鼠标滚轮事件并且不滚动页面?

Jquery可排序的鼠标偏移量

javascript - 我无法在 HTML5 游戏中获得鼠标输入

c++ - 多显示器环境中的鼠标环绕

c# - 从后面的 ASP 代码将参数传递给 Javascript 函数

c# - 如何使用 LINQ 从多列列表中获取 x 行

javascript - 如何根据容器内的鼠标位置正确滚动溢出的 div

javascript 在处理程序中删除事件,

c# - Visual Studio 2010 自动化和环境变量

c# - MonoTouch 电池电量始终为 -1