File: unitmain.pas

package info (click to toggle)
lazarus 1.6.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 172,444 kB
  • ctags: 124,173
  • sloc: pascal: 1,528,777; xml: 260,232; sh: 3,008; java: 603; makefile: 512; perl: 297; sql: 222; ansic: 137
file content (102 lines) | stat: -rw-r--r-- 2,028 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
unit unitmain;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Buttons, Printers;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    ListBox1: TListBox;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
    procedure PrintString(S:String);
    procedure PrintStream(St:TStream);
    procedure PrintSample;
  public
    { public declarations }
  end; 

var
  Form1: TForm1; 

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  // fill in the printer list
  Listbox1.Items.Assign(Printer.Printers);
end;

procedure TForm1.PrintString(S: String);
var
  Written: Integer;
begin
  Printer.Write(S[1], Length(S), Written);
end;

const
  MaxBufSize = 256;

procedure TForm1.PrintStream(St: TStream);
var
  Written: Integer;
  Buffer: array[0..MaxBufSize-1] of byte;
begin
  while St.Position<St.Size do begin
    Written := St.Read(Buffer, MaxBufSize);
    Printer.Write(Buffer, Written, Written);
  end;
end;

procedure TForm1.PrintSample;
var
  S: TStringStream;
begin
  // print a plain string
  PrintString('===   FIRST A STRING   ==='+LineEnding);
  PrintString(Memo1.Text);
  PrintString('=== NOW USING A STREAM ==='+LineEnding);
  // print using a stream
  S := TStringStream.Create(Memo1.Text);
  PrintStream(S);
  S.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Listbox1.ItemIndex<0 then begin
    ShowMessage('Select a printer from the list');
    exit;
  end;

  // on a freshly retrieved printer list, either method could
  // be used to select a printer: SetPrinter or PrinterIndex
  //Printer.PrinterIndex := Listbox1.ItemIndex;
  Printer.SetPrinter(ListBox1.Items[Listbox1.ItemIndex]);
  Printer.Title := Caption;
  Printer.RawMode := True;
  Printer.BeginDoc;
  PrintSample;
  Printer.EndDoc;

end;
end.