JMC*_*125 3 f# drawing winforms
我试图System.Windows.Forms.Form在F#中绘制非自定义(我的意思是,只是创建默认表单类的实例,而不是我可以创建的派生类的实例).
我创建了一个自定义表单,但我不需要也不想要一个新的复杂结构,所以我删除了它,它简化了代码; 这么多,它停止显示图像.
问题必须出在我创建的函数中,即在另一个F#项目中.我已经创建它(函数conect)来按照它们提供的顺序连接点,不同的是System.Drawing.Graphics.DrawLines,它以某些其他顺序在点之间绘制线条为什么还没有注意到(可能是从右到左,从上到下,因为点是表示).
Programa.fs相关代码片段:
let pen = new Pen(brush = Brushes.Black, width = 1.0f)
let original =
([|new PointF(50.0f, 50.0f); new PointF(100.0f, 50.0f)|])
use form1 = new Form(Width = 400, Height = 400, Text = "Fractais (Teste - Windows Forms)")
form1.Paint.Add(
fun e -> // (1)
original
|> List.ofArray
|> Base.applyFractal 1uy Base.fractalFunc1
|> Base.conect e.Graphics pen)
Run Code Online (Sandbox Code Playgroud)
如果在lambda表达式而不是写入的内容中e.Graphics.DrawLines(pen, original),它将在列表中的点之间绘制一条简单的线.
以下是整个解决方案中Basework中的故障排除方法:
let conect (gr:Graphics) (pen:Pen) (points:PointF list) =
let rec usefulFunc (gr:Graphics) (pen:Pen) (points:PointF list) prevPoint =
match points with
| [] -> ()
| point :: remainings ->
gr.DrawLine (pen, prevPoint, point)
usefulFunc gr caneta remainings.Tail remainings.Head
usefulFunc gr pen points.Tail points.Head
Run Code Online (Sandbox Code Playgroud)
在Base.fsi中调用(来自表单初始化片段)和相关方法的签名(我可以给你所有完整方法的实现,但它会占用很多空间,这可能已经成为你已经成为一个很长的问题):
val fractalFunc1 : points:PointF list -> PointF list
val applyFractal : stepNumber:byte -> fractalFunc:(PointF list -> PointF list) -> points:PointF list -> PointF list
val conect : gr:Graphics -> pen:Pen -> points:PointF list -> unit
Run Code Online (Sandbox Code Playgroud)
对于这个特定问题,我的搜索结果都没有.我想知道如何使功能发挥conect作用.
提前致谢.
你在conectar中有一个错误.
fUtil gr caneta resto.Tail resto.Head
Run Code Online (Sandbox Code Playgroud)
应该
fUtil gr caneta resto ponto
Run Code Online (Sandbox Code Playgroud)
你已经匹配了匹配语句中的head和tail.
以下代码为我画了一条线.我没有太多修改.
open System.Drawing
open System.Windows.Forms
let caneta = new Pen(brush = Brushes.Black, width = 1.0f)
let original =
([|new PointF(50.0f, 50.0f); new PointF(100.0f, 50.0f)|])
let form1 = new Form(Width = 400, Height = 400, Text = "Fractais (Teste - Windows Forms)")
let conectar (gr:Graphics) (caneta:Pen) (pontos:PointF list) =
let rec fUtil (gr:Graphics) (caneta:Pen) (pontos:PointF list) pontoAnt =
match pontos with
| [] -> ()
| ponto :: resto ->
gr.DrawLine (caneta, pontoAnt, ponto)
fUtil gr caneta resto ponto
fUtil gr caneta pontos.Tail pontos.Head
form1.Paint.Add(
fun e -> // (1)
original
|> List.ofArray
//|> aplicFractal 1uy Base.funcFractal1
|> conectar e.Graphics caneta)
form1.Show()
Application.Run(form1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1801 次 |
| 最近记录: |