How to Print a Single Line Without Formfeed

Last reviewed: July 23, 1997
Article ID: Q124649
3.10 WINDOWS kbprint kbcode

The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) for Windows version 3.10

SUMMARY

This article shows by example how to have an application print one line at a time without a formfeed. Windows does not inherently provide this ability because it is in direct conflict with the design philosophy of a multitasking operating system.

One method of single-line printing, including sample code, is provided here. This method uses the combination of the RAW.DRV sample printer driver and the PASSTHROUGH escape, and supports printing over a network. It has the following limitations:

  • This method is not conducive to shared printing. If another application prints, it will assume that the printer is currently at the top of a page, which may not be the case if this code is being used.
  • This method is only compatible with dot matrix, daisywheel, or other printers capable of printing a single line or single line feed. It will not work with LaserJet printers that must print a page at a time.

MORE INFORMATION

If you are sure no other application will use the same printer, you can have your application print one line at a time to a dot matrix printer without issuing a formfeed. Use the RAW.DRV sample printer driver and the PASSTHROUGH escape to accomplish this. Follow these steps:

  1. Download RAW.DRV and place it in the \WINDOWS directory. For more information about RAW.DRV, including instructions for download, please see the following article in the Microsoft Knowledge Base:

    ARTICLE-ID: Q35708

       TITLE     : SAMPLE: Using a Device Driver to Send Binary Data to Printer
    
       Do not install this driver as the default printer driver. It is a sample
       driver and is not intended to be used for commercial applications.
    
    

  2. Call the PrintLineNow function by using the following code fragment:

    // The Abort Procedure for printing (a generic AbortProc)
    // This procedure needs to listed be in the Exports section
    //    of the .DEF file
    
BOOL CALLBACK AbortProc( HDC hDC, int Error ) {
  MSG msg;

  while(PeekMessage(&msg, NULL, 0, 0, TRUE))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return TRUE;
}

// The function that prints one line without formfeed
BOOL PrintLineNow( char *pData, WORD cbBytes ) {
  #define   LINEBUFFER_LENGTH 80 //or dynamically alloc enough space
  HDC       hDC;
  char      pOutput[LINEBUFFER_LENGTH+sizeof(WORD)];
  DOCINFO   di;
  ABORTPROC lpfnAbortProc;

  // Buffer size is LINEBUFFER_LENGTH bytes
  if( cbBytes > LINEBUFFER_LENGTH )
     return FALSE;

  // Get the DC using the RAW.DRV driver
  if( (hDC = CreateDC( "RAW", NULL, "LPT1", NULL ) ) == NULL )
    return FALSE;

  // Set up an Abort Procedure
  if((lpfnAbortProc = MakeProcInstance(AbortProc, hInst)) == NULL)
    {
      DeleteDC( hDC );
      return FALSE;
    }
  if( SetAbortProc( hDC, lpfnAbortProc ) <= 0 )
    {
      DeleteDC( hDC );
      FreeProcInstance( lpfnAbortProc );
      return FALSE;
    }

  // Start a Document
  di.cbSize = sizeof( DOCINFO );
  di.lpszDocName = "MyDoc";
  di.lpszOutput = NULL;
  if( StartDoc( hDC, &di ) <= 0 )
    {
      DeleteDC( hDC );
      FreeProcInstance( lpfnAbortProc );
      return FALSE;
    }

  // The start of a page
  if( StartPage( hDC ) <= 0 )
    {
      DeleteDC( hDC );
      FreeProcInstance( lpfnAbortProc );
      return FALSE;
    }

  // Put data in the buffer and send to the printer
  *(WORD *)pOutput = cbBytes;
  memcpy( &(pOutput[sizeof(WORD)]), pData, cbBytes );
  if( Escape( hDC, PASSTHROUGH, 0, pOutput, NULL ) <= 0 )
    {
      DeleteDC( hDC );
      FreeProcInstance( lpfnAbortProc );
      return FALSE;
    }

  // The end of the page
  if( EndPage( hDC ) < 0 )
    {
      DeleteDC( hDC );
      FreeProcInstance( lpfnAbortProc );
      return FALSE;
    }

  // End the Document
  if( EndDoc( hDC ) < 0 )
    {
      DeleteDC( hDC );
      FreeProcInstance( lpfnAbortProc );
      return FALSE;
    }

  // Clean up
  DeleteDC( hDC );
  FreeProcInstance( lpfnAbortProc );
  return TRUE;   // Success
}


Additional reference words: 3.10 NEWFRAME FF Mailing Label Form Feed
KBCategory: kbprint kbcode
KBSubcategory: GdiPrn
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 23, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.