home *** CD-ROM | disk | FTP | other *** search
- unit Main;
-
- { Copyright (c) 1995 by Charles Calvert)
- { Project Name: PRINTSRC }
-
- { This program will allow you to iterate through directories,
- selecting multiple text files, and then print them.
-
- At this stage, this program is not very robust. Sometimes
- I've found it best to leave the default font, rather than
- trying to select a new one.
-
- I don't know how much Windows handles automatically, but
- this program does not test for running out of paper, or
- for overflowing the print buffer.
- }
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs,
- Messages, Classes, Graphics,
- Controls, Forms, Dialogs,
- StdCtrls, AllDirs, Fileiter, ExtCtrls, FileCtrl, Buttons;
-
- type
- TForm1 = class(TForm)
- FileIterator1: TFileIterator;
- ListBox1: TListBox;
- PrintDialog1: TPrintDialog;
- PrinterSetupDialog1: TPrinterSetupDialog;
- FontDialog1: TFontDialog;
- DirectoryListBox1: TDirectoryListBox;
- DriveComboBox1: TDriveComboBox;
- Panel1: TPanel;
- Label1: TLabel;
- ChooseFiles: TBitBtn;
- PrintFiles: TBitBtn;
- PrinterFont: TBitBtn;
- BitBtn1: TBitBtn;
- EExt: TEdit;
- Label2: TLabel;
- procedure FileIterator1FoundFile(FileName: String; SR: TSearchRec);
- procedure ChooseFilesClick(Sender: TObject);
- procedure PrintFilesClick(Sender: TObject);
- procedure PrinterFontClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure BitBtn1Click(Sender: TObject);
- private
- PrintCancel: Boolean;
- procedure PrintHeader(N: string; TextHeight: Integer; PageNumber: Integer);
- procedure PrintFile(N: string);
- function LinesPerPrintedPage: Integer;
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- uses
- Printers;
-
- {$R *.DFM}
-
- procedure TForm1.FileIterator1FoundFile(FileName: String; SR: TSearchRec);
- begin
- ListBox1.Items.Add(FileName);
- end;
-
- procedure TForm1.ChooseFilesClick(Sender: TObject);
- begin
- ListBox1.Clear;
- FileIterator1.Run(EExt.Text, DirectoryListBox1.Directory);
- PrintFiles.Enabled := True;
- end;
-
- function TForm1.LinesPerPrintedPage: Integer;
- var
- PageHeight, TextHeight: Integer;
- TextMetrics: TTextMetric;
- begin
- PageHeight := Printer.PageHeight;
- GetTextMetrics(Printer.Handle, TextMetrics);
- TextHeight := TextMetrics.tmHeight + TextMetrics.tmExternalLeading;
- Result := PageHeight div TextHeight; { Lines Per Page on Printer }
- end;
-
- procedure TForm1.PrintHeader(N: String; TextHeight: Integer; PageNumber: Integer);
- var
- S: string;
- begin
- S := '=====================================================';
- Printer.Canvas.TextOut(1, TextHeight * 0, S);
- Printer.Canvas.TextOut(1, TextHeight * 1, N + ' Page: ' + IntToStr(PageNumber));
- Printer.Canvas.TextOut(1, TextHeight * 2, S);
- end;
-
- procedure TForm1.PrintFile(N: string);
- var
- S: string;
- P: TPrinter;
- F: System.Text;
- I, LinesPerPage, TextHeight, PageNumber : Integer;
- begin
- LinesPerPage := LinesPerPrintedPage;
- pageNumber := 1;
- i := 3;
- System.Assign(F, N);
- try
- TextHeight := Printer.Canvas.TextHeight(N);
- System.Reset(F);
- Printer.BeginDoc;
- PrintHeader(N, TextHeight, PageNumber);
- repeat
- if PrintCancel = True then Exit;
- ReadLn(F, S);
- Printer.Canvas.TextOut(1, TextHeight * i, S);
- Inc(i);
- if i = LinesPerPage then begin
- i := 3;
- Printer.NewPage;
- Inc(PageNumber);
- PrintHeader(N, TextHeight, PageNumber);
- end;
- until EOF(F);
- Printer.EndDoc;
- finally
- System.Close(F);
- end;
- end;
-
- procedure TForm1.PrintFilesClick(Sender: TObject);
- var
- N: string;
- i: Integer;
- begin
- PrinterSetUpDialog1.Execute;
- for i := 0 to ListBox1.Items.Count - 1 do begin
- SendMessage(ListBox1.Handle, lb_SetCurSel, i, 0);
- N := ListBox1.Items.Strings[i];
- PrintFile(N);
- if PrintCancel = True then Exit;
- end;
- end;
-
- procedure TForm1.PrinterFontClick(Sender: TObject);
- begin
- FontDialog1.Font := Printer.Canvas.Font;
- if FontDialog1.Execute then
- Printer.Canvas.Font := FontDialog1.Font;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Label1.Left := ListBox1.Left;
- PrintCancel := False;
- end;
-
- procedure TForm1.BitBtn1Click(Sender: TObject);
- begin
- PrintCancel := True;
- end;
-
- end.
-