Printing without built-in support (PLEASE HELP)!

I'm fairly new to the wonderful world of MFC. I'm using VC++ 5.0 with an MDI interface. My problem is the application was initially created without Printing support.
What I'm trying to do is print out a simple text style form using the values that are filled out in a bunch of ListCtrl's inside of a FormView. Unfortunately I get an ASSERT whenever I try to place a CString value in my TextOut function. I looked into the debugger and I see something that asks me if I called DoPreparePrinting()?.
Well here is the code I'm trying to use for my printing routine...
void CCalReportView::print_report()
  CCalDoc* pDoc = static_cast <CCalDoc*> (GetDocument());
  CDC * pDC = new CDC;  
  CFont font;
                  NULL,"Courier New");        
     // create a CPrintDialog object        
     CPrintDialog dlg(FALSE, PD_ALLPAGES |
     // if no device context clean up, bail out
       MessageBox("Invalid Printer Device Context...");

    // attach the printer DC handle to the CDC object      
    TRACE("Starting doc\n");
    CFont * pFont;
    pFont = pDC->SelectObject(&font);        
    // print to the printer device context
    // m_strTechname is a CString member  
    pDC->TextOut(10,10, m_strTechname);      
    // restore the old font                        
    // delete the printer device handle          
    // clean up memory        
    delete pDC;                  
I call this function from my Doc class after pressing a "Print Report" menu button. I have tried adding OnPreparePrinting() and returning DoPreparePrinting() but then I receive nothing at all but a "0" on the printed paper. If anyone has any ideas I would GREATLY appreciate it! And any ideas on a beter approach would be nice also, I do have alot of data I need to print out with multiple pages all contained within a FormView and ListCtrls. The m_strTechname is just the very beginning of the report and it's just an editbox. If anyone needs more information please don't hesitate to E-mail me at

Thanks alot for all of your help.
Who is Participating?
plarocheConnect With a Mentor Commented:
Here's the email I sent you, so that others may read it if they pay the points:

Your strings must come from somewhere or are they only contained in the list?  Either way you will have to retrieve them manually.

What I would do is to implement an object, let's call it CPrnList, which receives a printing  dc and a listctrl pointer.  What that object would do is retrieve the page size, print list items and call EndPage/StartPage if more items need to be printed on another page.

I know this is a lot of work at first but when doing this you will find that you are able to customize your reports at will. You will be able to make your object insert a footer or header .

When drawing the listctrl's items you will need to keep track of where you're at, draw the next item or change page.

I have done such a system but much more involved, where an object is a print container and you insert print objects into the container. The container then calculates where the objects will be printed and if more pages are needed.  Alas the code belongs to our client and I cannot send it. But the knowledge I can share! ;-)

1) Try replacing your line:
pDC = dlg.GetPrinterDC();

2) You call StartDoc with a string. This ain't the way the doc says it should be done. Here it is(from my code):

    DOCINFO     docInfo;

    memset(&docInfo, 0, sizeof(DOCINFO) );
    docInfo.cbSize      = sizeof(DOCINFO);
    docInfo.lpszDocName = _T("LVMS - Session");
    docInfo.lpszOutput  = NULL;

    if( pDC->StartDoc(&docInfo) == SP_ERROR ) {
        return FALSE;

This is what i can say for now. I have done many times printing on my own without doc/view support. If that doesn't solve it I'll try to help you otherwise.
newton1Author Commented:
Thanks for the help, but I'm still not getting any values for the CString object that I have. Perhaps the problem lies deeper than I thought. I have been trying many different ways to access the data other than using TextOut(10, 10, m_strTechname);
For example..TextOut(10, 10,GetDlgItemText(IDC_TECH_EDT, m_strTechname));
By doing this I get a couple of @ symbols. If you know have  any idea what is happening here, I would appreciate it.

Thanks again.

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

you need a simple way.
CView::OnPrint(CDC* pDC,CPrintInfo )
this function will call the OnDraw(...)
the the CDC* is a instance of CPrintDC
now you override this function and call another function
to print
of course you need add other functions of print
into the View
I hope this shall help you!!! =)

I really don't get the problem (for wyy_cq the question asked for printing without built-in support).  Your printing works but you have something completely bogus in your CString?  
newton1Author Commented:
Thanks for the answer, but the problem is more with my extraction of the CString members and getting them to display correctly on the page. The more I look at my initial question, the more I realize how it may be very misleading. If I could change the questions contents, I would. I'm trying to printout a simple text style report that will display many variables extracted from a series of ListCtrls within a CFormView. This is where my question concerning the pDC->TextOut() function call comes into play.
Once again I'm sorry for the inconvenience and misunderstanding of the initial question.
If you have a solution, or need more specific info, please E-mail me.

Thanks alot
It might be due to the mapping mode you are using. Try setting the y-axis value to negative
newton1Author Commented:
Thanks alot plaroche, I'm currently using your last idea, seems to work great.
I appreciate your help.

Thanks to everyone elses ideas and suggestions, always helpful!

HI newton1 .

I think you only need to print out,no matter what window your program ownning.

suppose you need to print in a console window.(this is the worst condition).

you can do these steps:
1.create a DC without using MFC .
2.write to the DC.
3.destroy it.

CreateDC(...) will help you create a DC of printer.
the printer device name will be found in the win.ini file
or in the register entry.
with this DC, you can do anything of print
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.