打印控制,在线等待,可到csdn上取分(10分)

  • 主题发起人 主题发起人 blues hu
  • 开始时间 开始时间
B

blues hu

Unregistered / Unconfirmed
GUEST, unregistred user!
谁有控制针式打印机(链式)走纸的dll的源码.
以下是我凑的源代码,错误多多,还请各位指教.我的环境为delphi6.0+win2000
library setprint;
uses
Windows,
printers,
WinSpool,
SysUtils,
Registry,
Classes;
function SetPaperSize(nWidth,nHeight,nOrient:word):integer;
var
ADevice,ADriver,APort:string;
ADeviceMode:THandle;
DevMode:PDeviceMode;
begin

setlength(adevice,255);
setlength(adriver,255);
setlength(aport,255);
if adevicemode=0 then

begin

printer.PrinterIndex :=printer.PrinterIndex ;
printer.GetPrinter(pchar(adevice),pchar(adriver),pchar(aport),adevicemode);
end;

if adevicemode<>0 then

begin

devmode:=globallock(adevicemode);
devmode^.dmFields :=devmode^.dmFields or dm_copies;
devmode^.dmCopies :=1;
end;

with devmode^do

begin

case nOrient of
0: begin

devmode^.dmFields:=devmode^.dmFields or DM_ORIENTATION;
devmode^.dmOrientation:=DMORIENT_PORTRAIT;
end;

1: begin

devmode^.dmFields:=devmode^.dmFields or DM_ORIENTATION;
devmode^.dmOrientation:=DMORIENT_LANDSCAPE;
end;

end;

devmode^.dmFields:=DM_PAPERSIZE or DM_PAPERWIDTH or DM_PAPERLENGTH;
devmode^.dmPaperSize:=DMPAPER_USER;
devmode^.dmPaperWidth:=nWidth;
devmode^.dmPaperLength:=nHeight;
end;

globalunlock(adevicemode);
globalfree(adevicemode);
closeprinter(adevicemode);
result:=1;
end;

exports
SetPaperSize;
{$R *.res}
begin

end.

 
你可以参考《Delphi 5开发人员指南》书中的源码
 
Question: How to get the location of the canvas on the piece of paper that emerges from the printer?
Q:
Does anybody know how to get, programmatically, the location of the canvas
on the piece of paper that emerges from the printer? i.e. the size of the
top, left, right and bottom margins?
A:
procedure TPrtPvw.GetOffsetPrinter;
var
pt: TPoint;
tmpAncho,tmpAlto: longint;
begin
Escape(hPrinter,GETPRINTINGOFFSET,0,Nil,@pt);
gOffSetLeft:=pt.X;
gOffSetTop:=pt.Y;
Escape(hPrinter,GETPHYSPAGESIZE,0,Nil,@pt);
tmpAncho:=pt.X;
tmpAlto:=pt.Y;
gOffSetRight:=tmpAncho-gOffSetLeft-Printer.PageWidth;
gOffSetBottom:=tmpAlto-gOffSetTop-Printer.PageHeight;
end;



 
library setprint;
uses
Windows,
printers,
WinSpool,
SysUtils,
Registry,
Classes;
procedure SetPaperSize(nWidth,nHeight,nOrient:word);
var
hdriver:THandle;
a1,lpdevmode:PDeviceMode;
szdevicename,szport:string;
hmem:hglobal;
begin
printer.PrinterIndex :=-1;
szdevicename:=printer.Printers.Strings[printer.printerindex];
szport:='LPT1:';
openprinter(pchar(szdevicename),hdriver,nil);
a1:=nil;
hmem:=globalalloc(gptr,documentpropertiesa(printer.Handle,hdriver,pchar(szdevicename),a1^,a1^,0));
lpdevmode:=globallock(hmem);
a1:=nil;
do
cumentpropertiesa(printer.Handle ,hdriver,pchar(szdevicename),lpdevmode^,a1^,dm_out_buffer);
lpdevmode^.dmFields :=lpdevmode^.dmFields or dm_papersize or dm_paperwidth or dm_paperlength;
lpdevmode^.dmPaperSize :=dmpaper_user;
lpdevmode^.dmPaperWidth :=nwidth;
lpdevmode^.dmPaperLength :=nheight;
case norient of
0: begin
lpdevmode.dmFields:=lpdevmode.dmFields or dm_orientation;
lpdevmode.dmOrientation :=dmorient_portrait;
end;
1: begin
lpdevmode.dmFields :=lpdevmode.dmFields or dm_orientation;
lpdevmode.dmOrientation :=dmorient_landscape;
end;
end;
a1:=nil;
do
cumentproperties(printer.Handle ,hdriver,pchar(szdevicename),a1^,lpdevmode^,dm_in_buffer or dm_update);
globalunlock(hmem);
globalfree(hmem);
closeprinter(hdriver);
end;
exports
SetPaperSize;
{$R *.res}
begin
end.
 
type
PHandle = ^THandle;
procedure InitPrinter(PaperSize, PaperWidth, PaperHeight, Orientation: Integer);
var
p1, p2, p3: string;
ph: PHandle;
hd: THandle;
devmode: PDeviceMode;
md: TDeviceMode;
sz: TSize;
begin
sz.cx := PaperWidth;
sz.cy := PaperHeight;
if PaperSize=0 then
PaperSize := DMPAPER_USER;
Printer.PrinterIndex := -1;
p1 := stringofchar(' ', 32);
p2 := stringofchar(' ', 32);
p3 := stringofchar(' ', 32);
Printer.GetPrinter(@(p1[1]), @(p2[1]), @(p3[1]), hd);
p1 := strpas(@(p1[1]));
p2 := strpas(@(p2[1]));
p3 := strpas(@(p3[1]));
ph := Pointer(Integer(Printer)+ TObject.InstanceSize + // 访问Printer.FPrinterHandle这个私有变量
SizeOf(TCanvas)+
SizeOf(TStrings)+
SizeOf(Integer)+
SizeOf(TStrings)+
SizeOf(Integer)+
SizeOf(string)+
SizeOf(Boolean)+
SizeOf(Boolean)+
SizeOf(TPrinterCapabilities)+
SizeOf(TPrinterState)+
SizeOf(HDC)+
SizeOf(PDeviceMode)+
SizeOf(THandle));
if ph^ = 0 then
raise Exception.Create('找不到打印机');
if hd = 0 then
hd := GlobalAlloc(GHND,do
cumentProperties(0, ph^, @(p1[1]), md, md, 0));
devmode := GlobalLock(hd);
if (Win32Platform <> VER_PLATFORM_WIN32_NT) or (sz=DMPAPER_USER) then
devMode^.dmFields := DM_PAPERSIZE or DM_PAPERLENGTH or DM_PAPERWIDTH or DM_ORIENTATION {or DM_COPIES}
else
devMode^.dmFields := DM_PAPERSIZE or DM_ORIENTATION;
devmode^.dmOrientation := Orientation;
devmode^.dmPaperSize := sz;
if (Win32Platform <> VER_PLATFORM_WIN32_NT) or (sz=DMPAPER_USER) then
begin
devmode^.dmPaperLength := PaperHeight;
devmode^.dmPaperWidth := PaperWidth;
end;
try
ifdo
cumentProperties(0, ph^, @(p1[1]), devmode^, devmode^, DM_IN_BUFFER or DM_OUT_BUFFER) <> IDOK then
raise Exception.Create('打印机设置错误');
finally
globalUnlock(hd);
end;
printer.SetPrinter(@(p1[1]), @(p2[1]), @(p3[1]), hd);
end;
 
win2000下怎样实现自定义纸张啊
没有高手了吗???
 
我的回答你根本没看嘛
 
to pearl 我的环境为d6+win2000,你的源代码编译通不过,你试试吧,好像是sz=DMPAPER_USER
类型不兼容,请帮我看看,上次没有看,是因为你的思路与大众思路好像不太一样,
加之小弟初搞又很急也就没仔细看了,抱谦。我的qq为18963264
 
哦?
我用d5, 编译运行都正常。
d6中类型检查严格了吗? 不可能吧?
DMPAPER_USER是个常量呀
你直接写sz=$100吧
 
to pearl
还是一样,类型不符,你的sz是tsize
 
type
PHandle = ^THandle;
procedure InitPrinter(PaperSize, PaperWidth, PaperHeight, Orientation: Integer);
var
p1, p2, p3: string;
ph: PHandle;
hd: THandle;
devmode: PDeviceMode;
md: TDeviceMode;
sz: Integer;
begin
if PaperSize=0 then
PaperSize := DMPAPER_USER;
Printer.PrinterIndex := -1;
p1 := stringofchar(' ', 32);
p2 := stringofchar(' ', 32);
p3 := stringofchar(' ', 32);
Printer.GetPrinter(@(p1[1]), @(p2[1]), @(p3[1]), hd);
p1 := strpas(@(p1[1]));
p2 := strpas(@(p2[1]));
p3 := strpas(@(p3[1]));
ph := Pointer(Integer(Printer)+ TObject.InstanceSize + // 访问Printer.FPrinterHandle这个私有变量
SizeOf(TCanvas)+
SizeOf(TStrings)+
SizeOf(Integer)+
SizeOf(TStrings)+
SizeOf(Integer)+
SizeOf(string)+
SizeOf(Boolean)+
SizeOf(Boolean)+
SizeOf(TPrinterCapabilities)+
SizeOf(TPrinterState)+
SizeOf(HDC)+
SizeOf(PDeviceMode)+
SizeOf(THandle));
if ph^ = 0 then
raise Exception.Create('找不到打印机');
if hd = 0 then
hd := GlobalAlloc(GHND,do
cumentProperties(0, ph^, @(p1[1]), md, md, 0));
devmode := GlobalLock(hd);
if (Win32Platform <> VER_PLATFORM_WIN32_NT) or (sz=DMPAPER_USER) then
devMode^.dmFields := DM_PAPERSIZE or DM_PAPERLENGTH or DM_PAPERWIDTH or DM_ORIENTATION {or DM_COPIES}
else
devMode^.dmFields := DM_PAPERSIZE or DM_ORIENTATION;
devmode^.dmOrientation := Orientation;
devmode^.dmPaperSize := sz;
if (Win32Platform <> VER_PLATFORM_WIN32_NT) or (sz=DMPAPER_USER) then
begin
devmode^.dmPaperLength := PaperHeight;
devmode^.dmPaperWidth := PaperWidth;
end;
try
ifdo
cumentProperties(0, ph^, @(p1[1]), devmode^, devmode^, DM_IN_BUFFER or DM_OUT_BUFFER) <> IDOK then
raise Exception.Create('打印机设置错误');
finally
globalUnlock(hd);
end;
printer.SetPrinter(@(p1[1]), @(p2[1]), @(p3[1]), hd);
end;
 
to pearl
你的papersize从来没有用过还有sz可能没初始化
最关健的是调用还出错
 
不好意思, 我原来的代码里还要做点别的事, 所以定义了一个sz变量。 贴上来前修改代码时忘了改了。
你把sz定义去掉好了。 代码中用到sz的地方就用papersize代替吧。
调用出错我就不知道原因了。 我的代码在d5下用没问题(事实上这段代码是我们项目中用于报表打印的, 绝对可以执行)
如果你用d6, 那么检查一下TPrinter源代码, 看看FPrinterHandle是不是地址被改掉了?
 
to pearl
谢谢仁兄的大力支持,明天就给分
请到csdn与我联系
这是我的qq:18962364
email: huhao618@163.com
再次感谢
 
多人接受答案了。
 
后退
顶部