Article ID: 29943 - Last Review: November 21, 2006 - Revision: 1.2

SCREEN Function Gives COLOR Attribute; Foreground, Background

This article was previously published under Q29943

On This Page

Expand all | Collapse all

SUMMARY

When using text mode (using the SCREEN statement with mode 0), the SCREEN function can return the color attribute of a character on the screen. If the SCREEN function is invoked at a location where a character is printed, the color attribute returned is a combination of the foreground and background colors. However, on a graphics screen mode (a SCREEN mode other than 0), the SCREEN function always returns zero.

If the character is not blinking (that is, the high-order, eighth bit is off), the foreground color can be obtained by taking the color attribute MOD 16, and the background color can be obtained by dividing the color attribute by 16. If the character is blinking, just turn off the eighth bit in the attribute byte before this calculation. For example:
   X% = SCREEN (1, 1, 1)
   X% = X% AND &H7F   ' Turns off bit 8.
				
The following formula shows the relationship between the attribute and the foreground and background colors, taking into account all eight bits of the attribute:
   ATTRIBUTE = 128*(FOREGROUND\16) + BACKGROUND*16 + (FOREGROUND MOD 16)
				
This information applies to Microsoft QuickBasic versions 1.00, 1.01, 1.02, 2.00, 2.01, 3.00, 4.00, 4.00b, and 4.50 for MS-DOS; to Microsoft Basic Compiler versions 6.00 and 6.00b for MS-DOS and MS OS/2; and to Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10 for MS-DOS and MS OS/2.

MORE INFORMATION

The following is a bitmap of the attribute returned by the SCREEN function:
        BIT ->    7     6     5     4     3     2     1     0
               _________________________________________________
               |  0  |  1  |  0  |  0  |  0  |  0  |  0  |  1  |
               -------------------------------------------------
                  ^     ^     ^     ^     ^     ^     ^     ^
                  |     |     |     |     |     |     |     |
        Blinking --     -------------     |     -------------
                       |      Intensity --     |
     Background Color --                       -- Foreground Color
				

Code Example

CLS
COLOR 4, 1
PRINT "hello";
X% = SCREEN (1, 1, 1)
PRINT "Attribute: "; X%
X% = X% AND &H7F          ' Makes sure blinking bit (bit 8) is off.
PRINT "Foreground: "; X% MOD 16
PRINT "Background: "; X% \ 16
				

APPLIES TO
  • Microsoft QuickBasic 1.01
  • Microsoft QuickBasic 1.02
  • Microsoft QuickBasic 2.01
  • Microsoft QuickBasic 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBasic 4.5 for MS-DOS
  • Microsoft BASIC Compiler 6.0
  • Microsoft BASIC Compiler 6.0b
  • Microsoft BASIC Professional Development System 7.0
  • Microsoft BASIC Professional Development System 7.1
Keywords: 
KB29943
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
 

Article Translations