我正在尝试在mouseClick位置的中心周围绘制一个矩形。为此我觉得我需要获得x和y坐标作为int。
(这是已修改的代码,e.X
或e.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吗?
答案 0 :(得分:4)
如评论中所述,要从MouseEventArgs
获取鼠标位置,您只需访问X
或Y
个属性即可
仅反映Location.X
上提供的Location.Y
和e
属性
关于你的编辑和你的其他评论,我认为你通过每次点击添加一个新的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