使用 MFC 打印到打印机 DC

Aco*_*orn 2 c++ printing mfc

我遵循了 Microsoft 关于创建设备上下文的教程,并且尝试在互联网上查找合适的来源(显然,MFC 是一个神秘的东西)。以下成功打印出“Hello, World!”;除了它非常小。

如何将 CImage 而不是文本发送到打印机?我怎样才能使文本的大小大于几毫米?我已经搜索过 MSDN,但所有内容要么已经过时(就像我正在使用的示例代码),要么只是没有详细记录。

 // get the default printer
  CPrintDialog dlg(FALSE);
  dlg.GetDefaults();

  // is a default printer set up?
  HDC hdcPrinter = dlg.GetPrinterDC();
  if (hdcPrinter == NULL)
  {
    //MessageBox(_T("Buy a printer!"));
  }
  else
  {
    // create a CDC and attach it to the default printer
    CDC dcPrinter;
    dcPrinter.Attach(hdcPrinter);

    // call StartDoc() to begin printing
    DOCINFO docinfo;
    memset(&docinfo, 0, sizeof(docinfo));
    docinfo.cbSize = sizeof(docinfo);
    docinfo.lpszDocName = _T("CDC::StartDoc() Code Fragment");

    // if it fails, complain and exit gracefully
    if (dcPrinter.StartDoc(&docinfo) < 0)
    {
      //MessageBox(_T("Printer wouldn't initalize"));
    }
    else
    {
      // start a page
      if (dcPrinter.StartPage() < 0)
      {
        //MessageBox(_T("Could not start page"));
        dcPrinter.AbortDoc();
      }
      else
      {
        // actually do some printing
        //CGdiObject* pOldFont = dcPrinter.SelectStockObject(SYSTEM_FONT);

        dcPrinter.SetMapMode(MM_HIENGLISH);
      auto font = CreateFont(
      3'000,                        // nHeight
      1'500,                         // nWidth
      0,                         // nEscapement
      0,                         // nOrientation
      FW_NORMAL,                 // nWeight
      FALSE,                     // bItalic
      FALSE,                     // bUnderline
      0,                         // cStrikeOut
      ANSI_CHARSET,              // nCharSet
      OUT_DEFAULT_PRECIS,        // nOutPrecision
      CLIP_DEFAULT_PRECIS,       // nClipPrecision
      DEFAULT_QUALITY,           // nQuality
      DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
      _T("Arial"));                 // lpszFacename
      dcPrinter.SelectObject(&font);
        dcPrinter.TextOut(450, 450, _T("Hello World!"), 12);
        dcPrinter.EndPage();
        dcPrinter.EndDoc();
        //dcPrinter.SelectObject(pOldFont);
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

Adr*_*thy 5

小文本问题

问题在于,默认情况下,字体的大小是以与设备相关的单位指定的,并且打印机的分辨率通常比屏幕高得多。因此,如果您在屏幕上创建了 20 像素高的字体(每英寸可能有 96 像素),当您尝试在打印机上使用该字体(每英寸可能有 300 或 600 点)时,您的文本看起来确实小的。

正如另一个答案所示,一个想法是更改映射模式,以便打印机使用更接近屏幕上的单位。

另一种方法是根据打印机的 DPI 创建具有适当大小(LOGFONT 结构中的 lfHeight 字段)的新字体,您可以使用该GetDeviceCaps函数确定该字体。如果您想要特定的字体大小(例如 14 点文本),这会很方便。

LOGFONT lf = {0};
lf.lfHeight = -MulDiv(point_size, ::GetDeviceCaps(dcPrinter, LOGPIXELSY), 72);
// init other field of lf as you like
HFONT hfontPrinter = ::CreateFontIndirect(&lf);
HFONT hfontOld = ::SelectObject(hdcPrinter, hfontPrinter);

// now draw to the hdcPrinter

::SelectObject(hdcPrinter, hfontOld);
::DeleteObject(hfontPrinter);
Run Code Online (Sandbox Code Playgroud)

发送 CImage

我不使用MFC,但看起来你可以直接用CImage::StretchBlt打印机DC调用。再次强调,当您选择目标坐标时,您可能必须考虑打印机更高的分辨率。