Printing without built-in support (PLEASE HELP)!

Posted on 1998-11-11
Last Modified: 2013-11-20
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.
Question by:newton1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1

Expert Comment

ID: 1324503
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.

Author Comment

ID: 1324504
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.


Expert Comment

ID: 1324505
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!!! =)

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1324506
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?  

Author Comment

ID: 1324507
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

Expert Comment

ID: 1324508
It might be due to the mapping mode you are using. Try setting the y-axis value to negative

Accepted Solution

plaroche earned 200 total points
ID: 1324509
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! ;-)


Author Comment

ID: 1324510
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!


Expert Comment

ID: 1324511
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

724 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