为什么这个程序会吃系统资源? 烦请高手指点.(100分)

  • 主题发起人 主题发起人 wmsoft
  • 开始时间 开始时间
W

wmsoft

Unregistered / Unconfirmed
GUEST, unregistred user!
程序代码如下: 用它试一下, 系统资源肯定会越来越少.

uses rawprn;

procedure Tspglform.Button1Click(Sender: TObject);
var Myprinter : TRawPrinter;
begin

MyPrinter:=TRawPrinter.Create;
myprinter.startraw;
myprinter.write(chr(13)+chr(10));
myprinter.endraw;
myprinter.free;
end;


/////////////////////////

unit rawprn;

interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, printers;

type TRawprinter =class(TPrinter)
public
dc2 : HDC;
procedure startraw;
procedure endraw;
procedure write(const s : string);
procedure writeln(const s : string);
end;


//var

implementation

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(const s : string);
begin

// pass.l:=2;
// strpcopy(pass.s,#13#10);
pass.l:=length(s)+2;
strpcopy(pass.s,s+#13#10);
escape(dc2,PASSTHROUGH,0,@pass,nil);
end;


end.


 
是不是漏了 DeleteDC(dc2)?在 EndDraw 中加上?
 
我也出够这样的问题
加上DeleteDC(dc2)没问题好了
 
谢谢! 分钱! yi_min兄, 你晚了一点点哦.
 
后退
顶部