문제

How do I override the MouseMove (or any mouse event for that matter) in F#, similar to how it's done in C#? Meaning I want to write something like this, just to get the current mouse coordinates.

override form.MouseMove e = 
    mouseX = e.X
    mouseY = e.Y

Edit: basically I want to access the MouseEventArgs, and in this code the argument e is interpreted as PaintEventArgs instead.

Second Edit: This code actually works, just had another code block with an override on the OnPaint event, so apparently I couldn't use the same event argument variable (e) for both overrides in F#.

도움이 되었습니까?

해결책 2

I think the method you want is called OnMouseMove.

type MyForm() =
    inherit Form()
    override this.OnMouseMove(e) = printfn "Mouse is at %d %d" e.X e.Y

다른 팁

As an alternative to overriding the virtual method in a derived class, you could use a F# object expression to achieve the same result. This is useful to create an instance of overridden behavior without the need to define a derived class.

let mutable mouseLoc = Point()
let myForm = {
    new Form() with
        override __.OnMouseMove args =
            mouseLoc <- args.Location }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top