Skip to content

SCREEN(控制台)


获取指定位置的字符或颜色属性

语法

declare function Screen ( byval row as long, byval column as long, byval colorflag as long = 0 ) as ulong

用法

result = Screen( row, column [, colorflag ] )

参数

row

从控制台左上角开始的以1为基的偏移量。

column

从控制台左上角开始的以1为基的偏移量。

colorflag

若等于0,返回 ASCII 码;否则返回颜色属性。若省略,默认为0。

返回值

字符的 ASCII 码或颜色属性。

说明

Screen 返回控制台输出中给定位置的字符或颜色属性。在控制台模式和图形模式下均可使用。

颜色属性的格式取决于当前颜色深度:

若颜色类型为每像素最多4位的调色板类型(如 Win32 控制台),则颜色属性为8位值,高4位保存单元格背景色,低4位保存前景色(字符颜色)。

若颜色类型为8位调色板,则颜色属性为16位值,高字节保存背景色,低字节保存前景色。

若颜色类型为全彩色,则颜色属性为32位整数,保存单一颜色值。若 colorflag 等于1,则返回前景色;若 colorflag 等于2,则返回背景色。

标准16色调色板的颜色值为:

颜色颜色
0黑色8灰色
1蓝色9亮蓝色
2绿色10亮绿色
3青色11亮青色
4红色12亮红色
5品红13粉红色
6棕色14黄色
7白色15亮白色

示例

start GeSHi

vb
Dim character_ascii_value As ulong
Dim attribute As ulong
Dim background As ulong
Dim cell_color As ulong
Dim row As Long, col As Long

character_ascii_value = Screen( row, col )
attribute = Screen( row, col, 1 )
background = attribute Shr 4
cell_color = attribute And &hf

end GeSHi

start GeSHi

vb
'' open a graphics screen with 4 bits per pixel
'' (alternatively, omit this line to use the console)
ScreenRes 320, 200, 4

'' print a character
Color 7, 1
Print "A"

Dim As ulong char, col, fg, bg

'' get the ASCII value of the character we've just printed
char = Screen(1, 1, 0)

''get the color attributes
col = Screen(1, 1, 1)
fg = col And &HF
bg = (col Shr 4) And &HF

Print Using "ASCII value: ### (""!"")"; char; Chr(char)
Print Using "Foreground color: ##"; fg
Print Using "Background color: ##"; bg
Sleep

end GeSHi

start GeSHi

vb
'' open a graphics screen with 8 bits per pixel
ScreenRes 320, 200, 8

'' print a character
Color 30, 16
Print "Z"

Dim As ulong char, col, fg, bg

'' get the ASCII value of the character we've just printed
char = Screen(1, 1, 0)

''get the color attributes
col = Screen(1, 1, 1)
fg = col And &HFF
bg = (col Shr 8) And &HFF

Print Using "ASCII value: ### (""!"")"; char; Chr(char)
Print Using "Foreground color: ###"; fg
Print Using "Background color: ###"; bg
Sleep

end GeSHi

start GeSHi

vb
'' open a full-color graphics screen
ScreenRes 320, 200, 32

'' print a character
Color RGB(255, 255, 0), RGB(0, 0, 255) 'yellow on blue
Print "M"

Dim As ulong char, fg, bg

'' get the ASCII value of the character we've just printed
char = Screen(1, 1, 0)

''get the color attributes
fg = Screen(1, 1, 1)
bg = Screen(1, 1, 2)

Print Using "ASCII value: ### (""!"")"; char; Chr(char)
Print Using "Foreground color: &"; Hex(fg, 8)
Print Using "Background color: &"; Hex(bg, 8)
Sleep

end GeSHi

平台差异

  • 在 Linux 版本中,返回值可能与控制台上显示的字符不同。例如,不可打印的控制字符——如 Print 文本末尾隐式出现的 LF 字符(10)——可能会被读取,而不是原本位于该位置的字符。

与 QB 的差异

  • 在 QB 中,若坐标超出屏幕范围,Screen 会触发错误。

另请参阅

  • Screen (Graphics)
  • Color

返回 目录

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