Windows Console - Box Drawing Subroutine

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
dmontaine
Posts: 23
Joined: Mar 15, 2018 7:13

Windows Console - Box Drawing Subroutine

Post by dmontaine »

This program, box, contains the subroutine DrawBox. You can specify the type, location, size and the colors of the box lines when calling the subroutine. Single, Double and mixed lines are supported. This example program will draw four boxes, each with a different line type, size, and color combination. The program uses the US English keyboard codes with the Consolas font. Users may need to modify it for other languages and fonts. If you do, please post the modified program back to this thread.

Code: Select all

' Program: Box
' Target: Windows 10/11 Console
' Draw a box on a Windows console
' Tested with US English Windows 10 and Consolas font
' Subroutine DrawBox Copyright 2022 Donald S Montaine
' Released under the Creative Commons CC-BY license

'=========================================
' SUBROUTINES
'=========================================

sub DrawBox(Byval BoxType     as string,  _
            Byval StartY      as integer, _
            Byval StartX      as integer, _
            Byval BoxWidth    as integer, _
            Byval BoxHeight   as integer, _
            Byval ForeColor   as integer, _
            Byval BackColor   as integer)

   ' Line drawing codes          ' T codes are unused in this program
   ' dim LTee   as integer         'Left Tee Line Drawing Character
   ' dim RTee   as integer         'Right Tee Line Drawing Character
   ' dim TTee   as integer         'Top Tee Line Drawing Character
   ' dim BTee   as integer         'Bottom Tee Line Drawing Character
   dim UpLCnr as integer         'Upper Left Corner Line Drawing Character
   dim UpRCnr as integer         'Upper Right Corner Line Drawing Character
   dim HorzLn as integer         'Horizontal Line Drawing Character
   dim VertLn as integer         'Vertical Line Drawing Character
   dim LwLCnr as integer         'Lower Left Corner Line Drawing Character
   dim LwRCnr as integer         'Lower Right Corner Line Drawing Character

   dim ctr as integer            ' loop counter
   
   ' Codes for line drawing SL=Single Line  DL = Double Line DSD=Double Verticals & Single Horizontals
   select case BoxType
      case "SL"
         UpLCnr = 218 : UpRCnr = 191 : HorzLn = 196 : VertLn = 179 : LwLCnr = 192 : LwRCnr = 217
         'LTee   = 195 : RTee   = 180 : TTee   = 194 : BTee   = 183
      case "DL"
         UpLCnr = 201 : UpRCnr = 187 : HorzLn = 205 : VertLn = 186 : LwLCnr = 200 : LwRCnr = 188
         'LTee   = 195 : RTee   = 180 : TTee   = 194 : BTee   = 183
      case "DSD"      
         UpLCnr = 214 : UpRCnr = 183 : HorzLn = 196 : VertLn = 186 : LwLCnr = 211 : LwRCnr = 189
         'LTee   = 195 : RTee   = 180 : TTee   = 194 : BTee   = 183
      case "SDS"
         UpLCnr = 213 : UpRCnr = 184 : HorzLn = 205 : VertLn = 179 : LwLCnr = 212 : LwRCnr = 190
         'LTee   = 195 : RTee   = 180 : TTee   = 194 : BTee   = 183
      case else
         return
   end select

   ' Draw the Box
   locate StartY, StartX
   color ForeColor, BackColor
   print chr(UpLCnr) + string(BoxWidth-2,HorzLn) + chr(UpRCnr) ;
   for ctr as integer = StartY+1 to StartY+BoxHeight-2
      locate ctr,StartX
      print chr(VertLn)  ;
      locate ctr,StartX+BoxWidth-1
      print chr(VertLn) ;
   next
   locate ctr,StartX
   print chr(LwLCnr) + string(BoxWidth-2,HorzLn) + chr(LwRCnr) ;
end sub

'========================================
' MAIN PROGRAM
'++++++++++++++++++++++++++++++++++++++++

dim LocY as integer                   ' column location of upper left corner of box
dim LocX as integer                   ' row location of upper left corner of box
dim Wid  as integer                   ' width of box
dim Hgt  as integer                   ' height of box

' Colors Enumeration (0 - 15) ie Black = 1, Cyan = 4 etc
Enum
   Black : Blue : Green : Cyan : Red : Majenta : Brown : LightGrey : DarkGrey 
   BrightBlue : BrightGreen : BrightCyan : BrightRed : BrightMajenta : Yellow : White
end enum

width 80,24                          ' set width and height of console

DrawBox("DL",2,2,78,22,Black,DarkGrey)
DrawBox("SL",3,4,74,20,Red,Black)
DrawBox("SDS",4,6,70,18,BrightBlue,Black)
DrawBox("DSD",5,8,66,16,Yellow,Black)

locate 24,1
sleep
Last edited by dmontaine on Jul 15, 2022 17:20, edited 2 times in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Windows Console - Box Drawing Subroutine

Post by D.J.Peters »

Does not work in a german console !
dmontaine
Posts: 23
Joined: Mar 15, 2018 7:13

Re: Windows Console - Box Drawing Subroutine

Post by dmontaine »

As mentioned in the header, it was tested for US English with the consolas font. For other languages/fonts the box drawing characters may have different codes. Some console fonts may not even have box drawing characters. This is a working template for the language and font specified. Users can modify it to suit other languages. Note: I have modified the description text in the original posting to make clear that this example is language and font specific.
Post Reply