首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

C++ Builder 取得印表機參數完整指南:解析度、紙張大小、實體尺寸

     編輯:關於C++

核心 API:GetDeviceCaps

透過 Printer()->Handle 取得印表機 DC,呼叫 GetDeviceCaps 查詢各項能力值。

1. 印表機解析度 (DPI)


int dpiX = GetDeviceCaps(Printer->Handle, LOGPIXELSX);  // 水平 DPI
int dpiY = GetDeviceCaps(Printer->Handle, LOGPIXELSY);  // 垂直 DPI

2. 可列印區域大小 (像素)

VCL TPrinter 屬性直接提供:


int printableWidth  = Printer->PageWidth;   // 可列印寬度 (像素)
int printableHeight = Printer->PageHeight;  // 可列印高度 (像素)

3. 實體紙張大小 (像素)


int physicalWidth  = GetDeviceCaps(Printer->Handle, PHYSICALWIDTH);
int physicalHeight = GetDeviceCaps(Printer->Handle, PHYSICALHEIGHT);

注意:實體尺寸 ≥ 可列印區域,差值為不可列印邊距 (硬體限制)。

4. 實體紙張大小 (英吋/公分)


double widthInch  = (double)physicalWidth  / dpiX;
double heightInch = (double)physicalHeight / dpiY;

double widthCm  = widthInch  * 2.54;
double heightCm = heightInch * 2.54;

5. 其他實用 GetDeviceCaps 索引

索引常數意義用途
HORZSIZE / VERTSIZE螢幕/紙張實體尺寸 (毫米)螢幕為實際可視區,印表機為紙張大小
HORZRES / VERTRES可繪製區域解析度 (像素)印表機同 PageWidth/PageHeight
BITSPIXEL色深 (位元/像素)1=單色, 4=16色, 8=256色, 24=真彩色
PLANES色彩平面數通常為 1
NUMBRUSHES / NUMPENS / NUMFONTS可用筆刷/畫筆/字型數裝置能力查詢

完整範例:列印參數報表


void __fastcall TForm1::BtnGetPrinterInfoClick(TObject *Sender) {
    if (!Printer->Printers->Count) {
        ShowMessage("系統無印表機");
        return;
    }

    Printer->PrinterIndex = 0;  // 選擇預設印表機
    Printer->BeginDoc();        // 必須 BeginDoc 後 Handle 才有效

    HDC hdc = Printer->Handle;
    int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
    int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
    int physW = GetDeviceCaps(hdc, PHYSICALWIDTH);
    int physH = GetDeviceCaps(hdc, PHYSICALHEIGHT);
    int printW = Printer->PageWidth;
    int printH = Printer->PageHeight;

    AnsiString info = "印表機參數報表:\n\n";
    info += "解析度: " + IntToStr(dpiX) + " x " + IntToStr(dpiY) + " DPI\n";
    info += "實體紙張: " + IntToStr(physW) + " x " + IntToStr(physH) + " 像素\n";
    info += "可列印區: " + IntToStr(printW) + " x " + IntToStr(printH) + " 像素\n";
    info += "實體尺寸: " + FormatFloat("0.00", (double)physW/dpiX) + " x " +
              FormatFloat("0.00", (double)physH/dpiY) + " 英吋\n";
    info += "邊距 (像素): 左/右=" + IntToStr((physW-printW)/2) +
              " 上/下=" + IntToStr((physH-printH)/2);

    Printer->Abort();  // 不實際列印,僅查詢
    ShowMessage(info);
}

常見陷阱

  • Handle 無效:必須在 BeginDoc 後、EndDoc/Abort 前使用
  • 多印表機:Printer->PrinterIndex 切換後需重新 BeginDoc
  • 單位換算:DPI 為每英吋點數,英吋轉公分乘 2.54
  • 不可列印區:實體尺寸減去可列印區即為硬體邊距,無法列印內容
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved