在F#中使用Gtk#DrawingArea:未定义ExposeEvent

时间:2016-05-20 08:28:15

标签: f# gtk#

设置:mono 4.5,linux,f#4.0,gtk#

这是我的代码,主要是从示例代码段中复制的:

open System
open Gtk

let (width, height) = (800, 600)

[<EntryPoint>]
let main argv =
  Application.Init ()
  let window = new Window ("helloworld")

  window.SetDefaultSize(width, height)
  window.DeleteEvent.Add(fun e -> window.Hide(); Application.Quit(); e.RetVal <- true)

  let drawing = new Gtk.DrawingArea () 
  drawing.ExposeEvent.Add(fun x ->
    let gc = drawing.Style.BaseGC(StateType.Normal)
    let allocColor (r,g,b) =
       let col = ref (Gdk.Color(r,g,b))
       let _ = gc.Colormap.AllocColor(col, true, true)
       !col
    gc.Foreground <- allocColor (255uy, 0uy, 0uy)
    drawing.GdkWindow.DrawLine(gc, 0, 0, 100, 100)
    )
  window.Add(drawing)
  window.ShowAll()
  window.Show()
  Application.Run ()
  0

无法使用以下错误进行编译:

The field, constructor or member 'ExposeEvent' is not defined

1 个答案:

答案 0 :(得分:0)

原来是gtk2 -> gtk3 difference。这是更新的代码 - DrawingArea现在发出Drawing而不是ExposeEvent

open System
open Gtk
open Cairo

let (width, height) = (800, 600)

[<EntryPoint>]
let main argv =
  Application.Init ()
  let window = new Window ("helloworld")

  window.SetDefaultSize(width, height)
  window.DeleteEvent.Add(fun e -> window.Hide(); Application.Quit(); e.RetVal <- true)

  let drawing = new Gtk.DrawingArea () 
  drawing.Drawn.Add(fun args ->
    let cr = args.Cr
    cr.MoveTo(0.0, 0.0)
    cr.LineTo(100.0, 100.0)
    cr.LineWidth = 1.0
    cr.Stroke ()
    ) 
  window.Add(drawing)
  window.ShowAll()
  window.Show()
  Application.Run ()