使用 DocumentProperties 修改打印机设置 您所在的位置:网站首页 实达bp3000打印机设置参数 使用 DocumentProperties 修改打印机设置

使用 DocumentProperties 修改打印机设置

2023-11-18 21:04| 来源: 网络整理| 查看: 265

使用 DocumentProperties () 函数修改打印机设置 项目 08/11/2023

本文介绍如何使用 DocumentProperties() 函数修改打印机设置。

原始产品版本: 打印机 原始 KB 编号: 167345

摘要

使用 DEVMODE 结构修改打印机设置比仅更改结构的字段更困难。 具体而言,设备的有效 DEVMODE 结构包含只能由 函数修改的 DocumentProperties() 私有数据。

本文介绍如何使用 DocumentProperties() 函数修改 DEVMODE 结构的内容。

详细信息

Win32 SDK 记录的 DEVMODE 结构包含公共或“设备无关数据”以及私有或“设备依赖数据”。 DEVMODE 的专用部分紧跟在内存连续缓冲区中由 DEVMODE 结构定义的公共部件之后。

程序无法预测此缓冲区的大小,因为它因打印机和打印机驱动程序的版本而异。 此外,由程序声明的 DEVMODE 结构没有足够的空间用于专用设备数据。 如果将缺少专用数据的 DEVMODE 缓冲区传递给 、 ResetDC()和 DocumentProperties()等CreateDC()函数,则函数可能会失败。

若要可靠地将 DEVMODE 与设备驱动程序配合使用,请按照以下步骤创建和修改它:

确定设备所需的缓冲区大小,然后为其分配足够的内存。

DocumentProperties() 当最后一个参数设置为 0 时,返回 DEVMODE 缓冲区所需的字节数。 本文中的示例代码使用此技术来确定缓冲区的正确大小。 然后,示例代码使用 的 malloc() C 运行时内存分配函数来分配足够大的缓冲区。 由于 DocumentProperties() 和 函数类似于 ResetDC() 并将 CreateDC() 指向 DEVMODE 的指针作为参数,因此大多数应用程序都可以分配由指针寻址的内存。

但是,需要作为全局内存句柄的函数(如常用 PrintDlg() 采用参数)。 如果程序使用最终 DEVMODE 缓冲区作为这些函数之一的参数,则它应使用 GlobalAlloc() 分配内存,并使用 GlobalLock()获取指向缓冲区的指针。

要求设备驱动程序使用默认设置初始化 DEVMODE 缓冲区。

示例代码再次调用 DocumentProperties() ,以使用当前默认设置初始化分配的缓冲区。 DocumentProperties()在 fMode 参数中传递命令时DM_OUT_BUFFER,使用打印机的当前设置填充称为 pDevModeOutput 参数的缓冲区。

对 DEVMODE 的公共部分进行更改,并要求设备驱动程序通过调用 DocumentProperties()将更改合并到 DEVMODE 的专用部分。

使用步骤 2 中的当前设置初始化缓冲区后,示例代码对 DEVMODE 的公共部分进行更改。 有关 DEVMODE 成员的说明,请参阅 Win32 SDK 文档。 此示例代码确定打印机是否可以使用方向和双面 (双面) 设置并相应地更改它们。

注意

DEVMODE 的 dmFields 成员中的标志仅指示打印机使用关联的结构成员。 打印机具有各种不同的物理特征,因此可能仅支持 DEVMODE 记录的功能的子集。 若要确定 DEVMODE 字段的支持设置,应用程序应使用 DeviceCapabilities()。

然后,示例代码对 DocumentProperties() 进行第三次调用,并在 pDevModeInput 和 pDevModeOutput 参数中传递 DEVMODE 缓冲区。 它还使用 OR (“|”在 fMode 参数中传递DM_IN_BUFFER和DM_OUT_BUFFER的组合命令) 运算符。 这些命令指示函数采用输入缓冲区中包含的任何设置,并将其与设备的当前设置合并。 然后,它将结果写入 out 参数中指定的缓冲区。

注意

DocumentProperties() 通过打印机的句柄引用特定打印机:hPrinter。 此句柄是从 OpenPrinter()获取的,示例代码对此进行了演示。 OpenPrinter() 需要打印机的名称,该名称通常是打印机在操作系统 shell 中显示的友好名称。 可以从 、从 返回PrintDlg()的 DEVNAMES 结构或默认打印机获取EnumPrinters()此名称。

在本文中,使用 执行 DocumentProperties()分配正确的缓冲区大小和初始化该缓冲区的前两个步骤。 还可以使用 GetPrinter()执行这些步骤。

有关详细信息和示例,请参阅 使用 SetPrinter 函数修改打印机设置。

示例代码

示例代码遵循以下三个步骤来获取和更改 DEVMODE 缓冲区。 函数采用命名打印机,并配置 DEVMODE 以双面打印,如果支持这些功能,则采用横向打印。 返回给调用方的结果 DEVMODE 适用于使用 DEVMODE 缓冲区的其他 API 调用,例如 CreateDC()、 SetPrinter()、 PrintDlg()或 ResetDC()。 当调用方使用 DEVMODE 缓冲区完成时,调用方负责释放内存。

LPDEVMODE GetLandscapeDevMode(HWND hWnd, char *pDevice) { HANDLE hPrinter; LPDEVMODE pDevMode; DWORD dwNeeded, dwRet; /* Start by opening the printer */ if (!OpenPrinter(pDevice, &hPrinter, NULL)) return NULL; /* * Step 1: * Allocate a buffer of the correct size. */ dwNeeded = DocumentProperties(hWnd, hPrinter, /* Handle to our printer. */ pDevice, /* Name of the printer. */ NULL, /* Asking for size, so */ NULL, /* these are not used. */ 0); /* Zero returns buffer size. */ pDevMode = (LPDEVMODE)malloc(dwNeeded); /* * Step 2: * Get the default DevMode for the printer and * modify it for your needs. */ dwRet = DocumentProperties(hWnd, hPrinter, pDevice, pDevMode, /* The address of the buffer to fill. */ NULL, /* Not using the input buffer. */ DM_OUT_BUFFER); /* Have the output buffer filled. */ if (dwRet != IDOK) { /* If failure, cleanup and return failure. */ free(pDevMode); ClosePrinter(hPrinter); return NULL; } /* * Make changes to the DevMode which are supported. */ if (pDevMode->dmFields & DM_ORIENTATION) { /* If the printer supports paper orientation, set it.*/ pDevMode->dmOrientation = DMORIENT_LANDSCAPE; } if (pDevMode->dmFields & DM_DUPLEX) { /* If it supports duplex printing, use it. */ pDevMode->dmDuplex = DMDUP_HORIZONTAL; } /* * Step 3: * Merge the new settings with the old. * This gives the driver an opportunity to update any private * portions of the DevMode structure. */ dwRet = DocumentProperties(hWnd, hPrinter, pDevice, pDevMode, /* Reuse our buffer for output. */ pDevMode, /* Pass the driver our changes. */ DM_IN_BUFFER | /* Commands to Merge our changes and */ DM_OUT_BUFFER); /* write the result. */ /* Finished with the printer */ ClosePrinter(hPrinter); if (dwRet != IDOK) { /* If failure, cleanup and return failure. */ free(pDevMode); return NULL; } /* Return the modified DevMode structure. */ return pDevMode; }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有