Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

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  
);
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.

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.

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.

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);
        }
      }
   }
}

Header: afxwin.h

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.