F#和Winforms

F#和Winforms,winforms,graphics,f#,Winforms,Graphics,F#,我有一个这样的程序 let form = new Form() let drawArea = new Panel(Location = new Point(200,0), Height = 600, Width = 800) let rectBrush = new SolidBrush(Color.Blue) form.Controls.Add(drawArea) drawArea.MouseClick.Add(fun args -> drawArea.Paint.Add(fu

我有一个这样的程序

let form = new Form()
let drawArea = new Panel(Location = new Point(200,0), Height = 600, Width = 800)
let rectBrush = new SolidBrush(Color.Blue)
form.Controls.Add(drawArea)

drawArea.MouseClick.Add(fun args -> 
    drawArea.Paint.Add(fun e -> 
        e.Graphics.FillRectangle(rectBrush, args.X, args.Y, 50, 50)))

Application.Run(form)
因此,当我单击时,会出现一个蓝色矩形。但是,这些矩形存储在哪里?有没有办法检索“绘图区域”中所有矩形的列表

否则,是否有方法将矩形作为子控制器添加到面板或simular winform对象


谢谢

我不知道用那种方式检索这些矩形的方法

最好是创建一个矩形类,并使其从控件或用户控件继承。然后重写它的受保护OnPaint(…)方法。例如:

public class FilledRectangle : UserControl
{
     private readonly float x, y, w, h;
     // also the brush here

     public FilledRectangle(float x, float y, float w, float h)
     {
          this.x = x;
          // ...
     }

     protected override void OnPaint(PaintEventArgs e) // not sure about the event args type name
     {
          e.Graphics.FillRectangle(this.myBrush, this.x, this.y, this.w, this.h);
     }
}
现在,您可以简单地将此类型的对象添加到控件集合,如下所示:

Controls.Add(new FilledRectangle(...));
使用这种方法,您可以轻松地创建矩形对象的集合,并使它们的行为符合您的要求

请注意,我上面写的示例是用C语言编写的,但是将其移植到F语言应该不会太困难。我不这么做的唯一原因是我不太熟悉它的语法。你的问题不是针对F,而是针对.NET


另请注意,正如Hans所评论的,在调用Invalidate()之前,绘制事件不会触发。

Ugh,每次鼠标单击都会添加一个新的绘制事件处理程序。不,您无法检索写入的矩形。只需让MouseClick处理程序向列表中添加一个矩形。然后调用form.Invalidate来触发重新绘制,这是现在无法工作的。并使用一个绘制事件处理程序来迭代该列表。顺便说一句,这背后的原因是Winforms使用“立即模式”绘图。其他框架,如WPF,使用保留模式,即它们显式地存储可见结构以供以后重用。也见啊哈!感谢您提供的信息,我不知道表单使用的是即时模式。感谢您提供的信息,这可能是正确的方法。你的回答和评论已经满足了我的需要。