Window Text Printing

Top  Previous  Next

A blank window is uninteresting so lets put some text into it.  Earlier we discussed how to use the PRINT statement to output text into the console window. To output text to a window we use the PRINT statement and specify the window as the first parameter.

In the console we could use LOCATE to specify where text would be printed. For a window we use the MOVE statement.  Since both text and graphics are sent to a window as bitmapped images, the MOVE statement takes its parameters in pixels and not characters. Pixels start from the upper left corner at location 0,0.  The syntax of MOVE is:

MOVE window, x, y

The window keeps track of the current position specified by the MOVE statement. When text is printed, the position is adjusted to the end of the line.
 

DEF w as WINDOW
OPENWINDOW w,0,0,640,200,@SIZE|@MINBOX|@MAXBOX,0,"Text",&main
CENTERWINDOW w
MOVE w,4,20
FOR x=0 TO 50
    PRINT w,"X"
NEXT x
MOVE w,4,40
PRINT w,"This is a test!"
 
run = 1
WAITUNTIL run = 0
CLOSEWINDOW w
END
 
SUB main
SELECT @MESSAGE
     CASE @IDCLOSEWINDOW
                run = 0
     CASE @IDMOUSEMOVE
                MOVE w,4,60
                PRINT w,"Mouse Position: ",@MOUSEX,@MOUSEY,"   "
ENDSELECT
RETURN
ENDSUB

Changing the font

The text printed to a window will use the default font specified in the display control panel unless changed with the SETFONT statement. The SETFONT statement has the syntax of:

SETFONT window, typeface, height, weight {, flags | charset} {,ID}

Height and weight can both be 0 in which case a default size and weight will be used. Weight ranges from 0 to 1000 with 700 being standard for bold fonts and 400 for normal fonts. Flags can be a combination of @SFITALIC, @SFUNDERLINE, or @SFSTRIKEOUT for italicized,  underlined, and strikeout fonts. If an ID is specified then the font of a control in the window or dialog is changed.

Example code fragment:
 

SETFONT mywin, "Ariel", 20, 700, @SFITALIC
PRINT mywin, "ARIEL bold italic"

Selecting character sets.

Certain fonts may have more than one character set. Normally this information is set automatically by the flag value returned by FONTREQUEST. You can set the character set manually by using the following values ORed in with the flags

ANSI_CHARSET  = 0

DEFAULT_CHARSET = 0x00010000

SYMBOL_CHARSET  = 0x00020000

SHIFTJIS_CHARSET = 0x00800000

HANGEUL_CHARSET 0x00810000

GB2312_CHARSET  = 0x00860000

CHINESEBIG5_CHARSET = 0x00880000

OEM_CHARSET = 0x00FF0000

JOHAB_CHARSET = 0x00820000

HEBREW_CHARSET  = 0x00B10000

ARABIC_CHARSET  = 0x00B20000

GREEK_CHARSET = 0x00A10000

TURKISH_CHARSET = 0x00A20000

VIETNAMESE_CHARSET = 0x00A30000

THAI_CHARSET = 0x00DE0000

EASTEUROPE_CHARSET = 0x00EE0000

RUSSIAN_CHARSET = 0x00CC0000

MAC_CHARSET = 0x004D0000

BALTIC_CHARSET = 0x00BA0000

For example to set a terminal font which requires the OEM character set with an italic style:
 

SETFONT mywin, "Terminal", 20, 700, @SFITALIC | 0x00FF0000

If a character set doesn't exist in a particular font then the system will pick a font that closely matches the requested one.

Changing text colors

Text and graphics default to black on white. To change the current foreground drawing color of a window use the FRONTPEN statement. For the background color use the BACKPEN statement.  The syntax of FRONTPEN and BACKPEN are:

FRONTPEN window, color

BACKPEN window, color

The color chosen by the FRONTPEN will be used by text, lines, outlines of rectangles and ellipses, and borders. The BACKPEN color is used as a fill for text if the drawing mode is not transparent.

The color variable can be set easily with the RGB function. RGB takes three numbers from 0 to 255 representing the intensity of red, green and blue components.

Example code fragments:
 

FRONTPEN mywin, RGB(0,0,255):REM light blue
FRONTPEN mywin, RGB(100,0,0):REM medium red
BACKPEN mywin,RGB(200,200,200):REM light gray

FONTREQUEST function

The FONTREQUEST function opens the standard system font dialog. The functions returns the name of the font and sets four variables with the attributes of the requested font. The syntax of the FONTREQUEST function is:

name = FONTREQUEST( window, varSize, varWeight, varFlags, varColor {,dispname})

The variable parameters must be of type INT. FONTREQUEST returns an empty string if the user cancels the dialog.

Example code fragment:

DEF size,weight,flags,col:INT
DEF fontname:STRING
 
fontname = FONTREQUEST(win,size,weight,flags,col)
IF fontname <> ""
   SETFONT win,fontname,size,weight,flags
   FRONTPEN win,col
ENDIF

The variables can be preset to show initial font settings when the dialog is displayed. Optional dispname string presets the font name in the combobox of the system font dialog.