Solved

Help needed on line printing

Posted on 2002-06-03
7
395 Views
Last Modified: 2010-08-05
Anyone knows how to stop advancing to the next page in line printing? Meaning the printer will print a portion of the paper (if it does not use up the whole page) and wait for the next print.

Currently this is the coding I am using for the printing part:

// p points to the start of the buffer, pSize holds the number of characters
void PrintString(char *p, DWORD pSize)
{
    CDC             dc;
    CPrintDialog    printDlg(FALSE);
    CRect           r;
    int             nHeight;
   
    // ask the user to select a printer
    if (printDlg.DoModal() == IDCANCEL)
        return;
   
    // Attach a printer DC
    dc.Attach(printDlg.GetPrinterDC());
    dc.m_bPrinting = TRUE;

    // use Textmappingmode, that's easiest to map the fontsize
    dc.SetMapMode(MM_TEXT);
   
    // setup font specifics
    LOGFONT LogFont;
   
    CFont     aFont, *oldFont;
   
    LogFont.lfHeight = -MulDiv(10, GetDeviceCaps(dc, LOGPIXELSY), 72);
    LogFont.lfWidth = 0;
    LogFont.lfEscapement = 0;
    LogFont.lfOrientation = 0;
    LogFont.lfWeight = 0;
    LogFont.lfItalic = false;
    LogFont.lfUnderline = 0;
    LogFont.lfStrikeOut = 0;
    LogFont.lfCharSet = ANSI_CHARSET;
    LogFont.lfOutPrecision = OUT_TT_PRECIS;
    LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
    LogFont.lfQuality = DEFAULT_QUALITY;
    LogFont.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
    lstrcpy (LogFont.lfFaceName, "MS Sans Serif");
    dc.SetBkMode(OPAQUE);
    aFont.CreateFontIndirect ( &LogFont );
    // ok, we've build the font, now use it
    oldFont = dc.SelectObject( &aFont );        
       
    // Get the application title
    CString strTitle;
    strTitle.LoadString(AFX_IDS_APP_TITLE);
   
    // Initialise print document details
   
    DOCINFO di;
    ::ZeroMemory (di, sizeof (DOCINFO));
    di.cbSize = sizeof (DOCINFO);
    // application title appears in the spooler view
    di.lpszDocName = strTitle;
   
    // Begin a new print job
    BOOL bPrintingOK = dc.StartDoc( &di );
       
    // Get the printing extents and store in the m_rectDraw field of a
    // CPrintInfo object
    CPrintInfo Info;
    int w = dc.GetDeviceCaps(HORZRES);
    int h = dc.GetDeviceCaps(VERTRES);
    Info.m_rectDraw.SetRect(0,0, w, h);
   
    char *startAt = p;
    int totalDone = 0;
    int lengthToGo = pSize;
   
    for (UINT page = Info.GetMinPage();
    bPrintingOK & totalDone < lengthToGo; page++)
    {
        // begin new page
        dc.StartPage();
        Info.m_nCurPage = page;
       
        // calc how much text fits on one page
        r = Info.m_rectDraw;
        r.bottom = r.top;
        int i = 0;
        while (r.bottom < Info.m_rectDraw.bottom & totalDone + i < lengthToGo)
        {
            r.right = Info.m_rectDraw.right;
            nHeight = dc.DrawText(startAt, i++, r,
                DT_CALCRECT|DT_WORDBREAK|DT_NOCLIP|DT_EXPANDTABS);
        }
        // go one back to assure correct height
        if (r.bottom >= Info.m_rectDraw.bottom)
            i--;
       
       
        // print that text
        dc.DrawText(startAt, i, r, DT_WORDBREAK|DT_NOCLIP|DT_EXPANDTABS);
       
        // go to next page
        startAt += i;
        totalDone += i;
       
        // end page
        bPrintingOK = (dc.EndPage() > 0);
    }
   
    // end a print job
    if (bPrintingOK)
        dc.EndDoc();
    else
        // abort job.
        dc.AbortDoc();
   
   
    // restore font
    dc.SelectObject(oldFont);
    // free font memory
    qFont.DeleteObject();
    // detach the printer DC
    dc.Detach();
}

0
Comment
Question by:cytay
  • 3
  • 3
7 Comments
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
If you never call
   dc.EndPage()
then it won't eject the page.  

But this sounds very odd.  You really can't leave the printer and the spooler 'dangling' with a half page.  

The only reason you might want to do this is because you expect to print some more data on the bottom part of the page.  In that case, the normal procedure would be to go ahead and print that data -- make it part of the same print job.

Can you explain your goal?  There may be other, better ways to accomplish it.

-- Dan
0
 

Author Comment

by:cytay
Comment Utility
Hi Dan,

Even if I commented out dc.EndPage(), it will still eject the page.

I am developing a program which uses dot matrix printer. Everytime the user places any purchase on sth then the program will print out the purchase detail. I need them to be printed on the same paper.

It doesn't make sense for me to eject out all the paper in this case right?

Thanks.
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
dc.EndPage() is called twice.

For dot-matrix output of partial page, it is probably best to use old style printing, where you open LPT1 as a file and dump text to it.

Windows output is page-oriented.  It is best to accumulate a full page, then print it all at once.

One outside-the-box idea is to define a short page (custom paper size) ... say about the length of 10 lines of text.  Then each output is a single page.  You would need to set up the printer so that it knows to advance only 10 lines for each page.  This will be printer-dependant.  Check all of the settings for the printer driver, there may be some help there.

-- Dan
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:cytay
Comment Utility
Hi Dan,

Still can't work out for this time...
the old method of open
FILE *ptr = fopen("LPT1","w");
should close by this
fclose(ptr);

I am facing problem that it only print once...
Thanks.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 300 total points
Comment Utility
>>I am facing problem that it only print once...

What do you mean? How many times do you want it to print?  

-- Dan
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> For dot-matrix output of partial page, it is probably best to use old style printing,
>> where you open  LPT1 as a file and dump text to it.
>>
>> Windows output is page-oriented.  It is best to accumulate a full page, then print it all at once.
to continue this thought...  windows output is page-oriented because most new printers are "page printers" as opposed to "line printers".  A page printer does not physically print until it has an entire page of information to print.  It cannot print partial pages.  

Theoretically you could leave a page partially "used" in the printer and then print to the rest of the page at a later time.  But windows will never let you do this.  Whenever you stopp printing to it, windows will assume the last page printed is a full page and will finsih cause the page to be printed.  It has to do this.    It is very reasonable to assume that programs want to print to whole pages.  Image what would happen if it didn't make this assumption.  If a program is printing and crashes it might leave a page 1/2 done.  Then the next program to print would have its output mixed with the previous program's output.   Windows won't allow this.

Your best bet is to rethink your entire approach.

Write the information to a log file and when you have a full page worth, then print the info.  (We write point-of-sales systems and this is precisely what we do.)  There are many advantages to this.

For example, consider what you will do now if the printer jams?   You can reprint the 2nd part of the page since you are generatig that "currently" but the 1st part of the page was generated earlier, could you regenerate it?  probably not.   By keeping a log you can regenerate all the necessary info to perform a reprint.
0
 

Author Comment

by:cytay
Comment Utility
Dan,

Thanks for your suggestions and comments. I have solved the problem by using the old method of printing.

nietod,

Thanks to you too. Unfortunately I have to give the points to Dan becoz he was here first. :)



0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now