MFC:获取所有打印机的名称(打印机模块-2)

[复制链接]
发表于 2025-7-27 04:03:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
配景:

“遍历当前用户的每一台虚拟打印机,将其默认纸张设置为 A4 并设置为纵向。”
实现原理:

1.从当前用户的注册表读取所有已配置的打印机;
2.遍历每台打印机;
3.输出其逻辑与实际纸张大小;
4.实验设置为 A4 纸,纵向;
5.输出设置是否成功。
  1. #include <Windows.h>
  2. #include <stdio.h>
  3. void SetPrinterPaperSizeAndOrientation(HANDLE hPrinter, int nPaperIndex, int nOrientation)
  4. {
  5.     DEVMODE devMode;
  6.     memset(&devMode, 0, sizeof(DEVMODE));
  7.     devMode.dmSize = sizeof(DEVMODE);
  8.    
  9.     // 获取当前打印机的设备模式
  10.     if (DocumentProperties(NULL, hPrinter, NULL, &devMode, NULL, DM_OUT_BUFFER) != IDOK)
  11.     {
  12.         // 获取设备模式失败
  13.         return;
  14.     }
  15.    
  16.     // 修改纸张大小和方向
  17.     devMode.dmPaperSize = nPaperIndex; // 设置纸张大小
  18.     devMode.dmOrientation = nOrientation; // 设置纸张方向
  19.     // 更新打印机的设备模式
  20.     if (DocumentProperties(NULL, hPrinter, NULL, &devMode, &devMode, DM_IN_BUFFER | DM_OUT_BUFFER) != IDOK)
  21.     {
  22.         // 更新设备模式失败
  23.         return;
  24.     }
  25.     // 获取逻辑高度和实际高度
  26.     int nLogicHeight = devMode.dmPelsHeight; // 逻辑高度
  27.     int nActualHeight = devMode.dmYResolution; // 实际高度
  28. }
  29. // 获取打印机纸张信息
  30. void GetPrinterPaperInfo(const TCHAR* pszPrinterName, int& nLogicalWidth, int& nLogicalHeight, int& nPhysicalWidth, int& nPhysicalHeight)
  31. {
  32.     HKEY hKey;
  33.     LONG lResult;
  34.     // 构造打印机注册表项路径
  35.     TCHAR szKeyPath[MAX_PATH];
  36.     _stprintf_s(szKeyPath, _T("Software\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Printers\\%s\\PrinterDriverData"), pszPrinterName);
  37.     // 打开打印机注册表项
  38.     lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyPath, 0, KEY_READ, &hKey);
  39.     if (lResult == ERROR_SUCCESS)
  40.     {
  41.         TCHAR szData[MAX_PATH];
  42.         DWORD dwDataSize = sizeof(szData);
  43.         // 获取逻辑纸张宽度
  44.         lResult = RegQueryValueEx(hKey, _T("PaperWidth"), NULL, NULL, (LPBYTE)szData, &dwDataSize);
  45. if (lResult == ERROR_SUCCESS)
  46. {
  47.     // 正确处理数据
  48.     printf("Value data: %s\n", szData);
  49. }
  50. else if (lResult == ERROR_MORE_DATA)
  51. {
  52.     printf("Buffer size too small\n");
  53. }
  54. else if (lResult == ERROR_INVALID_PARAMETER)
  55. {
  56.     printf("Invalid parameter\n");
  57. }
  58. else
  59. {
  60.     printf("Error querying default registry value: %d\n", lResult);
  61. }
  62.         if (lResult == ERROR_SUCCESS)
  63.         {
  64.             sscanf_s(szData, "%d", &nLogicalWidth);
  65.         }
  66.         // 获取逻辑纸张高度
  67.         lResult = RegQueryValueEx(hKey, _T("PaperHeight"), NULL, NULL, (LPBYTE)szData, &dwDataSize);
  68.         if (lResult == ERROR_SUCCESS)
  69.         {
  70.             sscanf_s(szData, "%d", &nLogicalHeight);
  71.         }
  72.         // 获取实际纸张宽度
  73.         lResult = RegQueryValueEx(hKey, _T("PaperWidthActual"), NULL, NULL, (LPBYTE)szData, &dwDataSize);
  74.         if (lResult == ERROR_SUCCESS)
  75.         {
  76.             sscanf_s(szData, "%d", &nPhysicalWidth);
  77.         }
  78.         // 获取实际纸张高度
  79.         lResult = RegQueryValueEx(hKey, _T("PaperHeightActual"), NULL, NULL, (LPBYTE)szData, &dwDataSize);
  80.         if (lResult == ERROR_SUCCESS)
  81.         {
  82.             sscanf_s(szData, "%d", &nPhysicalHeight);
  83.         }
  84.         RegCloseKey(hKey);
  85.     }
  86. }
  87. int main()
  88. {
  89.     HKEY hKey;
  90.     LONG lResult;
  91.     DWORD dwIndex = 0;
  92.     TCHAR szPrinterName[MAX_PATH];
  93.     DWORD dwSize = sizeof(szPrinterName);
  94.     // 打开打印机列表注册表项
  95.     lResult = RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices"), 0, KEY_READ, &hKey);
  96.     if (lResult == ERROR_SUCCESS)
  97.     {
  98.         // 遍历打印机列表
  99.         while (RegEnumKeyEx(hKey, dwIndex, szPrinterName, &dwSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
  100.         {
  101.             int nLogicalWidth = 0; // 逻辑纸张宽度
  102.             int nLogicalHeight = 0; // 逻辑纸张高度
  103.             int nPhysicalWidth = 0; // 实际纸张宽度
  104.             int nPhysicalHeight = 0; // 实际纸张高度
  105.             // 获取打印机纸张信息
  106.             GetPrinterPaperInfo(szPrinterName, nLogicalWidth, nLogicalHeight, nPhysicalWidth, nPhysicalHeight);
  107.             // 输出获取的纸张信息
  108.             printf("Printer Name: %s\n", szPrinterName);
  109.             printf("Logical Paper Size: %d x %d\n", nLogicalWidth, nLogicalHeight);
  110.             printf("Physical Paper Size: %d x %d\n", nPhysicalWidth, nPhysicalHeight);
  111.             // 重置打印机名称缓冲区大小
  112.             dwSize = sizeof(szPrinterName);
  113.             dwIndex++;
  114.         }
  115.         RegCloseKey(hKey);
  116.     }
  117.     return 0;
  118. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表