Winforms 将图形带到前端F#

Winforms 将图形带到前端F#,winforms,f#,Winforms,F#,我正在做一个项目,你应该能够在windows窗体环境中创建形状。我现在有两种不同的形状,叫做圆形和矩形,它们就是它们所说的,具有相似的性质 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 memb

我正在做一个项目,你应该能够在windows窗体环境中创建形状。我现在有两种不同的形状,叫做圆形和矩形,它们就是它们所说的,具有相似的性质

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)
键入矩形1(x:int,y:int,brush1)=
设可变thisx=x
设可变thisy=y
设可变宽度=50
让可变高度=20
让可变画笔=画笔1

使用get()=thisx和set x=thisx成员obj.x的基本思想是:可以向
Rectangle1
类添加
z
坐标。当一个矩形被击中时,确保它得到最高的
z
值。在绘制矩形之前,确保它们是按升序
z
值排序的。

基本思想:您可以向
Rectangle1
类添加
z
坐标。当一个矩形被击中时,确保它得到最高的
z
值。在绘制矩形之前,请确保它们按升序
z
值进行排序。

根据命中测试功能,您的
矩形列表中的矩形存储顺序似乎与它们在屏幕上的显示顺序相反(首先对开始处的矩形进行命中测试,因此它将位于图形顶部)

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

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

根据命中测试功能,您的
矩形列表
存储矩形的顺序似乎与它们在屏幕上的显示顺序相反(开始处的矩形首先被命中测试,因此它将位于图形顶部)

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

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

Wmeyer和Tomas的答案很好地满足了您对相对较小的矩形集的要求。如果您将使用10^3或更多的矩形,并且在启动GUI之前知道它们的坐标,则有简单的静态结构。在更复杂的情况下,请参阅“空间数据结构的设计和分析”的第3章Hanan Samet是您最好的朋友。

Wmeyer和Tomas的答案很好地满足了您对相对较小的矩形集的要求。如果您将使用10^3或更多的矩形,并且在启动GUI之前知道它们的坐标,则有简单的静态结构。在更复杂的情况下,请参阅第3章Hanan Samet的“空间数据结构的设计和分析”是您最好的朋友

let BringToFront value list = 
  value :: (List.filter (fun v -> v <> value) list)
BringToFront 3 [ 1;2;3;4 ] = [3;1;2;4]