【问题标题】:Detect window closing OCaml Graphics检测窗口关闭 OCaml 图形
【发布时间】:2015-12-29 10:55:56
【问题描述】:

我没有在图形文档中找到有关窗口关闭检测的详细信息。如何检测它以便我可以在关闭时触发操作?

谢谢。

【问题讨论】:

    标签: events window ocaml


    【解决方案1】:

    使用简单的测试程序(包含在此答案的末尾)并手动关闭窗口告诉我们引发了以下异常:

    Fatal error: exception Graphics.Graphic_failure("fatal I/O error")
    

    这意味着我们可以使用异常处理程序来处理正在关闭的窗口:

    try
      (* Here goes the code opening and manipulating the window *)
    with
      | Graphic_failure("fatal I/O error") ->
      (* Here goes the code handling the window being closed manually*)
    

    测试程序(需要运行ocamlc graphics.cma test.ml -o test编译):

    open Graphics
    
    let rec loop () = loop ()
    let () =
    (*
      try
    *)
        Graphics.open_graph " 400x600";
        loop ()
    (*
      with
        | Graphic_failure("fatal I/O error") ->
          print_string "Caught exception";
          print_newline ()
    *)
    

    【讨论】:

    • 对我来说,它引发了Graphic_failure("Xlib error: BadDrawable (invalid Pixmap or Window parameter)")...我的应用程序也有一些图形,但受终端控制,有点像解释器,所以我没有事件循环。我仍然有一个命令循环,所以我可以在发送命令后捕获异常......所以一个临时解决方案是匹配 Graphic_failure("fatal I/O error")Graphic_failure("Xlib error: BadDrawable (invalid Pixmap or Window parameter)"),但我怎么知道我应该没有其他异常抓到?
    • 即使我尝试在主程序中捕获这些异常,也会导致致命错误。对于上面的Graphic_failure,我已经用try match 包围了启动命令(文本和图形)的代码,但它没有帮助。我也尝试过匹配Graphic_failure _,但仍然没有帮助......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2021-03-15
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多