You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi下Epson TM30默认80mm连续纸卷设置及相关技术问题

解决Epson TM-30打印对话框默认A4及纸张尺寸获取问题

一、获取当前打印机的纸张类型

通过Windows API的GetPrinter结合DEVMODE结构可以读取打印机当前的纸张配置,以下是C++实现示例:

#include <windows.h>
#include <iostream>

void GetCurrentPaperType(LPCWSTR printerName) {
    HANDLE hPrinter;
    if (!OpenPrinterW(printerName, &hPrinter, NULL)) {
        std::cout << "OpenPrinter 调用失败,错误码:" << GetLastError() << std::endl;
        return;
    }

    DWORD cbNeeded;
    GetPrinterW(hPrinter, 2, NULL, 0, &cbNeeded);
    PRINTER_INFO_2W* pPrinterInfo = (PRINTER_INFO_2W*)malloc(cbNeeded);
    if (!GetPrinterW(hPrinter, 2, (LPBYTE)pPrinterInfo, cbNeeded, &cbNeeded)) {
        std::cout << "GetPrinter 调用失败,错误码:" << GetLastError() << std::endl;
        ClosePrinter(hPrinter);
        free(pPrinterInfo);
        return;
    }

    DEVMODEW* pDevMode = pPrinterInfo->pDevMode;
    if (pDevMode) {
        if (pDevMode->dmPaperSize == DMPAPER_USER) {
            std::wcout << L"当前纸张:自定义尺寸,宽度:" << pDevMode->dmPaperWidth / 10.0 << L"cm,长度:" << pDevMode->dmPaperLength / 10.0 << L"cm" << std::endl;
        } else {
            std::wcout << L"当前纸张类型代码:" << pDevMode->dmPaperSize << std::endl;
        }
    }

    free(pPrinterInfo);
    ClosePrinter(hPrinter);
}

int main() {
    // 替换为你的打印机名称,例如 L"EPSON TM-T30 Receipt"
    GetCurrentPaperType(L"你的打印机名称");
    return 0;
}

代码说明:dmPaperWidth和dmPaperLength的单位是0.1毫米,需除以10转换为厘米;若dmPaperSize为DMPAPER_USER,说明是自定义纸张。

二、查找"Endless Paper Roll 80mm"对应的参数

使用DeviceCapabilities枚举打印机支持的所有纸张,找到目标纸张对应的代码,C++示例如下:

#include <windows.h>
#include <iostream>
#include <vector>

void EnumPrinterPapers(LPCWSTR printerName) {
    DWORD numPapers = DeviceCapabilitiesW(printerName, NULL, DC_PAPERS, NULL, NULL);
    if (numPapers == INVALID_DEVICE_CAPABILITIES) {
        std::cout << "DeviceCapabilities 调用失败,错误码:" << GetLastError() << std::endl;
        return;
    }

    std::vector<WORD> paperSizes(numPapers);
    std::vector<WCHAR> paperNames(numPapers * CCHPAPERNAMES);

    DeviceCapabilitiesW(printerName, NULL, DC_PAPERS, (LPBYTE)&paperSizes[0], NULL);
    DeviceCapabilitiesW(printerName, NULL, DC_PAPERNAMES, (LPBYTE)&paperNames[0], NULL);

    for (DWORD i = 0; i < numPapers; i++) {
        std::wcout << L"纸张代码:" << paperSizes[i] << L",名称:" << &paperNames[i * CCHPAPERNAMES] << std::endl;
    }
}

int main() {
    EnumPrinterPapers(L"你的打印机名称");
    return 0;
}

运行后会列出所有支持的纸张名称和对应代码,80mm卷纸可能是自定义类型(代码对应DMPAPER_USER)或驱动自定义的非标准代码(数值大于DMPAPER_USER)。

三、强制打印对话框默认使用80mm卷纸

若系统全局设置未生效,可在程序中手动指定纸张尺寸以覆盖默认A4,以下是C#示例:

using System.Drawing.Printing;
using System.Windows.Forms;

PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = "EPSON TM-T30 Receipt";

// 遍历打印机支持的纸张,匹配80mm卷纸
foreach (PaperSize paper in pd.PrinterSettings.PaperSizes) {
    if (paper.PaperName.Contains("80mm") || paper.PaperName.Contains("Roll Paper 80")) {
        pd.DefaultPageSettings.PaperSize = paper;
        break;
    }
}

// 打开打印对话框
PrintDialog printDialog = new PrintDialog();
printDialog.Document = pd;
if (printDialog.ShowDialog() == DialogResult.OK) {
    pd.Print();
}

注意:不同驱动的纸张名称可能有差异,需根据枚举结果调整匹配关键词,比如"Continuous 80mm"等。

内容的提问来源于stack exchange,提问作者Daniele

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 15:23:11