Screen (Console)

FreeBASIC

Screen (Console)
 
Gets the character or color attribute at a given location

Syntax

Declare Function Screen ( ByVal row As Long, ByVal column As Long, ByVal colorflag As Long = 0 ) As Long

Usage

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

Parameters

row
1-based offset from the top left corner of the console.
column
1-based offset from the top left corner of the console.
colorflag
If equal to 0, the ASCII code is returned, otherwise the color attribute is returned. If omitted, it defaults to 0.

Return Value

The ASCII or color attribute of the character.

Description

Screen returns the character or the color attribute found at a given position of a console output. It works in console mode and in graphics mode.

The format of the color attribute depends on the current color depth:

If the color type is a palette type with up to 4 bits per pixel (such as the Win32 console), then the color attribute is an 8-bit value, where the higher four bits hold the cell background color and the lower four bits hold the foreground (character) color.

If the color type is an 8-bit palette, then the color attribute is a 16-bit value, where the high byte holds the background color and the low byte holds the foreground color.

If the color type is full color, then the color attribute is a 32-bit integer, holding a single color value. If colorflag is equal to 1, then the foreground color is returned; if colorflag is equal to 2, then the background color is returned.


The color values for the standard 16 color palette are:

ValueColorValueColor
0Black8Gray
1Blue9Bright Blue
2Green10Bright Green
3Cyan11Bright Cyan
4Red12Bright Red
5Magenta13Pink
6Brown14Yellow
7White15Bright White


Example

Dim character_ascii_value As Integer
Dim attribute As Integer
Dim background As Integer
Dim cell_color As Integer
Dim row As Integer, col As Integer

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


'' 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 UInteger 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


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

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

Dim As UInteger 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


'' 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 Integer 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

Platform Differences

  • On the Linux version, the value returned can differ from the character shown on the console. For example, unprintable control codes - such as the LF character (10) that implicitly occurs after the end of Printed text - may be picked up instead of the untouched character in its place.

Differences from QB

  • In QB Screen triggered an error if the coordinates were out of screen.

See also