该
SetPrinter
函数允许应用程序更改各种打印机属性。 但是,正如本文中的代码所示,正确调用
SetPrinter
需要一定量的准备。
原始产品版本:
窗户
原始 KB 编号:
140285
SetPrinter 的 hPrinter 参数
第一个参数是要更改其设置的打印机的句柄。 应从
OpenPrinter()
中检索此参数。
SetPrinter 的 dwLevel 参数
第二个参数指定要传递给
SetPrinter()
的数据的结构。 参数值可以是
0
、
2
、
3
、
4
、
5
、
6
、
7
、
8
或
9
。
SetPrinter 的 lpbPrinter 参数
第三个参数是与第二个
PRINTER_INFO_n
参数中的数字相对应的结构
n
。 此结构可能会造成混淆,因为它不仅仅是结构大小的缓冲区。 这些结构包含与设备无关的信息,但在内存中紧接着是设备驱动程序提供的一些与设备相关的信息量可变。 因此,需要完成一些工作来确定此缓冲区应有多重要。 这是通过调用实现的,调用
GetPrinter()
将设置为
pcbNeeded
所需的总大小。
此外,缓冲区通常包含大量与设备无关且依赖于设备的信息。 应用程序不会知道或关心这些结构成员中的大多数值。 因此,当你进行感兴趣的更改时,必须为所有这些其他数据片段插入正确的值。 第二次调用
GetPrinter()
时,将设置其他数据片段。
SetPrinter 的 DwCommand 参数
第四个参数用于暂停打印、恢复打印或清除所有打印作业。 此参数通常不会在使用时同时
lpbPrinter
使用。 本文不考虑设置打印机状态,因此示例代码将此参数设置为零。
关于 DEVMODE
通常,
DEVMODE
将 (而不是) 元素修改指向的结构
pDevMode
元素
PRINTER_INFO_n
。 在这种情况下,
pDevMode->dmFields
标志会告诉应用程序哪些字段可以更改。 因为这是由你
GetPrinter()
提供的,所以你可以在
dmFields
尝试更改之前检查标志。
此外,由于修改设备独立部件中的字段也可能影响依赖设备的部分
DEVMODE
的更改,因此需要在调用
SetPrinter()
之前调用
DocumentProperties()
,以便为其生成一致
DEVMODE
的
SetPrinter()
结构。
// MySetPrinter
// Demonstrates how to use the SetPrinter API. This particular function changes the orientation
// for the printer specified in pPrinterName to the orientation specified in dmOrientation.
// Valid values for dmOrientation are:
// DMORIENT_PORTRAIT (1) or DMORIENT_LANDSCAPE (2)
BOOL MySetPrinter(LPTSTR pPrinterName, short dmOrientation)
HANDLE hPrinter = NULL;
DWORD dwNeeded = 0;
PRINTER_INFO_2 *pi2 = NULL;
DEVMODE *pDevMode = NULL;
PRINTER_DEFAULTS pd;
BOOL bFlag;
LONG lFlag;
// Open printer handle (on Windows NT, you need full-access because you
// will eventually use SetPrinter)...
ZeroMemory(&pd, sizeof(pd));
pd.DesiredAccess = PRINTER_ALL_ACCESS;
bFlag = OpenPrinter(pPrinterName, &hPrinter, &pd);
if (!bFlag || (hPrinter == NULL))
return FALSE;
// The first GetPrinter tells you how big the buffer should be in
// order to hold all of PRINTER_INFO_2. Note that this should fail with
// ERROR_INSUFFICIENT_BUFFER. If GetPrinter fails for any other reason
// or dwNeeded isn't set for some reason, then there is a problem...
SetLastError(0);
bFlag = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
if ((!bFlag) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) || (dwNeeded == 0))
ClosePrinter(hPrinter);
return FALSE;
// Allocate enough space for PRINTER_INFO_2...
pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (pi2 == NULL)
ClosePrinter(hPrinter);
return FALSE;
// The second GetPrinter fills in all the current settings, so all you
// need to do is modify what you're interested in...
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded);
if (!bFlag)
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
// If GetPrinter didn't fill in the DEVMODE, try to get it by calling
// DocumentProperties...
if (pi2->pDevMode == NULL)
dwNeeded = DocumentProperties(NULL, hPrinter,
pPrinterName,
NULL, NULL, 0);
if (dwNeeded <= 0)
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
pDevMode = (DEVMODE *)GlobalAlloc(GPTR, dwNeeded);
if (pDevMode == NULL)
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
lFlag = DocumentProperties(NULL, hPrinter,
pPrinterName,
pDevMode, NULL,
DM_OUT_BUFFER);
if (lFlag != IDOK || pDevMode == NULL)
GlobalFree(pDevMode);
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
pi2->pDevMode = pDevMode;
// Driver is reporting that it doesn't support this change...
if (!(pi2->pDevMode->dmFields & DM_ORIENTATION))
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
// Specify exactly what we are attempting to change...
pi2->pDevMode->dmFields = DM_ORIENTATION;
pi2->pDevMode->dmOrientation = dmOrientation;
// Do not attempt to set security descriptor...
pi2->pSecurityDescriptor = NULL;
// Make sure the driver-dependent part of devmode is updated...
lFlag = DocumentProperties(NULL, hPrinter,
pPrinterName,
pi2->pDevMode, pi2->pDevMode,
DM_IN_BUFFER | DM_OUT_BUFFER);
if (lFlag != IDOK)
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
// Update printer information...
bFlag = SetPrinter(hPrinter, 2, (LPBYTE)pi2, 0);
if (!bFlag)
// The driver doesn't support, or it is unable to make the change...
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
// Tell other apps that there was a change...
SendMessageTimeout(HWND_BROADCAST, WM_DEVMODECHANGE, 0L,
(LPARAM)(LPCSTR)pPrinterName,
SMTO_NORMAL, 1000, NULL);
// Clean up...
if (pi2)
GlobalFree(pi2);
if (hPrinter)
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return TRUE;