Delphi-Projects
410 строк · 9.5 Кб
1unit CustomBlank;
2
3interface
4
5uses
6Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7Printers, ExtCtrls;
8
9type
10TCustomBlank = class
11private
12{ Private declarations }
13FMetaFile: TMetaFile;
14FMetafileCanvas: TMetafileCanvas;
15FPixelsPerInchX: Integer;
16FPixelsPerInchY: Integer;
17FTopMargin: Integer;
18FLeftMargin: Integer;
19FHeight: Integer;
20FWidth: Integer;
21function GetFont: TFont;
22procedure SetFont(const Value: TFont);
23protected
24{ Protected declarations }
25function GetCanvas: TCanvas;
26public
27{ Public declarations }
28function xm(x: Integer): Integer;
29function ym(y: Integer): Integer;
30procedure ResetFont(Name: string = ''; Size: Integer = 0;
31Style: string = '');
32procedure FL(x, y, x2, y2: Integer);
33procedure RL(x, y, x2, y2: Integer);
34procedure HL(x, y, x2: Integer);
35procedure VL(x, y, y2: Integer);
36procedure TL(x, y: Integer; S: string); overload;
37procedure TL(x, y, x2: Integer; S: string); overload;
38procedure TC(x, y, x2: Integer; S: string);
39procedure TB(x, y, x2, y2: Integer; S: string);
40procedure TV(x, y, x2, y2: Integer; S: string);
41procedure BeginScreenDoc;
42procedure BeginPrinterDoc;
43procedure EndDoc;
44procedure DrawPrinterPage;
45procedure Preview;
46procedure Print(NumCopies: Integer = 1; const DocTitle: string = 'Бланк');
47procedure DrawRulers;
48procedure SaveImage(const FileName: string);
49constructor Create; virtual;
50destructor Destroy; override;
51property Canvas: TCanvas read GetCanvas;
52property Font: TFont read GetFont write SetFont;
53property TopMargin: Integer read FTopMargin write FTopMargin;
54property LeftMargin: Integer read FLeftMargin write FLeftMargin;
55property Width: Integer read FWidth write FWidth;
56property Height: Integer read FHeight write FHeight;
57published
58{ Published declarations }
59end;
60
61implementation
62
63uses
64PreviewUnit, Math;
65
66{ TCustomBlank }
67
68constructor TCustomBlank.Create;
69begin
70FPixelsPerInchX := Screen.PixelsPerInch;
71FPixelsPerInchY := Screen.PixelsPerInch;
72
73//A4(mm)
74FLeftMargin := 20;
75FTopMargin := 15;
76FWidth := 210;
77FHeight := 297;
78end;
79
80destructor TCustomBlank.Destroy;
81begin
82FMetafileCanvas.Free;
83FMetaFile.Free;
84inherited Destroy;
85end;
86
87procedure TCustomBlank.BeginScreenDoc;
88begin
89FPixelsPerInchX := Screen.PixelsPerInch;
90FPixelsPerInchY := Screen.PixelsPerInch;
91
92FMetaFile.Free;
93FMetaFile := TMetaFile.Create;
94with FMetaFile do
95begin
96Height := MulDiv(FHeight, FPixelsPerInchX * 10, 254);
97Width := MulDiv(FWidth, FPixelsPerInchY * 10, 254);
98end;
99
100FMetafileCanvas.Free;
101FMetafileCanvas := TMetafileCanvas.Create(FMetaFile, 0);
102with FMetafileCanvas do
103begin
104Font.Name := 'Times New Roman';
105Font.Size := 8;
106Font.Charset := RUSSIAN_CHARSET;
107Pen.Width := 1;
108end;
109end;
110
111procedure TCustomBlank.BeginPrinterDoc;
112begin
113FPixelsPerInchX := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
114FPixelsPerInchY := GetDeviceCaps(Printer.Handle, LOGPIXELSY);
115
116FMetaFile.Free;
117FMetaFile := TMetaFile.Create;
118with FMetaFile do
119begin
120Height := MulDiv(FHeight, FPixelsPerInchX * 10, 254);
121Width := MulDiv(FWidth, FPixelsPerInchY * 10, 254);
122end;
123
124FMetafileCanvas.Free;
125FMetafileCanvas := TMetafileCanvas.Create(FMetaFile, Printer.Handle);
126with FMetafileCanvas do
127begin
128Font.Name := 'Times New Roman';
129Font.Size := 8;
130Font.Charset := RUSSIAN_CHARSET;
131if FPixelsPerInchY <> Screen.PixelsPerInch then
132Font.Size := Round(Font.Size * FPixelsPerInchY / Screen.PixelsPerInch);
133Pen.Width := 1 + FPixelsPerInchY div 300;
134end;
135end;
136
137procedure TCustomBlank.EndDoc;
138begin
139FMetafileCanvas.Free;
140FMetafileCanvas := nil;
141end;
142
143function TCustomBlank.xm(x: Integer): Integer;
144begin
145Result := MulDiv(x, FPixelsPerInchX * 10, 254);
146end;
147
148function TCustomBlank.ym(y: Integer): Integer;
149begin
150if y < 0 then
151begin
152y := -y;
153Result := MulDiv(y, FPixelsPerInchY * 10, 254) -
154FMetafileCanvas.TextHeight('W');
155end
156else
157Result := MulDiv(y, FPixelsPerInchY * 10, 254);
158end;
159
160procedure TCustomBlank.HL(x, y, x2: Integer);
161begin
162with FMetafileCanvas do
163begin
164y := ym(y);
165MoveTo(xm(x), y);
166LineTo(xm(x2), y);
167end;
168end;
169
170procedure TCustomBlank.RL(x, y, x2, y2: Integer);
171begin
172with FMetafileCanvas do
173begin
174//Rectangle(xm(x), ym(y), xm(x2), ym(y2));
175x := xm(x); x2 := xm(x2);
176y := ym(y); y2 := ym(y2);
177MoveTo(x, y);
178LineTo(x2,y);
179LineTo(x2,y2);
180LineTo(x, y2);
181LineTo(x, y);
182end;
183end;
184
185procedure TCustomBlank.TB(x, y, x2, y2: Integer; S: string);
186var
187R: TRect;
188begin
189R := Rect(xm(x), ym(y), xm(x2), ym(y2));
190DrawText(FMetafileCanvas.Handle, PChar(S), Length(S), R,
191DT_LEFT or DT_NOPREFIX or DT_WORDBREAK);
192end;
193
194procedure TCustomBlank.TV(x, y, x2, y2: Integer; S: string);
195var
196R: TRect;
197begin
198R := Rect(xm(x), ym(y), xm(x2), ym(y2));
199DrawText(FMetafileCanvas.Handle, PChar(S), Length(S), R,
200DT_CENTER or DT_VCENTER or DT_NOPREFIX or DT_SINGLELINE);
201end;
202
203procedure TCustomBlank.TL(x, y: Integer; S: string);
204begin
205FMetafileCanvas.TextOut(xm(x), ym(y), S);
206end;
207
208procedure TCustomBlank.TL(x, y, x2: Integer; S: string);
209var
210R: TRect;
211begin
212with FMetafileCanvas do
213begin
214R.Top := ym(y);
215R.Left := xm(x);
216R.Bottom := R.Top + TextHeight(S);
217R.Right := xm(x2);
218TextRect(R, R.Left, R.Top, S);
219end;
220end;
221
222procedure TCustomBlank.TC(x, y, x2: Integer; S: string);
223begin
224with FMetafileCanvas do
225begin
226x := xm(x);
227Inc(x, (xm(x2) - x - TextWidth(S)) div 2);
228TextOut(x, ym(y), S);
229end;
230end;
231
232procedure TCustomBlank.VL(x, y, y2: Integer);
233begin
234with FMetafileCanvas do
235begin
236x := xm(x);
237MoveTo(x, ym(y));
238LineTo(x, ym(y2));
239end;
240end;
241
242procedure TCustomBlank.FL(x, y, x2, y2: Integer);
243begin
244with FMetafileCanvas do
245begin
246MoveTo(xm(x), ym(y));
247LineTo(xm(x2), ym(y2));
248end;
249end;
250
251procedure TCustomBlank.Preview;
252var
253Form: TPreviewForm;
254begin
255Form := TPreviewForm.Create(Application);
256try
257Form.ScrollBox.DisableAutoRange;
258with Form.Image do
259begin
260SetBounds(2, 2, FMetaFile.Width, FMetaFile.Height);
261Form.Bevel.SetBounds(Left, Top, Width + 2, Height + 2);
262Canvas.Draw(xm(LeftMargin), ym(TopMargin), FMetaFile);
263//Form.ScrollBox.VertScrollBar.Range := Form.Image.Height;
264end;
265Form.ScrollBox.EnableAutoRange;
266with Form do
267begin
268Height := Min(Form.Image.Height + 40, Screen.Height - 40);
269Width := Min(Form.Image.Width + 40, Screen.Width - 40);
270//Left := (Screen.Width - Width - 40) div 2;
271//Top := (Screen.Height - Height - 40) div 2;
272ShowModal;
273end;
274finally
275Form.Free;
276end;
277end;
278
279procedure TCustomBlank.Print(NumCopies: Integer = 1; const DocTitle: string = 'Бланк');
280begin
281Printer.Title := DocTitle;
282Printer.BeginDoc;
283while NumCopies > 0 do
284begin
285DrawPrinterPage;
286Dec(NumCopies);
287if NumCopies > 0 then
288Printer.NewPage;
289end;
290Printer.EndDoc;
291end;
292
293procedure TCustomBlank.DrawRulers;
294var
295I: Integer;
296begin
297with FMetafileCanvas do
298begin
299HL(0, 0, FWidth);
300VL(0, 0, FHeight);
301for I := 1 to (FWidth div 10) do
302begin
303VL(I * 10 - 5, 0, 1);
304VL(I * 10, 0, 3);
305TL(I * 10, 3, IntToStr(I));
306end;
307for I := 1 to (FHeight div 10) do
308begin
309HL(0, I * 10 - 5, 1);
310HL(0, I * 10, 3);
311TL(3, I * 10, IntToStr(I));
312end;
313end;
314end;
315
316function TCustomBlank.GetCanvas: TCanvas;
317begin
318Result := FMetafileCanvas;
319end;
320
321procedure TCustomBlank.DrawPrinterPage;
322var
323X, Y: Integer;
324begin
325X := xm(FLeftMargin);
326Y := ym(FTopMargin);
327Dec(X, GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX));
328Dec(Y, GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY));
329Printer.Canvas.Draw(X, Y, FMetaFile);
330end;
331
332function TCustomBlank.GetFont: TFont;
333var
334Font: TFont;
335begin
336Font := TFont.Create;
337try
338Font.Assign(FMetafileCanvas.Font);
339if FPixelsPerInchY <> Screen.PixelsPerInch then
340Font.Size := Round(Result.Size * Screen.PixelsPerInch / FPixelsPerInchY);
341Result.Assign(Font);
342finally
343Font.Free;
344end;
345end;
346
347procedure TCustomBlank.SetFont(const Value: TFont);
348begin
349with FMetafileCanvas.Font do
350begin
351Assign(Value);
352if FPixelsPerInchY <> Screen.PixelsPerInch then
353Size := Round(Size * FPixelsPerInchY / Screen.PixelsPerInch);
354end;
355end;
356
357procedure TCustomBlank.ResetFont(Name: string = ''; Size: Integer = 0;
358Style: string = '');
359begin
360with FMetafileCanvas do
361begin
362if Name <> '' then
363try
364Font.Name := Name;
365except
366;
367end;
368
369if Size > 0 then
370begin
371Font.Size := Size;
372if FPixelsPerInchY <> Screen.PixelsPerInch then
373Font.Size := Round(Font.Size * FPixelsPerInchY / Screen.PixelsPerInch);
374end;
375
376if Style <> '' then
377begin
378if Pos('N', Style) > 0 then
379Font.Style := [];
380if Pos('B', Style) > 0 then
381Font.Style := Font.Style + [fsBold];
382if Pos('I', Style) > 0 then
383Font.Style := Font.Style + [fsItalic];
384if Pos('U', Style) > 0 then
385Font.Style := Font.Style + [fsUnderline];
386end;
387Font.Charset := RUSSIAN_CHARSET;
388end;
389end;
390
391procedure TCustomBlank.SaveImage(const FileName: string);
392var
393Img: TBitmap;
394begin
395Img := TBitmap.Create;
396try
397Img.PixelFormat := pf1bit;
398Img.Height := FMetaFile.Height;
399Img.Width := FMetaFile.Width;
400Img.Canvas.Draw(xm(FLeftMargin),ym(FTopMargin),FMetaFile);
401Img.SaveToFile(FileName + '.bmp');
402// FMetaFile.Height :=
403// FMetaFile.Width :=
404FMetaFile.SaveToFile(FileName + '.emf');
405finally
406Img.Free;
407end;
408end;
409
410end.
411