|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWriting plain text to the screen is as simple as BackgroundMany of us learned programming with GW-Basic in MSDOS on an IBM PC (Actually, for me it was Level II Basic in TRSDOS on the TRS-80). Back then, we didn't have the fancy GUI displays we have now. Printing to the screen was simple (primitive) but simple (easy). One just had to write... PRINT "Hello, World!"
... and the text was printed on the screen. And just as simply, if you wrote... LPRINT "Hello, World!"
... it would be typed out on the attached printer (which was referred to as a "Line Printer", hence the IF Where$ = "File" THEN OPEN "Output.txt" FOR OUTPUT AS 3
ELSE IF Where$="Printer" THEN OPEN "LPT1:" FOR OUTPUT AS 3
ELSE OPEN "CONS:" FOR OUTPUT AS 3
PRINT#3, "Hello, World!"
Then came Windows, which made drawing full graphics on the screen and printer possible, but made writing anything to the screen or printer difficult. Now, we have .NET, which has reclaimed the simplicity of the Microsoft DOS command line in Console mode applications. Once again, it's just a simple... Console.WriteLine("Hello, World!");
... to display text on the screen. Similarly, choosing between the screen or a text file can be handled like this: TextWriter output = TextWriter.Null;
if (Where == "File")
output = new StreamWriter("output.txt");
else
output = Console.Out
output.WriteLine("Hello, World!");
And yet, writing to the printer is as difficult as ever. What we need to fill the hole is a Using the CodeUse of LPrintWriter lprint = new LPrintWriter();
lprint.WriteLine("Hello, world!");
lprint.Close();
Because we are still going through Windows, where printing is still page- & spool-oriented, the (Actually, the dialog is displayed and printing started upon the
lprint.Font = new System.Drawing.Font("Arial", 22.0f);
Similarly, you can change the color of the text (assuming your printer can handle it), using the lprint.TextColor = Color.Blue;
Note that the font & color settings apply to the entire printout. You cannot set different fonts for different sections of a page. Additionally, HtmlAnchor A = new HtmlAnchor();
A.HRef = "http://www.msn.com";
A.InnerText = "MSN";
A.RenderControl(new HtmlTextWriter(lprint));
Returning to our switchable output example: TextWriter output = TextWriter.Null;
if (Where == "File") output = new StreamWriter("output.txt");
else if (Where == "Printer") output = new LPrintWriter();
else output = Console.Out
output.WriteLine("Hello, World!");
Note, that, despite its purpose to be used in Console application, since it uses Finally, it is written in C#, but if placed in a class library it can be used from a VB.NET application (that was actually the purpose for which it was written). ImplementationThe nice thing about However, in this case, I knew I just needed to collect the text as a big Printing is pretty much straightforward. At the start, the big History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||