5

我正在尝试使用Racket 的 GUI 工具包,并试图在屏幕上使用 ASCII 艺术获得一种很酷的变色效果。我有一个回调可以改变颜色,但我似乎无法弄清楚如何调用它来获得适当的结果。

起初我尝试使用消息控件来绘制文本,然后从那里更改文本颜色,但据我所知,没有办法更改消息的文本颜色。所以我继续使用canvas subwindow,但我似乎无法一次绘制多行文本;换行符刚刚被删除,ascii 艺术显示为一长行文本。此外,我用来更改颜色的 on-paint 回调似乎只在调整窗口大小或以其他方式更改时被调用,因此如果用户坐着观看,颜色不会改变。

如果我能找到一种方法在画布的每一帧上进行回调,我可以遍历一个文本字符串列表并将每个字符串绘制到屏幕上,垂直分隔以呈现换行符的外观,但我没有不知道画布是否存在这样的“帧内”调用。

关于如何使它工作的任何想法?

4

2 回答 2

7

Probably the easiest widget to use here is the text% class (you'll need an editor-canvas% for it too). Look at change-style and related methods to see how to change the styling of a particular piece of text.

Example:

#lang racket/gui

(define frame (new frame% [label "Test"] [width 300] 
                                         [height 300]))
(define text (new text%))
(define canvas (new editor-canvas% [parent frame] 
                                   [editor text]))

(define style-delta (make-object style-delta% 
                                 'change-normal-color))

;; do some red
(send style-delta set-delta-foreground "red")
(send text change-style style-delta)
(send text insert "Hello world in red\n")

;; do some blue
(send style-delta set-delta-foreground "blue")
(send text change-style style-delta)
(send text insert "Now available in blue")

(send frame show #t)
于 2012-08-21T02:57:38.783 回答
0

您不必指定更改命令。

更简单的一个:

#lang racket/gui

(define frame (new frame% [label "Test"] [width 300] 
                                         [height 300]))
(define text (new text%))
(define canvas (new editor-canvas% [parent frame] 
                                   [editor text]))

(define style-delta (make-object style-delta%))

;; do some red
(send style-delta set-delta-foreground "red")
(send text change-style style-delta)
(send text insert "Hello world in red\n")

;; do some blue
(send style-delta set-delta-foreground "blue")
(send text change-style style-delta)
(send text insert "Now available in blue\n")

;; do some modern
(send style-delta set-delta-foreground "black")
(send style-delta set-family 'modern)
(send text change-style style-delta)
(send text insert "Now available in modern\n(fixed width)")

(send frame show #t)
于 2015-01-14T01:41:32.647 回答