Printanje

Vse v zvezi s tiskanjem, urejanjem izpisov in reporting komponentami.
Odgovori
Heker
Prispevkov: 122
Pridružen: 27.11.2001 18:40:08

Printanje

Odgovor Napisal/-a Heker » 05.01.2002 15:39:45

Rd bi zvedu kk se cist na enostaven nacin sprinta vsebino Memota.

Hvala!

LP BLaž

Miha-R
Prispevkov: 2155
Pridružen: 14.05.2001 21:29:53

Printanje

Odgovor Napisal/-a Miha-R » 05.01.2002 15:45:01

Tole kodo je napisal Peter Below in jo objavil v Internetu:'

Koda: Izberi vse

'.trim({+------------------------------------------------------------
 | Procedure PrintMemo
 |
 | Parameters:
 |  aMemo: memo to print
 |  centerVertical: true if output should be centered vertically
 |  centerHorizontal: true if output should be centered horizontally
 | Call method:
 |  static
 | Description:
 |  Prints the contents of a memo on the currently selected 
 |  printer, using the memos Font. The output can be centered
 |  vertically and/or horizontally. Default margins will be used.
 |  If the text does not fit onto one page the centerVertical 
 |  parameter is ignored. 
 | Error Conditions:
 |  none
 | Note: 
 |  requires Printers in the Uses clause.
 |Created: 27.10.97 13:16:46 by P. Below
 +------------------------------------------------------------}
Procedure PrintMemo( aMemo: TMemo; 
                     centerVertical, centerHorizontal: Boolean );
  Var
    topMargin, bottomMargin, leftMargin, rightMargin: Integer;
    x,y: Integer;
    maxLineLength: Integer;
    linecounter: Integer;
    lineheight : Integer;
  Begin
    { Set the default margins to 1 inch for top, bottom, 0.75 inch
      for left and right margins. GetDeviceCaps is used to get the
      printers resolution (dots per inch). The margin variables
      contain the position of the margin on paper, in dots, not the
      distance from the border of the printing region! }
    topMargin := GetDeviceCaps( Printer.Handle, LOGPIXELSY );
    bottomMargin := Printer.PageHeight - topMargin;

    leftMargin := GetDeviceCaps( Printer.handle, LOGPIXELSX ) * 3 div 4;
    rightMargin := Printer.PageWidth - leftMargin;

    { Start the print job, assign the memo font to the printer canvas. 
      Note that we have to make sure the print job is closed properly,
      thus the try finally block. }
    Printer.BeginDoc;
    try
      Printer.Canvas.Font.PixelsPerInch :=
        GetDeviceCaps( Printer.Canvas.Handle, LOGPIXELSY );
        { This is really only necessary for Delphi 1, due to a
          buglet. }
      Printer.Canvas.Font:= aMemo.Font;

      { Determine the height of a line in pixels }
      lineHeight := Printer.Canvas.TextHeight('Ay');

      If centerHorizontal Then Begin
        { Iterate once over all lines of the memo and determine the 
          length, in pixels, of the longest line. We need that to
          adjust the leftMargin to center the text. }
        maxLineLength := 0;
        For linecounter := 0  To aMemo.Lines.Count-1 Do Begin
          x:= Printer.Canvas.TextWidth( aMemo.Lines[linecounter]);
          If x > maxLineLength Then 
            maxLineLength := x;
        End; { For }

        { Adjust the leftMargin to center the text into the 
          available space between the current margins. }
        x := leftMargin + 
             (rightMargin-leftMargin-maxLineLength) div 2;
        If x < leftMargin Then Begin
          { Problem, longest line does not fit into the available
            space! We leave the margin untouched, the line will
            be truncated. }
        End { If }
        Else
          leftMargin := x;
      End; { If }

      If centerVertical Then Begin
        { Calculate the vertical space needed to print all lines,
          adjust the topMargin to center the text as needed. }
        y := lineHeight * aMemo.Lines.Count;
        If y < (bottomMargin - topMargin) Then Begin
          topMargin := 
            topMargin + (bottomMargin - topMargin - y) div 2;
        End; { If }
        { Else
            space needed is larger than the height of a page,
            so ignore the center request. }
      End; { If }

      { Margins have been calculated, so we are finally set up
        to start printing lines. In the following code x and y
        hold the starting position of the next line. }
      x:= leftMargin;
      y:= topMargin;
      For linecounter := 0  To aMemo.Lines.Count-1 Do Begin
        Printer.Canvas.TextOut( x, y, aMemo.Lines[linecounter]);
        y := y + lineHeight;
        If y >= bottomMargin Then Begin
          { Eject the page and start a new one, but only if
            the line we have just printed is not the last line
            anyway. EndDoc will eject the current page. }
          If linecounter < (aMemo.Lines.Count-1) Then Begin
            Printer.NewPage;
            y:= topMargin;
          End;
        End; { If }
      End; { For }
    finally
      Printer.EndDoc;
    end;
  End; { PrintMemo }
, "

").'
''

AeBa
Prispevkov: 47
Pridružen: 15.09.2001 21:07:28

Printanje

Odgovor Napisal/-a AeBa » 05.01.2002 20:05:41

ali na kratko:

var
Line: Integer;
PrintText: TextFile;
begin
if PrintDialog1.Execute then
begin
AssignPrn(PrintText);
Rewrite(PrintText);
Printer.Canvas.Font := memo1.Font;
for Line := 0 to memo1.lines.Count - 1 do
Writeln(PrintText, memo1.lines[Line]);
CloseFile(PrintText);
end;


end;

Webko
Prispevkov: 424
Pridružen: 13.05.2001 18:27:25

Printanje

Odgovor Napisal/-a Webko » 05.01.2002 21:32:42

ali na kratko:

var
Line: Integer;
PrintText: TextFile;
begin
if PrintDialog1.Execute then
begin
AssignPrn(PrintText);
Rewrite(PrintText);
Printer.Canvas.Font := memo1.Font;
for Line := 0 to memo1.lines.Count - 1 do
Writeln(PrintText, memo1.lines[Line]);
CloseFile(PrintText);
end;


end;
AeBa, malo bi te samo še pokomentiral in tudi vse druge, ki ne uporabljate oznak [.KODA] [./KODA].
Če ne uporabite teh oznak, je vaša koda zelo neberljiva in tudi jaz, kakor je že Gre-Gor napisal v eni temi, kode potem ne berem.
When things are going well, something will go wrong! When things just coulden't go worse, they will!

aoven
Prispevkov: 2338
Pridružen: 27.06.2001 23:56:07

Printanje

Odgovor Napisal/-a aoven » 07.01.2002 09:01:49

Sicer je pa samo 4x krajša koda od Petrove
Že res, ampak ne vsebuje poravnave teksta, tako kot Petrova. In to je tisto, kar jo tako skrajša. ;)

Aleksander

Odgovori