Udostępnij za pośrednictwem


CDC::StartDoc

Informs the device driver that a new print job is starting and that all subsequent StartPage and EndPage calls should be spooled under the same job until an EndDoc call occurs.

int StartDoc(
   LPDOCINFO lpDocInfo 
);
int StartDoc(
   LPCTSTR lpszDocName 
);

Parameters

  • lpDocInfo
    Points to a DOCINFO structure containing the name of the document file and the name of the output file.

  • lpszDocName
    Pointer to a string containing the name of the document file.

Return Value

If the function succeeds, the return value is greater than zero. This value is the print job identifier for the document.

If the function fails, the return value is less than or equal to zero.

Remarks

This ensures that documents longer than one page will not be interspersed with other jobs.

For Windows versions 3.1 and later, this function replaces the STARTDOC printer escape. Using this function ensures that documents containing more than one page are not interspersed with other print jobs.

StartDoc should not be used inside metafiles.

Example

This code fragment gets the default printer, opens a print job, and spools one page with "Hello, World!" on it. Because the text printed by this code isn't scaled to the printer's logical units, the output text may be in such small letters that the result is unreadable. The CDC scaling functions, such as SetMapMode, SetViewportOrg, and SetWindowExt, can be used to fix the scaling.

void CDCView::DoStartDoc()
{
   // 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.TextOut(50, 50, _T("Hello World!"), 12);

          dcPrinter.EndPage();
          dcPrinter.EndDoc();
          dcPrinter.SelectObject(pOldFont);
        }
      }
   }
}

Requirements

Header: afxwin.h

See Also

Reference

CDC Class

Hierarchy Chart

CDC::Escape

CDC::EndDoc

CDC::AbortDoc

Other Resources

CDC Members