我不知道下面的能否解决你的问题:是个空件:只要在打印之前,把PrinterName赋为你安装的
打印机名称。(不是我写的)
//RawPrinter.pas
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)
Word;
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.