从别人那里抄的:
var Myprinter : TRawPrinter;
oldprinter : TPrinter;
begin
MyPrinter:=TRawPrinter.Create;
oldprinter:=setprinter(MyPrinter);
try
if Printdialog1.execute then
begin
myprinter.startraw;
myprinter.write('khsdhskhkshdksd');
myprinter.writeln;
myprinter.endraw;
end
finally
setprinter(oldprinyter);
myprinter.free;
end
end;
Here is the code for the raw printer object.
unit Rawprinter;
interface
uses printers,windows;
type TRawprinter =class(TPrinter)
public
dc2 : HDC;
procedure startraw;
procedure endraw;
procedure write(const s : string);
procedure writeln;
end;
implementation
uses sysutils,forms;
function AbortProc(Prn: HDC;
Error: Integer): Bool;
stdcall;
begin
Application.ProcessMessages;
Result := not Printer.Aborted;
end;
type
TPrinterDevice = class
Driver, Device, Port: String;
constructor Create(ADriver, ADevice, APort: PChar);
function IsEqual(ADriver, ADevice, APort: PChar): Boolean;
end;
constructor TPrinterDevice.Create(ADriver, ADevice, APort: PChar);
begin
inherited Create;
Driver := ADriver;
Device := ADevice;
Port := APort;
end;
function TPrinterDevice.IsEqual(ADriver, ADevice, APort: PChar): Boolean;
begin
Result := (Device = ADevice) and (Port = APort);
end;
procedure TRawprinter.startraw;
var
CTitle: array[0..31] of Char;
CMode : Array[0..4] of char;
do
cInfo: TDocInfo;
r : integer;
begin
StrPLCopy(CTitle, Title, SizeOf(CTitle) - 1);
StrPCopy(CMode, 'RAW');
FillChar(DocInfo, SizeOf(DocInfo), 0);
withdo
cInfodo
begin
cbSize := SizeOf(DocInfo);
lpszDocName := CTitle;
lpszOutput := nil;
lpszDatatype :=CMode;
end;
with TPrinterDevice(Printers.Objects[PrinterIndex])do
begin
DC2 := CreateDC(PChar(Driver), PChar(Device), PChar(Port), nil);
end;
SetAbortProc(dc2, AbortProc);
r:=StartDoc(dc2,do
cInfo);
end;
procedure TRawprinter.endraw;
var r : integer;
begin
r:=windows.enddoc(dc2);
end;
type passrec = packed record
l : word;
s : Array[0..255] of char;
end;
var pass : Passrec;
procedure TRawprinter.write(const s : string);
begin
pass.l:=length(s);
strpcopy(pass.s,s);
escape(dc2,PASSTHROUGH,0,@pass,nil);
end;
procedure TRawprinter.writeln;
begin
pass.l:=2;
strpcopy(pass.s,#13#10);
escape(dc2,PASSTHROUGH,0,@pass,nil);
end;
end.