将图形带到前面F#

时间:2013-01-05 16:32:09

标签: winforms f#

我正在做一个项目,你应该能够在Windows窗体环境中创建形状。我现在有两种不同的形状,叫做Circle1和Rectangle1,它们就像它们所谓的具有类似属性。

type Rectangle1(x:int, y:int,brush1)=
  let mutable thisx = x
  let mutable thisy = y
  let mutable thiswidth = 50
  let mutable thisheight = 20
  let mutable brush = brush1
  member obj.x with get () = thisx and set x = thisx <- x
  member oby.y with get () = thisy and set y = thisy <- y
  member obj.brush1 with get () = brush and set brush1 = brush <- brush1
  member obj.width with get () = thiswidth and set width = thiswidth <- width
  member obj.height with get () = thisheight and set height = thisheight <- height
  member obj.draw(g:Graphics) = g.FillRectangle(brush,thisx,thisy,thiswidth,thisheight)

此矩形可点击且可移动,但我遇到了问题。我需要某种类似于c#bringToFront()方法的方法。因此,当我点击一个形状时,我的形状会移到所有其他形状的前面。

我的存储列表如下所示:

let mutable RectangleList:List<Rectangle1> =  []

我使用hittest来判断用户是否遇到了形状:

let rec VilketObjRec (e:MouseEventArgs) (inputlist:List<Rectangle1>) = 
match inputlist with
     |[] -> None
     |head::tail -> if (((e.X >= head.x) && (e.X <= (head.x + head.width))) && (e.Y >= head.y) && (e.Y <= (head.y+head.height))) 
                       then Some(head) else VilketObjRec e tail

任何人都有任何想法如何解决这个问题?坦率地说,我迷路了。

3 个答案:

答案 0 :(得分:3)

根据命中测试功能,您的RectangleList似乎以与它们在屏幕上显示的顺序相反的顺序存储矩形(开始时的矩形首先进行测试,因此它将会是图纸顶部的那个。)

在这种情况下,如果要将矩形置于顶部,只需将其移动到列表的开头即可。您可以在开头创建一个具有指定值的新列表,然后使用filter从列表的其余部分中删除该值:

let BringToFront value list = 
  value :: (List.filter (fun v -> v <> value) list)

该函数适用于anly列表,所以这是一个使用整数的例子:

BringToFront 3 [ 1;2;3;4 ] = [3;1;2;4]

答案 1 :(得分:2)

Wmeyer和Tomas的答案非常符合您对相对较小的矩形集的要求。如果你将使用10 ^ 3和更多的矩形,并且在GUI启动之前知道它们的坐标,那么就有简单的静态结构enter link description here。在更复杂的情况下,Hanan Samet的“空间数据结构的设计和分析”的第3章是你最好的朋友。

答案 2 :(得分:1)

基本想法:您可以在z课程中添加Rectangle1坐标。点击矩形时,请确保它获得最高z值。在绘制矩形之前,请确保它们按升序z值进行排序。