Qbasic, Quickbasic/QuickBasic 4.5

Advertisement


Question
QUESTION: Hi Alex,I've made quite a lot of programs in QB,they still run as EXE prog.in XP & Vista but since USB printers no
longer seem to understand DOS I can't get a  print-out on
paper. Do you know of any routine that would work like
"PrtScrn" that I could call from anywhere in the prog.?
Thanks and greetings from Antwerp. L.

ANSWER: Hi, Laurent,

Yes, it is true, DOS has some trouble communicating with USB printers, but there is a handy trick you can use.  First, make sure anything you print on the screen also gets printed into a file.  Here is an easy way of doing that:

DECLARE SUB Print2( text AS STRING, fileHandle AS INTEGER )

DIM f AS INTEGER

f = FREEFILE

OPEN "tmp.txt" FOR RANDOM AS #f

' Instead of using: PRINT "text", now do Print2 "text", f
Print2 "This is a test", f

CLOSE #f
END

SUB Print2( text AS STRING, fileHandle AS INTEGER )
 PRINT text
 PRINT #fileHandle, text
END SUB

Now, after you have that, here is what I would do.  Note, this ONLY works for TEXT...if you are drawing graphics
So,  in the part of the code that looks like this:
CLOSE #f
END

Inbetween that, put this:
SHELL "notepad.exe /P tmp.txt"

This opens and prints it through windows' notepad program.  This is a bit of a cheap hack way of doing it, but it works, and it will use Windows' printer-usb drivers, so you won't have any problems.

I hope that helps,
-Alex

---------- FOLLOW-UP ----------

QUESTION: hi, Alex,
what I really wanted was a replacement for the following
routine that I used so far : GOSUB 6000
6000' Outprintroutine
      FOR V = 1 TO 18
       FOR H = 1 TO 80
        LPRINT CHR$(SCREEN(V, h));
       NEXT H
      NEXT V
  RETURN
Which did the trick quite well, as long as one uses the PRN
port.  Any suggestions ? Greetings.  Laurent

Answer
So, if your question is how to make that code work with a usb printer:

6000
 DIM toprint AS STRING
 DIM ascii AS INTEGER
 DIM x AS INTEGER
 FOR V = 1 TO 18
   FOR H = 1 TO 80
     ascii = SCREEN( V, H )
     IF ascii <> 0 THEN
       toprint = toprint + CHR$( ascii )
     END IF
   NEXT H
   toprint = toprint + CHR$( 13 )
 NEXT V
 x = FREEFILE
 OPEN "tmp.txt" FOR OUTPUT AS #x
   PRINT #x, toprint
 CLOSE #x
 SHELL "notepad /p tmp.txt"
 KILL "tmp.txt"


That should work on anything from windows95 all the way to vista

Take care,
-Alex

Qbasic, Quickbasic

All Answers


Answers by Expert:


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2012 About.com, a part of The New York Times Company. All rights reserved.