如何获取mouseClick的x和y坐标

时间:2016-11-26 09:18:59

标签: winforms f# coordinates

我正在尝试在mouseClick位置的中心周围绘制一个矩形。为此我觉得我需要获得x和y坐标作为int。

(这是已修改的代码,e.Xe.Y是此问题的解决方案)

  let mouseClick (e: MouseEventArgs) =
  let  x = e.X
  let  y = e.Y
  let coords = [|System.Drawing.Point((x-10),(y-10));
                 System.Drawing.Point((x-10),(y+10));
                 System.Drawing.Point((x+10),(y+10));
                 System.Drawing.Point((x+10),(y-10));
                 System.Drawing.Point((x-10),(y-10))|]
  window.Paint.Add(fun e -> e.Graphics.DrawLines(pen, coords ))

window.MouseClick.Add mouseClick

我尝试使用不起作用的e.Location属性,这在一定程度上是有意义的,因为当我打印它然后打印" x =(某个数字)y =(某个数字) "

任何人都可以帮我把x和y坐标作为int吗?

1 个答案:

答案 0 :(得分:4)

如评论中所述,要从MouseEventArgs获取鼠标位置,您只需访问XY个属性即可 仅反映Location.X上提供的Location.Ye属性

关于你的编辑和你的其他评论,我认为你通过每次点击添加一个新的Paint处理程序做错了,你只需要绘制(可能仍然需要Refresh at虽然有些观点)

let mouseClick (e: MouseEventArgs) =
  let  x = e.X
  let  y = e.Y
  let coords = [| System.Drawing.Point(x - 10, y - 10)
                  System.Drawing.Point(x - 10, y + 10)
                  System.Drawing.Point(x + 10, y + 10)
                  System.Drawing.Point(x + 10, y - 10)
                  System.Drawing.Point(x - 10, y - 10) |]

  // maybe use instead of let ?
  let g = window.CreateGraphics()
  g.Graphics.DrawLines(pen, coords)

window.MouseClick.Add mouseClick