Programming First Steps - Using the Printer


1. How do I write a program that prints some text?

  • This example shows how to print some text to the default printer in easy steps. The first step is to start the Developer Environment by double clicking its icon:

    Programming first steps - Using the printer

  • Use the File - New - Text File menu command to open a blank window. Then type in the following program (or copy and paste this program from the browser window):
      // Printing.cls
      Ubercode 1 Class Printing
      public function main()
        call Startprint(SysPrinter)
        call Drawtext(SysPrinter, "Hello Printer!" + NL)
        call Drawtext(SysPrinter, "Have a nice day..." + NL)
        call Endprint(SysPrinter)
      end function
      end class
  • The program has a single public function called main. Main is special because Windows starts there when it loads the program.
  • In the actual code part of main the call to Startprint activates the printer, and SysPrinter means the Windows default printer.
  • The calls to Drawtext send text to the printer. The text is enclosed in double quotes ("..."). NL means a New Line string, which causes the printer to move to the start of the next line. The strings are joined using the "+" sign.
  • The call to Endprint closes the printer. Endprint must always be called after Startprint.
  • Use the File - Save As command to save the program.
  • Use the Run - Quickbuild EXE File menu command to start the compiler, then wait for it to finish. If you entered the class as shown it will compile without errors. If there were errors the error message gives the line number of the error, so go to this point and check the syntax. The gaps between words doesn't matter and you can enter letters in upper or lower case. However the spelling and punctuation do matter. Also check you are typing the double quote character around the strings, you cannot type a single quote twice.
  • After the program successfully compiled run it with the Run - Start menu command. The program should finish running after a few seconds, but it may take 30 seconds or so for the printout to appear. Note the program requires either a printer attached to your PC, or requires a network printer.
  • The printout should have two lines of text on it, because we printed two lines using Drawtext.
  • Although outside the scope of this example, the program can be modified to use a non-default printer. Printer names have a maximum length of MAX_PRINTERNAME (255 characters).

2. How do I print some text from a file?

This program shows how to load a text file and copy the text to the printer:

  // Printfile.cls
  Ubercode 1 Class Printfile

  function Printme(in text:string[*])
    call Startprint(Sysprinter)
    call SetFontname(Sysprinter, "Times New Roman")
    call SetFontsize(Sysprinter, 14)
    call SetFontitalic(Sysprinter, True)
    for count from 1 to Strcount(text)
      call Drawtext(Sysprinter, Strline(text,count))
    end for
    call Endprint(Sysprinter)
  end function

  public function main()
    filename <- Openfiledialog("Choose File", 0, "*.txt")
    call Loadfile(filename, FILE_TEXT, textstr)
    if textstr /= "" then
      call Printme(textstr)
    end if
  end function

  end class

The program works as follows. The code that does the printing is put in a separate function Printme to make the program easier to understand. In general function main should contain as little code as possible, to make it easier to change the program as it develops. The most important lines in function Printme are:

  call Startprint(Sysprinter)
  call Drawtext(Sysprinter, text)
  call Endprint(Sysprinter)

Startprint initializes the printer and displays a window that shows the printer's progress. Drawtext copies some text to the printer, moving down to the next line if text ends with a new line character (the NL character). You could call any drawing routines here, such as Drawpicture etc, but this program keeps to printing text for now. Finally Endprint closes the printer and removes the printer progress window.

As you can see from the code, function Printme works by calling Startprint, then sets a more interesting font which is Times New Roman 14 point italic, then sets up a loop to get each line of the string and calls Drawtext. When Printme has looped through all the lines in the text string, it calls Endprint. You could easily include Printme in other programs as a handy routine for printing text.

The printer used by Printme is Sysprinter, which is the system default printer. This is set up under Windows by Start - Settings - Printers, then right-clicking on the printer you want to use as the default.

Moving on to function main, this uses Openfiledialog to choose the text file to print. If this text file exists and contains some text, main calls Printme to print the text. If no text file was chosen, or if Cancel was pressed in the Open file dialog, the programs ends without printing anything.

3. How do I print different fonts on a line?

Use Drawtext without appending a NL character. In that way you do not move to a new line after printing the text. For example:

  call Startprint(Sysprinter)
  call Drawtext(Sysprinter, "To ")
  call SetFontbold(Sysprinter, True)
  call Drawtext(Sysprinter, "boldly ")
  call SetFontbold(Sysprinter, False)
  call Drawtext(Sysprinter, "go")
  call Endprint(Sysprinter)

will print the following:

  To boldly go

You can change the font name, the font size, the bold and italic state and the color, all on the same line.