Printer.BeginDoc; Printer.NewPage; (*See comment*) Printer.Canvas.TextOut(300,300, 'Hello World'); Printer.EndDoc;That was pretty painless! Don't worry all of this will soon descend into the painful depths you know so well.
Printer.Canvas.TextOut(300,300, 'Hello World'); Printer.Canvas.TextOut(300,300, 'On top of other');.... but it would (obviously) also happen if you did....
Printer.Canvas.TextOut(300,300, 'Hello World'); Printer.Canvas.TextOut(301,301, 'On top of other');Not so obvious: What happens if you do....
Printer.Canvas.TextOut(300,300, 'Hello World'); Printer.Canvas.TextOut(300,400, 'On top of other?');...? That depends, of course on what point in the two strings is placed at 100,100 and 100,200 (likely the upper left corner) and on the size of the writing.
Printer.Canvas.TextOut(300,300, 'Hello Worldiiiiiiiiiiiiiiiiiiiiiii'); Printer.Canvas.TextOut(1200,300, 'On same line');might come out quite nicely... it did on my system. However, in Windows, what you get depends on a whole bunch of settings... in this case the typeface and character size will have a lot to do with what result you get. Even if the above came out ok, the following.... with the same number of "W"s as the previous had "i"s did not print nicely on my system.
Printer.Canvas.TextOut(300,300, 'Hello WorldWWWWWWWWWWWWWWWWWWWWWWW'); Printer.Canvas.TextOut(1200,300, 'On same line');Again... "good" answers to these problems are matters for a more advanced tutorial. For now, we're going to "solve" the problems by using a font with a fixed pitch: Courier New.
Printer.Canvas.font.name:='Courier New'; Printer.Canvas.font.size:=12; Printer.Canvas.TextOut(300,300, 'y23456789012345678901234567890'); Printer.Canvas.TextOut(1200,300, 'X'); Printer.Canvas.TextOut(700,400, 'World'); Printer.Canvas.TextOut(300,400, 'Hello');There are a couple of lessons in the output: "Hello World": Notice we "printed" the second word first? There's no need to use code to generate a page from top left to bottom right. You might "print" headings with a fixed procedure, and then fill in the fields in a later procedure.
InitForPrinterOutputWV; (*WV: Windows version*)
StartANewPageWV;
SendLine('1st line of text');
SendLine('2nd line of text');
SendLine('3rd line of text');
StartANewPageWV;
SendLine('Pg2 1st line');
SendLine('Pg2 2nd line');
AddToLine('A bit of a third line ');
SendLine('of text ');
SendLine('The end');
FinishPrintingWV;
A real program would need more error checking than I am including here. For instance, it would be necessary to check that lines were not too long, and that not too many lines were sent to a page.
writeln(lst,sVar1,sVar2,sVar3);For our program, that would convert to....
SendLine(sVar1+sVar2+sVar3);And... continuing with the detail... writeln could print out the contents of various types of variable whereas SendLine requires a string. Not a problem, thanks to Delphi's rich vocabulary....
const
bLPRheight=100; (*Line pitch, vertical*)
variables (Insert the following just after the "Private" keyword in the Form's type definition. This makes them "fields" of the form, but you can think of them as variables.)
sLPRBuffer:string;(*Holds line as it is being assembled from misc AddToLines*)
bLPRYPos:byte;(*Count of line which is next to use. First line: "0"*)
Revise the "Print" button as follows:
procedure TForm1.buPrintStuffClick(Sender: TObject);
begin
InitForPrinterOutputWV; (*WV: Windows version*)
StartANewPageWV;
SendLine('1st line of text');
SendLine('2nd line of text');
SendLine('3rd line of text');
//StartANewPageWV;
//SendLine('Pg2 1st line');
//SendLine('Pg2 2nd line');
//AddToLine('A bit of a third line ');
//SendLine('of text ');
//SendLine('The end');
FinishPrintingWV;
Create first attempts at the four procedures as follows. For each, you will also have to make an entry in the form's declaration. I.e., put the following in to the form's class definition, just before the word "Public". (Procedure declarations have to follow the field declarations in both the public and the private sections of the class declaration, by the way.)
procedure InitForPrinterOutputWV; procedure SendLine(sTmp:string); procedure AddToLine(sTmp:string); procedure FinishPrintingWV;And then, just above the final "end." add the following. the lines starting with "//" will have no effect for the moment... get what does have an effect working first.
procedure TForm1.InitForPrinterOutputWV;
begin
(*Not a lot needed in this simple example... but you might want, for instance,
to assign text to a page header string that gets printed at the start of each
page. This would be the place to do it. In a more complicated case, you
might also want to alter the font, and other things might need changing
as a result, and you would do those things here, too.*)
//Printer.Canvas.font.name:='Courier New';
//Printer.Canvas.font.size:=12;
//Printer.BeginDoc;
end;
procedure TForm1.StartANewPageWV;
begin
sLPRBuffer:='';
bLPRYPos:=0;
//Printer.NewPage;
end;
procedure TForm1.SendLine(sTmp:string);
begin
sLPRBuffer:=sLPRBuffer+sTmp;
showmessage('Would print: '+sLPRBuffer);
//Printer.Canvas.TextOut(300,bLPRYPos*bLPRHeight,sLPRBuffer);
inc(bLPRYPos);
sLPRBuffer:='';
end;
procedure TForm1.AddToLine(sTmp:string);
begin
sLPRBuffer:=sLPRBuffer+sTmp;
end;
procedure TForm1.FinishPrintingWV;
begin
if sLPRBuffer<>''then SendLine(sLPRBuffer);
//Printer.EndDoc;
end;
When the above works, take out most of the "//"s, but, for now, leave in those in the six line block starting with...
============
Fonts Property:
Applies to
TPrinter object; TScreen component
Declaration- property Fonts: TStrings;
Description:
Run-time and read-only. The Fonts property for the screen component returns a list of fonts supported by the screen.
The Fonts property for a printer object holds a list of fonts supported by the printer. The list contains TrueType fonts even if the printer doesn't support them natively because the Windows Graphics Device Interface (GDI) can draw TrueType fonts accurately when a print job uses them.
============
procedure TextRect(Rect: TRect; X, Y: Integer; const Text: string);
Description:
The TextRect method displays text inside a clipping rectangle. Any portions of the text passed in the Text parameter that fall "
====
Screen Variable:
Unit: Forms
Declaration- Screen: TScreen;
Description:
The Screen variable is a TScreen component that normally represents your screen device. By default, your application creates a screen component based on information from Windows about the current screen device and assigns it to Screen.
Example:
The following code sets the width of a form called Form1 to half the width of the screen:
Form1.Width := Screen.Width div 2;
======
NewPage
Description:
The NewPage method forces the current print job to begin printing on a new page in the printer. It also increments the value of the PageNumber property and resets the value of the Pen property of the Canvas back to (0, 0).
{end quotes}
Regarding NewPage: Not to niggle... but in case less experienced users are puzzled..... Part of that is believed to contain an error... probably should be resets PENPOS back to 0,0 And: The Delphi2 helpfile entry for TPrinter doesn't list Canvas as a property, but it does imply that there is one. Also TCanvas has a link to TCanvas for TPrinter
|
|
Click here to visit editor's freeware, shareware page.