去看看前面的问题吧,这个问题已经被讨论过很多遍了。当然每个人都可能会碰到一些特殊
的情况,但你的问题是一个通用型的问题,有通用的解决方法。
solution1:用Delphi.Printers.TPrinter类,一般的问题都能解决,如果这个类不能解决,
或者结果不理想的话就直接调用api函数吧。
solution2:用Delphi.WinSpool里申明好的外部函数,直接调用api,有什么不明白的去MSDN上
查。
这里附上用API的解决方法(TPrinter的方法前面的问题里讲的很详细了):
用OpenPrinter函数,通过这个方法可以得到打印机的句柄.
BOOL OpenPrinter(
LPTSTR pPrinterName, // printer or server name
LPHANDLE phPrinter, // printer or server handle
LPPRINTER_DEFAULTS pDefault // printer defaults
);
Parameters
pPrinterName
in Pointer to a null-terminated string that specifies the name of the printer
or print server.
Windows NT/2000/XP: If NULL, it indicates the local printer server.
phPrinter
out Pointer to a variable that receives a handle to the open printer or print
server object.
Windows 2000/XP: The phPrinter parameter can return an Xcv handle for use with
the XcvData function. For more information about XcvData, see the Microsoft
Windows 2000 Driver Development Kit.
pDefault
in Pointer to a PRINTER_DEFAULTS structure. This value can be NULL.
Return Values
If the function succeeds, the return value is a nonzero value.
If the function fails, the return value is zero. To get extended error
information, call GetLastError.
用SetForm来给一个特定的打印机设定纸张.
SetForm
The SetForm function sets the form information for the specified printer.
BOOL SetForm(
HANDLE hPrinter, // handle to printer object
LPTSTR pFormName, // form name
DWORD Level, // information level
LPBYTE pForm // form buffer
);
Parameters
hPrinter
in Handle to the printer for which the form information is set. Use the
OpenPrinter or AddPrinter function to retrieve a printer handle.
pFormName
in Pointer to a null-terminated string that specifies the form name for
which the form information is set.
Level
in Specifies the version of the structure to which pForm points. This
value must be 1.
pForm
in Pointer to a FORM_INFO_1 structure.
Return Values
If the function succeeds, the return value is a nonzero value.
If the function fails, the return value is zero. To get extended error
information, call GetLastError.
SetForm的第四个参数中设定你的自定义纸张.
typedef struct _FORM_INFO_1 {
DWORD Flags;
LPTSTR pName;
SIZEL Size;
RECTL ImageableArea;
} FORM_INFO_1, *PFORM_INFO_1;
Members
Flags
Specifies the form properties. The following values are defined. Value Meaning
FORM_USER If this bit flag is set, the form has been defined by the user.
Forms with this flag set are defined in the registry.
FORM_BUILTIN If this bit-flag is set, the form is part of the spooler. Form
definitions with this flag setdo
not appear in the registry.
FORM_PRINTER If this bit flag is set, the form is associated with a certain
printer, and its definition appears in the registry.
pName
Pointer to a null-terminated string that specifies the name of the form.
Size
Specifies the width and height, in thousandths of millimeters, of the form.
ImageableArea
Specifies the width and height, in thousandths of millimeters, of the form.
Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Unsupported.
Header: Declared in Winspool.h;
include Windows.h.
Unicode: Declared as Unicode and ANSI structures.
以上的函数我在NT/2000/XP全部用过,98下没试过.