怎样在windows下实现逐行打印???高手救命(150分)

  • 主题发起人 主题发起人 cyf_00002
  • 开始时间 开始时间
C

cyf_00002

Unregistered / Unconfirmed
GUEST, unregistred user!
写一个POS机的前端,要求扫描一件商品打印一行商品的信息,
而用PRINTER.begin
DOC PRINTER.ENDDOC 会自动加上页头和页尾,如果不加这两个函数,打印机又没有反映;我曾经试着通过SETPRINTER函数,对PAGE的属性改的乱七八糟,一点效果也没有,请高手帮忙????????????????????
不胜感激
 
给你,好用:
{---------------------------------------------------}
{ Copyright (c)2000 by Przemyslaw Jankowski }
{ e-mail: pjank@home.pl }
{---------------------------------------------------}
{ }
{ >> TRAWPrinter << component }
{ just put it on your form }
{ and you can send RAW data to the printer }
{ or simply print in text mode (just like indo
S) }
{ }
{ Here's all you need to know: }
{ properties: }
{ - PrinterName }
{ the name of the printer you want to use }
{ to use the default printer - leave this blank }
{ -do
cumentTitle }
{ the text that is listed in the Print Manager }
{ methods: }
{ - begin
Doc - this starts a new print job }
{ if PrinterName was empty, it will contain }
{ the default printer name }
{ - EndDoc - call this when you finish sending data }
{ - Abort - if you want to abort current print-job }
{ note: it will cancel only this part of the data }
{ that didn't yet go the printer }
{ - Write - use this between begin
Doc and EndDoc }
{ sends <Count> bytes of <Buffer> to the printer }
{ }
{ To make things easier there're more methods: }
{ - WriteStr - just pass any string as parameter }
{ - WriteLn - like the above but adds CRLF (#10#13) }
{ - NextPage - sends FormFeed code (#12) }
{ - CR - sends only CarrigeReturn (#13) }
{ - LF - sends only LineFeed (#10) }
{ }
{ All methods return: }
{ - True - if everything was O.K. }
{ - False - if something failed }
{ }
{---------------------------------------------------}
unit RAWPrinter;
{$B-}
interface
uses
Windows, SysUtils, Classes;
type
TRAWPrinter = class(TComponent)
constructor Create(AOwner:TComponent);
override;
destructor Destroy;
override;
private
fDocumentTitle: string;
fPrinterName: string;
fPrinterHandle: DWord;
fLevel: Integer;
// 0 - printer closed
// 1 - printer open
// 2 - job started
// 3 - page started
public
function SetToDefaultPrinter:Boolean;
function begin
Doc:Boolean;
function EndDoc:Boolean;
function Abort:Boolean;
function Write(const Buffer;
Count:Longint):DWord;
function WriteStr(const text:string):Boolean;
function WriteLn(const line:string):Boolean;
function NextPage:Boolean;
function CR:Boolean;
function LF:Boolean;
published
property PrinterName:string read fPrinterName write fPrinterName;
propertydo
cumentTitle:string read fDocumentTitle write fDocumentTitle;
end;

procedure Register;
implementation
uses
WinSpool;

procedure Register;
begin
RegisterComponents('PJank', [TRAWPrinter]);
end;

{ TRAWPrinter }
(***********************************************************************************)
constructor TRAWPrinter.Create(AOwner: TComponent);
begin
inherited;
fDocumentTitle:= '';
fPrinterName:= '';
fLevel:= 0;
end;

destructor TRAWPrinter.Destroy;
begin
if (fLevel>0) then
Abort;
inherited;
end;

(***********************************************************************************)
function TRAWPrinter.SetToDefaultPrinter:Boolean;
var str: array[0..79]of Char;
function FetchStr(s:string):string;
var i: Integer;
begin
s:= TrimLeft(s);
i:= Pos(',',s);
if i=0
then
Result:= s
else
Result:= Copy(s, 1, i-1);
end;
begin
GetProfileString('windows', 'device', '', str, SizeOf(str)-1);
fPrinterName:= FetchStr(str);
Result:= (fPrinterName<>'');
end;

(***********************************************************************************)
function TRAWPrinter.begin
Doc: Boolean;
vardo
cInfo: TDocInfo1;
begin
if (fLevel=0) then
begin
// open printer
if (fPrinterName='') then
SetToDefaultPrinter;
if (fPrinterName<>'') and OpenPrinter(PChar(fPrinterName), fPrinterHandle, nil)
then
fLevel:= 1;
end;
if (fLevel=1) then
begin
// start new job
withdo
cInfodo
begin
pDocName:= PChar(fDocumentTitle);
pOutputFile:= nil;
pDatatype:= 'RAW';
end;
if (StartDocPrinter(fPrinterHandle, 1, @DocInfo)<>0)
then
fLevel:= 2;
end;
if (fLevel=2) then
// start new page
if StartPagePrinter(fPrinterHandle)
then
fLevel:= 3;
Result:= (fLevel=3);
end;

function TRAWPrinter.EndDoc: Boolean;
begin
if (fLevel=3) then
if EndPagePrinter(fPrinterHandle) then
fLevel:= 2;
if (fLevel=2) then
if EndDocPrinter(fPrinterHandle) then
fLevel:= 1;
if (fLevel=1) then
if ClosePrinter(fPrinterHandle) then
fLevel:= 0;
Result:= (fLevel=0);
end;

function TRAWPrinter.Abort: Boolean;
begin
if (fLevel>1) then
if AbortPrinter(fPrinterHandle) then
fLevel:= 1;
if (fLevel=1) then
if ClosePrinter(fPrinterHandle) then
fLevel:= 0;
Result:= (fLevel=0);
end;

(***********************************************************************************)
function TRAWPrinter.Write(const Buffer;
Count: Integer): DWord;
begin
Result:= 0;
if (fLevel=3) then
WritePrinter(fPrinterHandle, Pointer(Buffer), Count, Result);
end;

function TRAWPrinter.WriteStr(const text: string): Boolean;
var len: DWord;
begin
len:= Length(text);
Result:= (Write(text,len)=len);
end;

function TRAWPrinter.WriteLn(const line: string): Boolean;
begin
Result:= WriteStr(line+#10#13);
end;

function TRAWPrinter.NextPage: Boolean;
begin
Result:= WriteStr(#12);
end;

function TRAWPrinter.CR: Boolean;
begin
Result:= WriteStr(#13);
end;

function TRAWPrinter.LF: Boolean;
begin
Result:= WriteStr(#10);
end;

(***********************************************************************************)
end.

//示例
var
MyRawPrinter: TRawPrinter;
OldPrinter: TPrinter;
{$R *.dfm}
procedure TFormPrinterTest.btnRawPrinterClick(Sender: TObject);
begin
OldPrinter := SetPrinter(MyRawPrinter);
try
MyRawPrinter.StartRaw;
MyRawPrinter.Canvas.Font.Name := '宋体';
MyRawPrinter.Canvas.Font.Size := 12;
MyRawPrinter.Write(' ===___________汽车衡记录单=== ');
MyRawPrinter.Writeln;
MyRawPrinter.Canvas.Font.Size := 9;
MyRawPrinter.Write('┌────────────────────────────┐');
MyRawPrinter.Writeln;
MyRawPrinter.EndRaw;
finally
SetPrinter(OldPrinter);
end;
end;

procedure TFormPrinterTest.FormCreate(Sender: TObject);
begin
MyRawPrinter := TRawPrinter.Create;
end;

procedure TFormPrinterTest.FormDestroy(Sender: TObject);
begin
MyRawPrinter.Free;
end;
 
with Printerdo
begin
begin
Doc;
Canvas.TextRect(Rect(200,200,PageWidth-200,PageHeight-200),
200, 200, yourinformation);
EndDoc;
end;
 
逐行打印只能用针打实现。
我的系统也有类似要求,用RawPrinter实现的,如楼上。 
 
var
F:TEXTFILE;
begin
ASSIGNFILE(F,'LPT1');
REWRITE(F);
WRITELN(F,chr(27) '@');
//initial the printer
WRITELN(F,'TEST1 FIRST LINE');你的内容
WRITELN(F,chr(12));
// move the paper to a new page
CLOSEFILE(F);
end;
 
To LabelSoft:
你的RawPrint是控件吗?我没搜到,能发给我一个吗?谢谢! Gselec@163.com
 
To LabelSoft:
给我发一个吧,多谢谢了。 chenjianzu@163.net
 
copy c:/1.txt prn
 
生成 一个 文本文件
复制到 打印机。copy c:/1.txt prn
 
不知道能不能给我发一个呀?wuhaihao11@163.com
先谢了。
 
也给我一个谢谢了。gyn1981@263.net
 
好久没来,这么多要的,全发了
 
也给我一个谢谢了。crabking@21cn.net
 
给我一个控件我就给分
 
后退
顶部