200分求解打印问题(200分)

  • 主题发起人 主题发起人 heping_soft
  • 开始时间 开始时间
H

heping_soft

Unregistered / Unconfirmed
GUEST, unregistred user!
我使用api函数来进行打印,如果用到tlist类的话,程序就不打印,不用tlist类的话,程序就可以打印
源码如下
type
PdcArray = ^TdcArray;
TdcArray = array[0..0] of hdc;
pMyPrintRecord = ^TMyPrintRecord;
TMyPrintRecord = record
PrintName:string;
PrintDriver:string;
PrintPort:string;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
PrintQueue:TList;
pMyRecord:pMyPrintRecord;
begin
PrintQueue:= TList.Create;
New(pMyRecord);
pMyRecord^.PrintName :='Smart Print';
GetTheListOfPrinters(pMyRecord);
PrintQueue.Add(pMyRecord);
PrintToSmallTicket(PrintQueue,False);
Dispose(pMyRecord);
PrintQueue.Free;
end;

procedure GetTheListOfPrinters(vPrintName:pMyPrintRecord);
var
ReturnPrint:PChar;
pPrintName:PChar;
sDriver,sPort:string;
begin
GetMem(ReturnPrint, 32767);
pPrintName := ReturnPrint;
GetMem(pPrintName,32767);
StrPCopy(pPrintName, vPrintName^.PrintName);
if GetProfileString('devices', pPrintName, '',ReturnPrint, 32767) <> 0 then
begin
sDriver := StrPas(ReturnPrint);
sPort := sDriver;
Delete(sDriver, Pos(',', sDriver), Length(sDriver));
Delete(sPort, 1, Pos(',', sPort));
vPrintName^.PrintDriver := sDriver;
vPrintName^.PrintPort := sPort;
end;
FreeMem(ReturnPrint,32767);
FreeMem(pPrintName,32767);
end;

procedure PrintToSmallTicket(vContext:TList;vOut:Boolean);
var
TotalHeight,i:Integer;
pPrintRecord:pMyPrintRecord;
temp:Real;
dcs : PdcArray;
{ hdc 数组(指针)}
dcsCount: integer;
{hdc 数量}
pPrinter : pChar;
{指向打印机名称}
pDriver : pChar;
{指向打印机驱动文件名}
pPort : pChar;
{指向打印机端口}
do
cInfo: TDocInfo;
{打印的信息}
{$IFDEF WIN32}
osv : TOSVERSIONINFO;
{测试是否在 Windows NT 下}
{$ENDIF}
dc : hdc;
{测试 hdc}
begin
if vContext.Count <=0 then
Exit;
{如果 DELPHI 的 Range Checking 是打开的记得要把它关上}
{这是为了允许跳过 dcs 数组的空元素,而没有编译错误或者运行时错误}
{$IFOPT R+}
{$DEFINE CKRANGE}
{$R-}
{$ENDIF}
{为 dcs 分配空间}
GetMem(dcs, sizeof(hdc) * vContext.Count);
dcsCount := 0;
//得到选择的每一个打印机并为每个打印机分配设备句柄
for i:=0 to vContext.Count -1do
begin
pPrintRecord := vContext.Items;
GetMem(pPrinter, Length(pPrintRecord^.PrintName) + 1);
GetMem(pDriver, Length(pPrintRecord^.PrintDriver) + 1);
GetMem(pPort, Length(pPrintRecord^.PrintPort) + 1);
StrPCopy(pPrinter, pPrintRecord^.PrintName);
StrPCopy(pDriver, pPrintRecord^.PrintDriver);
StrPCopy(pPort, pPrintRecord^.PrintPort);
GetVersionEx(osv);
if osv.dwPlatformId = VER_PLATFORM_WIN32_NT then
begin
PrintDC := CreateDc(pDriver, pPrinter, nil, nil);
end
else
begin
PrintDC := CreateDc(nil, pPrinter, nil, nil);
end;
FreeMem(pPrinter, Length(pPrintRecord^.PrintName));
FreeMem(pDriver, Length(pPrintRecord^.PrintDriver));
FreeMem(pPort, Length(pPrintRecord^.PrintPort));
if PrintDC <> 0 then
begin
dcs^[dcsCount] := PrintDC;
inc(dcsCOunt);
end;
end;
{现在打印试试吧}
if dcsCount > 0 then
begin

{为每个打印机分配一个文档}
GetMem(DocInfo.lpszDocName, 32);
for i := 0 to (dcsCount - 1)do
begin
do
cInfo.cbSize := SizeOf(DocInfo);
StrPCopy(DocInfo.lpszDocName, 'Testdo
c' + IntToStr(i));
StartDoc(dcs^,do
cInfo);
end;
FreeMem(DocInfo.lpszDocName, 32);
{准备让每个打印机打印一页}
for i := 0 to (dcsCount - 1)do
StartPage(dcs^);
{开始打印‘这是打印测试!’}
for i := 0 to (dcsCount - 1)do
TextOut(dcs^, 200, 200, ' 这是打印测试! ', Length(' 这是打印测试! '));
{结束一页}
for i := 0 to (dcsCount - 1)do
EndPage(dcs^);
{退出打印}
for i := 0 to (dcsCount - 1)do
EndDoc(dcs^);
{删除打印机}
for i := 0 to (dcsCount - 1)do
DeleteDc(dcs^);
end;

FreeMem(dcs, sizeof(hdc) * vContext.Count);
{If range checking was on when we started then
turn it back on}
{$IFDEF CKRANGE}
{$UNDEF CKRANGE}
{$R+}
{$ENDIF}
end;
end.
 

Similar threads

I
回复
0
查看
542
import
I
I
回复
0
查看
639
import
I
后退
顶部