问题描述
由于该程序必须能够在Windows XP上打印,所以我无法使用XPS API。
I can't use the XPS API since the program has to be able to print on Windows XP.
我正在尝试从Letter设置纸张尺寸
I'm trying to set the paper size from Letter to A4 using WinSpool.
这是我的测试代码:
var
H : THandle;
I : TBytes;
Info : PPrinterInfo2;
NeededSize : DWORD;
DevMode : PDeviceMode;
PD : TPrinterDefaults;
begin
PD.pDatatype := nil;
PD.pDevMode := nil;
PD.DesiredAccess := PRINTER_ACCESS_ADMINISTER;
if not OpenPrinter('Brother HL-5350DN series Printer', H, @PD) then begin
raise Exception.Create('OpenPrinter error: ' + SysErrorMessage(GetLastError));
end;
try
Assert(not GetPrinter(H, 2, nil, 0, @NeededSize));
SetLength(I, NeededSize);
Info := @I[0];
if not GetPrinter(H, 2, Info, NeededSize, @NeededSize) then begin
raise Exception.Create('GetPrinter error: ' + SysErrorMessage(GetLastError));
end;
DevMode := Info.pDevMode;
DevMode.dmFields := DevMode.dmFields or DM_PAPERSIZE;
DevMode.dmPaperSize := DMPAPER_A4;
Info.pSecurityDescriptor := nil; // According to MSDN it has to be niled if we're not going to change it.
if not SetPrinter(H, 2, Info, 0) then begin
raise Exception.Create('SetPrinter error: ' + SysErrorMessage(GetLastError));
end;
finally
ClosePrinter(H);
end;
TPrintDialog.Create(Self).Execute; // This is just so I can check the paper size
end;
我有两个与访问权限有关的问题。
I have two problems related to access rights.
如果我将 PD.DesiredAccess
设置为 PRINTER_ACCESS_ADMINISTER
GetPrinter
调用失败,我想这是由于UAC造成的。
If I set PD.DesiredAccess
to PRINTER_ACCESS_ADMINISTER
the GetPrinter
call fails, I guess this is due to UAC.
如果将其设置为 PRINTER_ACCESS_USE
GetPrinter
调用成功,并且Info结构良好,但是对 SetPrinter
的调用失败。
If I set it to PRINTER_ACCESS_USE
the GetPrinter
call succeeds and the Info structure is fine, but the call to SetPrinter
fails.
当我忽略 SetPrinter
的结果时,即使 SetPrinter 失败。
Interestingly enough when I ignore the Result of
SetPrinter
the print dialog reports A4 as the printer size even though SetPrinter
fails.
我做错了吗,足以将正确设置的PDeviceMode传递给OpenPrinter吗? (在写完这个问题之后,我实际上想到了这个问题:-)
Am I doing it completly wrong and it is enough to pass a correctly setup up PDeviceMode to OpenPrinter? (I actually came up with this after writing this question :-)
关于VCL的另一个问题:
Another question regarding the VCL:
如果我使用
Printers
单元,如何知道将缓冲区作为参数传递给 TPrinter.GetPrinter 方法?
If I use the
Printers
unit how do I know how big the buffers have to be that get passed as parameters to the TPrinter.GetPrinter
method?
背景:
系统为:Windows 7专业的64位英语,具有英语区域设置。
The system is: Windows 7 Professional 64-Bit English with English locale.
我正在尝试在网络打印机(Brother HL-5350DN打印机)上打印到A4纸。
I'm trying to print to A4 paper on a network printer (Brother HL-5350DN).
我已经将控制面板中的所有打印机设置都设置为A4纸,但是我正在编写的Delphi 2009程序仍然可以获取US Letter的纸尺寸。
I have set all printer settings in the control panel to A4 paper, but the Delphi 2009 program I'm writing still gets the paper dimensions for US Letter.
换句话说:Delphi程序不遵守打印机后台处理程序的默认设置。
In other words: The Delphi program doesn't respect the default settings of the printer spooler.
如果我先运行TPrinterDialog并选择正确的从那里手动打印纸张大小(在高级打印机设置中),一切都很好。
If I run a TPrinterDialog first and select the correct paper size from there manually (in the advanced printer settings) everything is fine.
m必须在没有任何UI的情况下运行,因此我必须以编程方式解决此问题,或者最好是该程序应该仅遵循默认的Windows打印机后台处理程序设置。
The program has to run without any UI, so I have to solve this programmatically or preferably the program should just respect the default Windows printer spooler settings.
也许我错过了一些重要的问题
Maybe I have missed some imporant setting?
推荐答案
推荐答案
就像David写道的那样,我的特定问题是通过在Windows中设置正确的打印机首选项来解决的。
Like David wrote, my specific problem is solved by setting the correct printer preferences in Windows.
我仍然没有找到设置应用程序本地打印属性的方法,但这不再是必需的。
I still haven't found a way to set the local printing properties for my application, but that is no longer necessary.
就像Sertac一样,您可以使用
TPrinter.GetPrinter
和 TPrinter.SetPrinter
读写全局打印机首选项。 (请参阅对该问题的评论)
Like Sertac wrote you can read and write the global printer preferences using
TPrinter.GetPrinter
and TPrinter.SetPrinter
. (See the comments to the question)
由于没有人提供答案,并且现在问题已解决,因此我将其标记为社区Wiki。随时改善此答案。
Since nobody provided an anwser and the problem is now solved, I'm marking this as community wiki. Feel free to improve this answer.
这篇关于如何使用WinSpool API设置纸张尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!