Skip to content

DRAW STRING


将文本渲染到图像或屏幕的图形语句。

语法

Draw String [buffer,] [STEP] (x, y), text [,color [, font [, method [, (alpha|blender) [, parameter] ] ] ] ]

用法

Draw String [buffer,] [STEP] (x, y), text [, color]
Draw String [buffer,] [STEP] (x, y), text , , font [, method [, alpha ] ]
Draw String [buffer,] [STEP] (x, y), text , , font, Custom, blender [, parameter]

参数

buffer

要绘制字符串的精灵。如果未提供,将绘制到屏幕上。

STEP

使用相对坐标。如果添加 STEP,x 和 y 坐标相对于最后绘制的点进行转换。

xy

要绘制到的水平/垂直位置,相对于屏幕左上角(除非使用了 STEP — 见上文)。文本的左上角将绘制在此位置。

text

包含要绘制文本的字符串

color

如果未提供字体,允许选择文本颜色。如果省略,使用默认前景 Color

如果提供了字体,color 将被忽略,字体本身为每个像素指定颜色。

font

包含自定义字体的图像缓冲区。如果未提供字体,使用当前文本分辨率的标准字体,且以下参数将被忽略。

method | Custom

指定字体字符如何绘制到目标表面上。与 Put 语句中的方法相同,唯一区别是此函数的默认方法为 Trans。此参数仅适用于自定义字体。

alpha

alpha 值,范围 0-255。此参数仅适用于 AddAlpha 方法。

blender

Custom 绘制方法的自定义混合函数;详见 Put (Graphics) 语句说明。此参数仅适用于 Custom 方法。

parameter

可选的指针,传递给自定义混合函数;如果省略,默认值为零(0)。

说明

此图形关键字以像素精度将字符串打印到屏幕,背景透明,可使用用户提供的字体。Draw String 不更新任何文本或图形光标,不会在行末换行。制表符、回车符和其他特殊字符在 Draw String 中没有特殊行为,均被视为普通字符。

在图形模式下,此函数提供了 Print 的灵活替代方案。它具有以下几个主要优势:

  • Draw String 可以将文本打印到屏幕上的任意坐标,而 Print 受限于 Locate 可访问的字符网格。
  • Print 会用当前背景颜色覆盖文本背后的背景。Draw String 不会这样做:它保留背景像素不变。
  • Put 一样,Draw String 有多种不同的文本打印方法,例如 AlphaCustom
  • Draw String 不限于单一字符集:可以提供自定义字体来替代使用。

注意:如果未提供自定义字体,Draw String 将默认使用 Print 使用的标准字体,字符大小由 Width 决定。即使传递了 method,也会被忽略,文本将使用提供的颜色以透明背景绘制。

Draw String 坐标受 WindowView (Graphics) 语句设置的自定义坐标系影响,绘制的文本遵守 View (Graphics) 设置的裁剪矩形。

自定义字体格式:

字体存储在标准 Get/Put 缓冲区中;字体必须使用与当前颜色深度相同深度的缓冲区存储,否则 Draw String 将以非法函数调用运行时错误退出。

字体缓冲区的第一行像素按字节(而非像素)方式存储字体头。第一个字节标识字体头版本,目前必须为 0。第二个字节给出字体中第一个支持字符的 ASCII 码;第三个字节给出最后一个支持字符的 ASCII 码。因此,如果字体支持完整范围 0-255,这两个字节的内容将是 0255

接下来是每个支持字符的宽度,每个占一个字节。假设字体包含 96 个字符,范围从 32 到 127(含),头部将有前三个字节存储 032127,后跟 96 个字节给出对应字符的宽度。

字体高度通过将缓冲区高度减 1 获得,即缓冲区的第一行像素作为字体头,其余行定义字形布局。缓冲区必须足够宽,以在同一行中依次容纳所有支持字符的精灵。

示例

此示例展示了基本的 Draw String 用法:在屏幕中央打印 "Hello world"

start GeSHi

vb
Const w = 320, h = 200 '' screen dimensions

Dim x As Integer, y As Integer, s As String

'' Open a graphics window
ScreenRes w, h

'' Draw a string in the centre of the screen:

s = "Hello world"
x = (w - Len(s) * 8) \ 2
y = (h - 1 * 8) \ 2

Draw String (x, y), s

'' Wait for a keypress before ending the program
Sleep

end GeSHi

此示例展示如何创建和使用自定义字体。为简单起见,使用 Draw String 的默认字体创建字形。

start GeSHi

vb
'' Define character range
Const FIRSTCHAR = 32, LASTCHAR = 127

Const NUMCHARS = (LASTCHAR - FIRSTCHAR) + 1
Dim As UByte Ptr p, myFont
Dim As Integer i

'' Open a 256 color graphics screen (320*200)
ScreenRes 320, 200, 8

'' Create custom font into PUT buffer

myFont = ImageCreate(NUMCHARS * 8, 9)

 '' Put font header at start of pixel data

#ifndef ImageInfo '' older versions of FB don't have the ImageInfo feature
p = myFont + IIf(myFont[0] = 7, 32, 4)
#else
ImageInfo( myFont, , , , , p )
#endif

p[0] = 0
p[1] = FIRSTCHAR
p[2] = LASTCHAR

 '' PUT each character into the font and update width information
For i = FIRSTCHAR To LASTCHAR
   
    '' Here we could define a custom width for each letter, but for simplicity we use
    '' a fixed width of 8 since we are reusing the default font glyphs
    p[3 + i - FIRSTCHAR] = 8
   
    '' Create character onto custom font buffer by drawing using default font
    Draw String myFont, ((i - FIRSTCHAR) * 8, 1), Chr(i), 32 + (i Mod 24) + 24
   
Next i

'' Now the font buffer is ready; we could save it using BSAVE for later use
Rem BSave "myfont.bmp", myFont

'' Here we draw a string using the custom font
Draw String (10, 10), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", , myFont
Draw String (10, 26), "abcdefghijklmnopqrstuvwxyz", , myFont
Draw String (66, 58), "Hello world!", , myFont

'' Free the font from memory, now we are done with it
ImageDestroy myFont

Sleep

end GeSHi

与 QB 的区别

  • FreeBASIC 新增

另请参阅

  • Print
  • ?
  • Draw
  • ImageCreate
  • ImageDestroy
  • ImageInfo
  • Put (Graphics)
  • Width

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系