dr drcket中的位图

时间:2011-03-18 16:51:16

标签: racket

如何在dr-racket中的帧(gui)上加载位图??? 请提供必要的代码和参考....

1 个答案:

答案 0 :(得分:5)

我承认,我很难在文档中找到合适的位置指向您。这里有一些代码可以做到这一点,但我有一种潜在的怀疑,认为有一种更简单的方法来完成这项工作:

#lang racket

(require racket/draw
         mred)

;; define a canvas that displays a bitmap when its on-paint
;; method is called
(define bitmap-canvas%
  (class canvas%
    (init-field [bitmap #f])
    (inherit get-dc)
    (define/override (on-paint)
      (send (get-dc) draw-bitmap bitmap 0 0))
    (super-new)))

;; load the bitmap
(define bitmap (read-bitmap "/tmp/red-arrow.bmp"))

;; create a new frame (top-level window)
(define f (new frame% [label "foo"] [width 100] [height 100]))

;; create a canvas
(define the-canvas (new bitmap-canvas% [parent f] [bitmap bitmap]))

;; show the canvas
(send f show #t)

更新:Matthew Flatt建议:你可以使用`message%'和位图作为标签。这肯定是很少的代码。这取决于你的目标。

#lang racket/gui

(define bitmap (read-bitmap "/tmp/red-arrow.bmp"))

(define f (new frame% [label "Bitmap"]))
(new message% [parent f] [label bitmap])
(send f show #t)