你之前不是发过一张帖子的吗?我也给了答案,再给一次吧:
unit DosPrinter;
// Note: Only Be Ok in Win98, and the printer must be in
// lpt1, lpt2 or lpt3;
// And Be Sure your Windows does not install the printer
//
// User Guide: Just add this unit into the "uses" clause, then you may
// call LPT1.Write, LPT1.WriteLn, LTP1.WriteFmt directly to
// make output on the printer. (LPT2 is also supported).
//
// Limitation: This unit does not have error checking capabilities.
// New added Guide:
// TDosPrinter;
// Can Check whether the printer is empty of paper, or
// printer does not linked, or other errors.
//
interface
uses Classes, SysUtils, Windows, MusicSys;
type
// 并口号
TDosLptPort = ( dpLpt1, dpLpt2, dpLpt3 );
// 错误类别, ( 未联机, 缺纸, 超时 )
TErrType = ( etLinkLost, etLackPaper, etTimeout );
// 打印错误事件
TErrMsgEvent = procedure(Sender: TObject; ErrType: TErrType;
var Retry: Boolean ) of object;
TDosPrinter = class
private
FLptPort: TDosLptPort;
FOnErr: TErrMsgEvent;
FblActive: Boolean;
protected
public
Constructor Create;
procedure BeginDoc;
procedure EndDoc;
procedure DoDoubleWidth( bl: Boolean );
procedure DoDoubleHeight( bl: Boolean );
procedure DoBold( bl: Boolean );
procedure ChineseMode;
procedure DoExpress( bl: Boolean );
procedure CR;
procedure Writeln( sLine: string );
procedure Write( sLine: string );
function WriteChar( Achar: char ): Boolean;
procedure MovePaper( iSize: integer );
property Active: Boolean read FblActive;
property LptPort: TDosLptPort read FLptPort write FLptPort;
property OnErr: TErrMsgEvent read FOnErr write FOnErr;
end;
function DosLpt1: TDosPrinter;
function DosLpt2: TDosPrinter;
implementation
var
_DosLpt1: TDosPrinter = nil;
_DosLpt2: TDosPrinter = nil;
function DosLpt1: TDosPrinter;
begin
if not Assigned( _DosLpt1 ) then
begin
_DosLpt1 := TDosPrinter.Create;
_DosLpt1.LptPort := dpLpt1;
end;
result := _DosLpt1;
end;
function DosLpt2: TDosPrinter;
begin
if not Assigned( _DosLpt2 ) then
begin
_DosLpt2 := TDosPrinter.Create;
_DosLpt2.LptPort := dpLpt2;
end;
result := _DosLpt2;
end;
{ TDosPrinter }
procedure TDosPrinter.BeginDoc;
begin
// Do nothing ...
end;
procedure TDosPrinter.ChineseMode;
begin
Write( #28 + '&' );
end;
procedure TDosPrinter.CR;
begin
Write( #13 );
end;
constructor TDosPrinter.Create;
begin
FLptPort := dpLpt1;
FblActive := True;
end;
procedure TDosPrinter.DoBold(bl: Boolean);
begin
if bl then
Write( #27 + 'E' )
else
Write( #27 + 'F' );
end;
procedure TDosPrinter.DoDoubleHeight(bl: Boolean);
begin
if bl then
Write( #27 + 'w' + #1 )
else
Write( #27 + 'w' + #0 );
end;
procedure TDosPrinter.DoDoubleWidth(bl: Boolean);
begin
if bl then
Write( #27 + 'W' + #1 )
else
Write( #27 + 'W' + #0 );
end;
procedure TDosPrinter.DoExpress(bl: Boolean);
begin
if bl then
Write( #28 + 'x' + #1 )
else
Write( #28 + 'x' + #0 );
end;
procedure TDosPrinter.EndDoc;
begin
// Do nothing ...
end;
procedure TDosPrinter.MovePaper(iSize: integer);
begin
Write( #27 + 'J' + char( iSize mod 255 ) );
end;
procedure TDosPrinter.Write(sLine: string);
var
index: longint;
begin
for Index := 1 to length( sLine ) do
if not WriteChar( sLine[Index] ) then
Break;
end;
function TDosPrinter.WriteChar( AChar: char): Boolean;
var
byteChar, byteStatus: Byte;
wordLpt: Word;
bPaperOut, bSelected, bIOError, bTimeOut, bOK: Boolean;
// below is new added by Musicwind, 2001-02-08
FErrType: TErrType;
Retry: Boolean;
dwTimeOut: DWORD;
begin
result := False;
if not mscIsWin98 then
begin
FblActive := result;
Exit;
end;
byteChar := byte( AChar );
if FLptPort = dpLpt1 then
wordLpt := 0 else
if FLptPort = dpLpt2 then
wordLpt := 1 else
if FLptPort = dpLpt3 then
wordLpt := 2
else
wordLpt := 0;
repeat
retry := False;
byteStatus := $40;
while (( byteStatus and $80 ) = 0 ) and (( byteStatus and $40 ) <> 0 ) do
asm
MOV AH, 0
MOV DX, wordLpt
MOV AL, byteChar
INT 17H
MOV byteStatus, AH
end;
bTimeOut := ( byteStatus and $01 ) <> 0;
bIOError := ( byteStatus and $08 ) <> 0;
bSelected := ( byteStatus and $10 ) <> 0;
bPaperOut := ( byteStatus and $20 ) <> 0;
if bTimeOut then
FErrType := etTimeOut
else
if bSelected and bPaperOut and bIOError then
FErrType := etLackPaper
else
if bSelected and bPaperOut or bIOError then
FErrType := etLinkLost
else
begin
// Print content
result := True;
end;
if not result then
begin
Retry := False;
if Assigned( FOnErr ) then
begin
Retry := True;
FOnErr( Self, FErrType, Retry );
end;
end;
until result or not Retry;
FblActive := result;
if not FblActive then
raise Exception.Create( '打印出错!' );
end;
procedure TDosPrinter.Writeln(sLine: string);
begin
Write( sLine + #13#10 );
end;
initialization
finalization
end.
unit DosPrinter;
// Note: Only Be Ok in Win98, and the printer must be in
// lpt1, lpt2 or lpt3;
// And Be Sure your Windows does not install the printer
//
// User Guide: Just add this unit into the "uses" clause, then you may
// call LPT1.Write, LPT1.WriteLn, LTP1.WriteFmt directly to
// make output on the printer. (LPT2 is also supported).
//
// Limitation: This unit does not have error checking capabilities.
// New added Guide:
// TDosPrinter;
// Can Check whether the printer is empty of paper, or
// printer does not linked, or other errors.
//
interface
uses Classes, SysUtils, Windows, MusicSys;
type
// 并口号
TDosLptPort = ( dpLpt1, dpLpt2, dpLpt3 );
// 错误类别, ( 未联机, 缺纸, 超时 )
TErrType = ( etLinkLost, etLackPaper, etTimeout );
// 打印错误事件
TErrMsgEvent = procedure(Sender: TObject; ErrType: TErrType;
var Retry: Boolean ) of object;
TDosPrinter = class
private
FLptPort: TDosLptPort;
FOnErr: TErrMsgEvent;
FblActive: Boolean;
protected
public
Constructor Create;
procedure BeginDoc;
procedure EndDoc;
procedure DoDoubleWidth( bl: Boolean );
procedure DoDoubleHeight( bl: Boolean );
procedure DoBold( bl: Boolean );
procedure ChineseMode;
procedure DoExpress( bl: Boolean );
procedure CR;
procedure Writeln( sLine: string );
procedure Write( sLine: string );
function WriteChar( Achar: char ): Boolean;
procedure MovePaper( iSize: integer );
property Active: Boolean read FblActive;
property LptPort: TDosLptPort read FLptPort write FLptPort;
property OnErr: TErrMsgEvent read FOnErr write FOnErr;
end;
function DosLpt1: TDosPrinter;
function DosLpt2: TDosPrinter;
implementation
var
_DosLpt1: TDosPrinter = nil;
_DosLpt2: TDosPrinter = nil;
function DosLpt1: TDosPrinter;
begin
if not Assigned( _DosLpt1 ) then
begin
_DosLpt1 := TDosPrinter.Create;
_DosLpt1.LptPort := dpLpt1;
end;
result := _DosLpt1;
end;
function DosLpt2: TDosPrinter;
begin
if not Assigned( _DosLpt2 ) then
begin
_DosLpt2 := TDosPrinter.Create;
_DosLpt2.LptPort := dpLpt2;
end;
result := _DosLpt2;
end;
{ TDosPrinter }
procedure TDosPrinter.BeginDoc;
begin
// Do nothing ...
end;
procedure TDosPrinter.ChineseMode;
begin
Write( #28 + '&' );
end;
procedure TDosPrinter.CR;
begin
Write( #13 );
end;
constructor TDosPrinter.Create;
begin
FLptPort := dpLpt1;
FblActive := True;
end;
procedure TDosPrinter.DoBold(bl: Boolean);
begin
if bl then
Write( #27 + 'E' )
else
Write( #27 + 'F' );
end;
procedure TDosPrinter.DoDoubleHeight(bl: Boolean);
begin
if bl then
Write( #27 + 'w' + #1 )
else
Write( #27 + 'w' + #0 );
end;
procedure TDosPrinter.DoDoubleWidth(bl: Boolean);
begin
if bl then
Write( #27 + 'W' + #1 )
else
Write( #27 + 'W' + #0 );
end;
procedure TDosPrinter.DoExpress(bl: Boolean);
begin
if bl then
Write( #28 + 'x' + #1 )
else
Write( #28 + 'x' + #0 );
end;
procedure TDosPrinter.EndDoc;
begin
// Do nothing ...
end;
procedure TDosPrinter.MovePaper(iSize: integer);
begin
Write( #27 + 'J' + char( iSize mod 255 ) );
end;
procedure TDosPrinter.Write(sLine: string);
var
index: longint;
begin
for Index := 1 to length( sLine ) do
if not WriteChar( sLine[Index] ) then
Break;
end;
function TDosPrinter.WriteChar( AChar: char): Boolean;
var
byteChar, byteStatus: Byte;
wordLpt: Word;
bPaperOut, bSelected, bIOError, bTimeOut, bOK: Boolean;
// below is new added by Musicwind, 2001-02-08
FErrType: TErrType;
Retry: Boolean;
dwTimeOut: DWORD;
begin
result := False;
if not mscIsWin98 then
begin
FblActive := result;
Exit;
end;
byteChar := byte( AChar );
if FLptPort = dpLpt1 then
wordLpt := 0 else
if FLptPort = dpLpt2 then
wordLpt := 1 else
if FLptPort = dpLpt3 then
wordLpt := 2
else
wordLpt := 0;
repeat
retry := False;
byteStatus := $40;
while (( byteStatus and $80 ) = 0 ) and (( byteStatus and $40 ) <> 0 ) do
asm
MOV AH, 0
MOV DX, wordLpt
MOV AL, byteChar
INT 17H
MOV byteStatus, AH
end;
bTimeOut := ( byteStatus and $01 ) <> 0;
bIOError := ( byteStatus and $08 ) <> 0;
bSelected := ( byteStatus and $10 ) <> 0;
bPaperOut := ( byteStatus and $20 ) <> 0;
if bTimeOut then
FErrType := etTimeOut
else
if bSelected and bPaperOut and bIOError then
FErrType := etLackPaper
else
if bSelected and bPaperOut or bIOError then
FErrType := etLinkLost
else
begin
// Print content
result := True;
end;
if not result then
begin
Retry := False;
if Assigned( FOnErr ) then
begin
Retry := True;
FOnErr( Self, FErrType, Retry );
end;
end;
until result or not Retry;
FblActive := result;
if not FblActive then
raise Exception.Create( '打印出错!' );
end;
procedure TDosPrinter.Writeln(sLine: string);
begin
Write( sLine + #13#10 );
end;
initialization
finalization
end.
解决方案 »
- delphi中,双击该组件出现是一个错误提示对话框,不能进入代码编辑区,请问为什么?
- 读中文的速度如何控制呀?
- 请教关于DELPHI中"EDIT"组件的问题?
- 功能强大的图形控件推荐给大家。和大家一起分享/...
- 一个EXE文件中,两个窗体,能否主窗体的最小化不影响另外一个?
- bit 类型字段显示问题
- 我真孤陋寡闻啊,今天才知道TurboPower开始公开源码,散分!!!!!!
- 我不知道问题出在哪!请高手帮帮忙!小弟感激不尽!!!
- 图片量化问题!
- TAdvStringgrid 是什么组件?
- DELPHI用的DB库好像不稳定??
- 一个在主程序中调用DLL的问题,请赐教,不好意思我只有40分了......
呵呵,
我用了,
AssignFile(lpt, 'LPT1' );
ReWrite(lpt );
Writeln(lpt, '先导科技有限公司' );
write(lpt,#13);
Writeln(lpt, 'sdfsdf' );
write(lpt,#13);
Writeln(lpt, 'sdfsdf' );
write(lpt,#13);
Writeln(lpt, 'sdfsdf' );
write(lpt,#13);
Writeln(lpt, 'sdfsdf' );
//write(lpt,#13);
//Write( lpt, #12 ); //打印机换页
//这里可以改成打印机的控制进退纸的命令.
CloseFile( lpt );
可是为什么,汉字变成乱码呢?
怎么解决呀
找不到呀