File: unitmain.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (176 lines) | stat: -rw-r--r-- 4,194 bytes parent folder | download | duplicates (6)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
unit unitmain;

{$mode objfpc}{$H+}

interface

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

type

  { TForm1 }

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

var
  Form1: TForm1; 

implementation

{$R *.lfm}

const
  CRLF = #13#10;

{ 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.PrintZebraSample;
var
  s: String;
begin
  // sample code taken from the Online ZPL viewer
  // http://labelary.com/viewer.html
  s :=
    '^XA' + CRLF +
    '^FX Top section with company logo, name and address.' + CRLF +
    '^CF0,60' + CRLF +
    '^FO50,50^GB100,100,100^FS' + CRLF +
    '^FO75,75^FR^GB100,100,100^FS' + CRLF +
    '^FO88,88^GB50,50,50^FS' + CRLF +
    '^FO220,50^FDInternational Shipping, Inc.^FS' + CRLF +
    '^CF0,40' + CRLF +
    '^FO220,100^FD1000 Shipping Lane^FS' + CRLF +
    '^FO220,135^FDShelbyville TN 38102^FS' + CRLF +
    '^FO220,170^FDUnited States (USA)^FS' + CRLF +
    '^FO50,250^GB700,1,3^FS' + CRLF +
    '^FX Second section with recipient address and permit information.' + CRLF +
    '^CFA,30' + CRLF +
    '^FO50,300^FDJohn Doe^FS' + CRLF +
    '^FO50,340^FD100 Main Street^FS' + CRLF +
    '^FO50,380^FDSpringfield TN 39021^FS' + CRLF +
    '^FO50,420^FDUnited States (USA)^FS' + CRLF +
    '^CFA,15' + CRLF +
    '^FO600,300^GB150,150,3^FS' + CRLF +
    '^FO638,340^FDPermit^FS' + CRLF +
    '^FO638,390^FD123456^FS' + CRLF +
    '^FO50,500^GB700,1,3^FS' + CRLF +
    '^FX Third section with barcode.' + CRLF +
    '^BY5,2,270' + CRLF +
    '^FO100,550^BC^FD12345678^FS' + CRLF +
    '^FX Fourth section (the two boxes on the bottom).' + CRLF +
    '^FO50,900^GB700,250,3^FS' + CRLF +
    '^FO400,900^GB1,250,3^FS' + CRLF +
    '^CF0,40' + CRLF +
    '^FO100,960^FDShipping Ctr. X34B-1^FS' + CRLF +
    '^FO100,1010^FDREF1 F00B47^FS' + CRLF +
    '^FO100,1060^FDREF2 BL4H8^FS' + CRLF +
    '^CF0,190' + CRLF +
    '^FO485,965^FDCA^FS' + CRLF +
    '^XZ' + CRLF;
  PrintString(s);
end;

function TForm1.SetCurrentPrinter: boolean;
begin
  result := false;
  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]);
  result := true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not SetCurrentPrinter then
    exit;

  Printer.Title := Caption;
  Printer.RawMode := True;
  Printer.BeginDoc;
  PrintSample;
  Printer.EndDoc;
end;

procedure TForm1.btnZPLClick(Sender: TObject);
begin
  if not SetCurrentPrinter then
    exit;

  Printer.Title := 'Zebra test';
  Printer.RawMode := True;
  Printer.BeginDoc;
  PrintZebraSample;
  Printer.EndDoc;
end;

end.