我正在做一个名为“2dShapeEditor”的项目,您应该能够在表单上创建、单击和拖动不同类型的形状。在我现在所处的项目阶段,我需要确定我的目标矩形是哪种类型。它被初始化为“选项”,但可以通过“hittest”更改单击的特定矩形。
然后,我将我的 move 方法应用到这个矩形,使其随鼠标移动。
我的类型 RectangleZ
type RectangleZ(x:int, y:int)=
let mutable thisx = x
let mutable thisy = y
let mutable thiswidth = 50
let mutable thisheight = 20
let brush = new SolidBrush(Color.Black)
member obj.x with get () = thisx and set x = thisx <- x
member obj.y with get () = thisy and set y = thisy <- y
member obj.width with get () = thiswidth and set width = thiswidth <- width
member obj.height with get () = thisheight and set height = thisheight <- height
member obj.thisColor = Color.FromArgb(167, 198, 253)
member obj.draw(paper:Graphics) = paper.FillRectangle(brush, thisx, thisy, 50, 20)
member obj.ShapeType = "Rectangle"
我最热门的方法:
let rec getShape (e:MouseEventArgs) (inputl:List<RectangleZ>) = match inputl with
|[] -> None
|s::tail->if(((e.X >= s.x) && (s.x <= (s.x + s.width))) && ((e.Y >= s.y) && (e.Y <= (s.y + s.height)))) then Some(s) else getShape e tail //Some(s)
我的目标矩形如下所示:
let mutable targetedRec = None
我的“Move()”方法如下所示:
let Move (e:MouseEventArgs) = if(targetedRec = None) then None else targetedRec.Value.x <- e.X
targetedRec.Value.y <- e.Y
“Move()”方法给了我错误:“基于此程序点之前的信息查找不确定类型的对象。在此程序之前可能需要类型注释来约束对象的类型。这可能允许查找要解决。”
是的,编译器提示我出了什么问题以及如何修复它,我尝试过匹配 if 语句,但我根本不明白什么是错误的以及如何修复它。如果我删除了targetedRec 的Option 类型并将其作为“RectangleZ”类型,我的很多项目都会失败,因为我不能留空if 语句。有什么建议么?