var PrnFileName: TextFile; begin Assignfile(PrnFileName,MoneyPort); Rewrite(PrnFileName); write(PRNFileName,CHR(27)+'p'+CHR(0)+CHR(60)+CHR(255) );或你打印的内容 CloseFile(PRNFileName);
unit Ufm26;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,printers;type TForm26 = class(TForm) Label1: TLabel; Label2: TLabel; Button1: TButton; Button2: TButton; procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form26: TForm26; procedure prnstr(str:String;j:integer); implementationuses Ufm13;{$R *.DFM}procedure TForm26.Button2Click(Sender: TObject); begin close; end;procedure TForm26.Button1Click(Sender: TObject); var str:string; fdname:array[1..12] of ShortString; i:integer; strspace:pchar; begin //try Assignprn(prnfile); Rewrite(prnfile); for i:=0 to 2 do writeln(prnfile); //空打三行 strspace:=' '; //将标题前赋空格 for i:=0 to 100 do write(prnfile,strpas(strspace)); form13.Label1.Font.Size :=20; Printer.Canvas.Font :=form13.Label1.Font;// Memo1.Font;将lable1字体的大小赋给画布 str:=form13.Label1.Caption; writeln(prnfile,str); writeln(prnfile); for i:=0 to form13.table1.FieldCount-4 do begin form13.DBGrid1.Columns.Items[i].Title.Font.Size:=14; Printer.Canvas.Font :=form13.DBGrid1.Columns.Items[i].Title.Font;// Memo1.Font; end; write(prnfile,'| '); for i:=0 to form13.table1.FieldCount-4 do begin write(prnfile,form13.table1.fields[i].fieldname); if i=0 then write(prnfile,' | ') else if i=1 then write(prnfile,' |') else write(prnfile,'|'); end; writeln(prnfile); form13.DBGrid1.Font.Size:=14; Printer.Canvas.Font :=form13.DBGrid1.Font; with form13.table1 Do begin first; while Not EOF Do begin fdname[1]:=FieldByName('日期').AsString; fdname[2]:=FieldByName('时间').AsString; fdname[3]:=FieldByName('媒水温度').AsString; fdname[4]:=FieldByName('出水温度').AsString; fdname[5]:=FieldByName('回水温度').AsString; fdname[6]:=FieldByName('燃烧机').AsString; fdname[7]:=FieldByName('循环泵').AsString; fdname[8]:=FieldByName('补水阀').AsString; fdname[9]:=FieldByName('排烟温度').AsString; // fdname[10]:=FieldByName('卫热温度').AsString; // fdname[11]:=FieldByName('卫热循环泵').AsString; // fdname[12]:=FieldByName('给水泵').AsString; // Writeln(prnfile,fdname[1],fdname[2],fdname[3],fdname[4],fdname[5],fdname[6],fdname[7],fdname[8],fdname[9]); for i:=1 to 9 do prnstr(trim(fdname[i]),i); writeln(prnfile); next; end; end; System.CloseFile(prnfile); // except // On eprinter Do // MessageDlg('没接打印机或打印机没有开,请连好打印机后再试', mtError,[mbOk], 0); end;procedure prnstr(str:String;j:integer); var //prnfile:Textfile; i,n,n0,m,num:integer; space:string;//pchar; begin space:='/'; num:=length(str); n0:=(8-num) div 2; n:=2*n0+1; if (j>5) and (j<9)then n:=n-2; for i:=1 to n do write(prnfile,space);//strpas(space)); write(prnfile,str); m:=8-num-n0; m:=2*m+1; if num<3 then m:=m-1; for i:=1 to m do write(prnfile,space);//strpas(space)); end; end.
用API直接打印uses CommDlg;{$IFNDEF WIN32}const MAX_PATH = 144;{$ENDIF}procedure TForm1.Button1Click(Sender: TObject);varPd : TPrintDlg;DocInfo: TDocInfo;beginFillChar(Pd, sizeof(Pd), #0);Pd.lStructSize := sizeof(Pd);Pd.hWndOwner := Form1.Handle;Pd.Flags := PD_RETURNDC;if PrintDlg(pd) then beginFillChar(DocInfo, sizeof(DocInfo), #0);DocInfo.cbSize := SizeOf(DocInfo);GetMem(DocInfo.lpszDocName, 32);GetMem(DocInfo.lpszOutput, MAX_PATH);lStrCpy(DocInfo.lpszDocName, 'My Document');{Add this line to print to a file }lStrCpy(DocInfo.lpszOutput, 'C:.doc');StartDoc(Pd.hDc, DocInfo);StartPage(Pd.hDc);TextOut(Pd.hDc, 100, 100, 'Page 1', 6);EndPage(Pd.hDc);StartPage(Pd.hDc);TextOut(Pd.hDc, 100, 100, 'Page 2', 6);EndPage(Pd.hDc);EndDoc(Pd.hDc);FreeMem(DocInfo.lpszDocName, 32);FreeMem(DocInfo.lpszOutput, MAX_PATH);end;end;
制定dbgrid就可以把该dbgrid中的
东西打印出来.
Printer.Canvas.TexOut(100,100,'ffgdfg');
Printer.EndDoc;
PrnFileName: TextFile;
begin
Assignfile(PrnFileName,MoneyPort);
Rewrite(PrnFileName); write(PRNFileName,CHR(27)+'p'+CHR(0)+CHR(60)+CHR(255) );或你打印的内容
CloseFile(PRNFileName);
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,printers;type
TForm26 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form26: TForm26;
procedure prnstr(str:String;j:integer);
implementationuses Ufm13;{$R *.DFM}procedure TForm26.Button2Click(Sender: TObject);
begin
close;
end;procedure TForm26.Button1Click(Sender: TObject);
var
str:string;
fdname:array[1..12] of ShortString;
i:integer;
strspace:pchar;
begin
//try
Assignprn(prnfile);
Rewrite(prnfile);
for i:=0 to 2 do writeln(prnfile); //空打三行
strspace:=' '; //将标题前赋空格
for i:=0 to 100 do write(prnfile,strpas(strspace));
form13.Label1.Font.Size :=20;
Printer.Canvas.Font :=form13.Label1.Font;// Memo1.Font;将lable1字体的大小赋给画布
str:=form13.Label1.Caption;
writeln(prnfile,str);
writeln(prnfile);
for i:=0 to form13.table1.FieldCount-4 do begin
form13.DBGrid1.Columns.Items[i].Title.Font.Size:=14;
Printer.Canvas.Font :=form13.DBGrid1.Columns.Items[i].Title.Font;// Memo1.Font;
end;
write(prnfile,'| ');
for i:=0 to form13.table1.FieldCount-4 do
begin
write(prnfile,form13.table1.fields[i].fieldname);
if i=0 then write(prnfile,' | ')
else if i=1 then write(prnfile,' |')
else write(prnfile,'|');
end;
writeln(prnfile);
form13.DBGrid1.Font.Size:=14;
Printer.Canvas.Font :=form13.DBGrid1.Font;
with form13.table1 Do
begin
first;
while Not EOF Do
begin
fdname[1]:=FieldByName('日期').AsString;
fdname[2]:=FieldByName('时间').AsString;
fdname[3]:=FieldByName('媒水温度').AsString;
fdname[4]:=FieldByName('出水温度').AsString;
fdname[5]:=FieldByName('回水温度').AsString;
fdname[6]:=FieldByName('燃烧机').AsString;
fdname[7]:=FieldByName('循环泵').AsString;
fdname[8]:=FieldByName('补水阀').AsString;
fdname[9]:=FieldByName('排烟温度').AsString;
// fdname[10]:=FieldByName('卫热温度').AsString;
// fdname[11]:=FieldByName('卫热循环泵').AsString;
// fdname[12]:=FieldByName('给水泵').AsString;
// Writeln(prnfile,fdname[1],fdname[2],fdname[3],fdname[4],fdname[5],fdname[6],fdname[7],fdname[8],fdname[9]);
for i:=1 to 9 do prnstr(trim(fdname[i]),i);
writeln(prnfile);
next;
end;
end;
System.CloseFile(prnfile);
// except
// On eprinter Do
// MessageDlg('没接打印机或打印机没有开,请连好打印机后再试', mtError,[mbOk], 0);
end;procedure prnstr(str:String;j:integer);
var
//prnfile:Textfile;
i,n,n0,m,num:integer;
space:string;//pchar;
begin
space:='/';
num:=length(str);
n0:=(8-num) div 2;
n:=2*n0+1;
if (j>5) and (j<9)then n:=n-2;
for i:=1 to n do write(prnfile,space);//strpas(space));
write(prnfile,str);
m:=8-num-n0;
m:=2*m+1;
if num<3 then m:=m-1;
for i:=1 to m do write(prnfile,space);//strpas(space));
end;
end.
用API直接打印uses CommDlg;{$IFNDEF WIN32}const MAX_PATH = 144;{$ENDIF}procedure TForm1.Button1Click(Sender: TObject);varPd : TPrintDlg;DocInfo: TDocInfo;beginFillChar(Pd, sizeof(Pd), #0);Pd.lStructSize := sizeof(Pd);Pd.hWndOwner := Form1.Handle;Pd.Flags := PD_RETURNDC;if PrintDlg(pd) then beginFillChar(DocInfo, sizeof(DocInfo), #0);DocInfo.cbSize := SizeOf(DocInfo);GetMem(DocInfo.lpszDocName, 32);GetMem(DocInfo.lpszOutput, MAX_PATH);lStrCpy(DocInfo.lpszDocName, 'My Document');{Add this line to print to a file }lStrCpy(DocInfo.lpszOutput, 'C:.doc');StartDoc(Pd.hDc, DocInfo);StartPage(Pd.hDc);TextOut(Pd.hDc, 100, 100, 'Page 1', 6);EndPage(Pd.hDc);StartPage(Pd.hDc);TextOut(Pd.hDc, 100, 100, 'Page 2', 6);EndPage(Pd.hDc);EndDoc(Pd.hDc);FreeMem(DocInfo.lpszDocName, 32);FreeMem(DocInfo.lpszOutput, MAX_PATH);end;end;