使用自定义 OnPaint 在 F# 中自定义 Windows.Forms 控件?
Custom Windows.Forms control in F# with custom OnPaint?
我试图在 F# 中实现自定义 Windows.Forms 控件,但我的 "OnPaint" 方法似乎根本没有被调用(它没有显示,调试消息也没有打印安慰)。我做错了什么?
open System
open System.Drawing
open System.Windows.Forms
let form = new Form(Visible=true, Text="Drawing App", WindowState=FormWindowState.Maximized)
type Canvas() =
class
inherit Control()
override c.OnPaint(e:PaintEventArgs) =
System.Diagnostics.Debug.WriteLine("OnPaint")
base.OnPaint(e)
let g = e.Graphics
g.DrawLine(Pens.Blue, 0, 0, c.Width, c.Height)
end
System.Diagnostics.Debug.WriteLine("hello")
let canvas = new Canvas()
canvas.Visible <- true
form.Controls.Add(canvas)
[<STAThread>]
Application.Run(form)
如果我将 "let canvas ..." 块替换为下面的块,标签 会 显示在 window:
let label = new Label(Text="sample label")
form.Controls.Add(label)
线没有画出来,因为c.Width = c.Height = 0.
设置Canvas大小并得到结果:
canvas.Size <- Size (form.Width, form.Height)
编辑:
Canvas尺寸与表格相同,足以进行活动订阅:
form.SizeChanged.Add(fun e -> canvas.Size <- form.Size; canvas.Refresh())
受到的启发,这就是我现在所关注的:
open System
open System.Drawing
open System.Windows.Forms
let form = new Form(Visible=true, Text="Drawing App", WindowState=FormWindowState.Maximized)
type Canvas() =
inherit Control()
override c.OnPaint(e:PaintEventArgs) =
//System.Diagnostics.Debug.WriteLine("OnPaint")
base.OnPaint(e)
let g = e.Graphics
g.DrawLine(Pens.Blue, 0, 0, c.Width, c.Height)
override c.OnResize(e:EventArgs) =
c.Refresh()
let canvas = new Canvas(Dock = DockStyle.Fill)
form.Controls.Add(canvas)
[<STAThread>]
Application.Run(form)
似乎对我有用,而且在我看来更合适。我暂时将此标记为可接受的解决方案,但我仍然对可能的改进或其他建议感兴趣。
我试图在 F# 中实现自定义 Windows.Forms 控件,但我的 "OnPaint" 方法似乎根本没有被调用(它没有显示,调试消息也没有打印安慰)。我做错了什么?
open System
open System.Drawing
open System.Windows.Forms
let form = new Form(Visible=true, Text="Drawing App", WindowState=FormWindowState.Maximized)
type Canvas() =
class
inherit Control()
override c.OnPaint(e:PaintEventArgs) =
System.Diagnostics.Debug.WriteLine("OnPaint")
base.OnPaint(e)
let g = e.Graphics
g.DrawLine(Pens.Blue, 0, 0, c.Width, c.Height)
end
System.Diagnostics.Debug.WriteLine("hello")
let canvas = new Canvas()
canvas.Visible <- true
form.Controls.Add(canvas)
[<STAThread>]
Application.Run(form)
如果我将 "let canvas ..." 块替换为下面的块,标签 会 显示在 window:
let label = new Label(Text="sample label")
form.Controls.Add(label)
线没有画出来,因为c.Width = c.Height = 0.
设置Canvas大小并得到结果:
canvas.Size <- Size (form.Width, form.Height)
编辑:
Canvas尺寸与表格相同,足以进行活动订阅:
form.SizeChanged.Add(fun e -> canvas.Size <- form.Size; canvas.Refresh())
受到
open System
open System.Drawing
open System.Windows.Forms
let form = new Form(Visible=true, Text="Drawing App", WindowState=FormWindowState.Maximized)
type Canvas() =
inherit Control()
override c.OnPaint(e:PaintEventArgs) =
//System.Diagnostics.Debug.WriteLine("OnPaint")
base.OnPaint(e)
let g = e.Graphics
g.DrawLine(Pens.Blue, 0, 0, c.Width, c.Height)
override c.OnResize(e:EventArgs) =
c.Refresh()
let canvas = new Canvas(Dock = DockStyle.Fill)
form.Controls.Add(canvas)
[<STAThread>]
Application.Run(form)
似乎对我有用,而且在我看来更合适。我暂时将此标记为可接受的解决方案,但我仍然对可能的改进或其他建议感兴趣。