Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 576
  • Last Modified:

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;
  font.CreateFont(-280,0,0,0,400,FALSE,FALSE,
                  0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
                  CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
                  NULL,"Courier New");        
     // create a CPrintDialog object        
     CPrintDialog dlg(FALSE, PD_ALLPAGES |
                      PD_RETURNDC,NULL);
     dlg.DoModal();                
     if(!dlg.m_pd.hDC){                
     // if no device context clean up, bail out
       MessageBox("Invalid Printer Device Context...");
       font.DeleteObject();                        
       return;
     }            

    // attach the printer DC handle to the CDC object      
    pDC->Attach(dlg.m_pd.hDC);
    TRACE("Starting doc\n");
    pDC->StartDoc("MyDocument");                
    pDC->StartPage();
   
    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                        
    pDC->SelectObject(pFont);
    pDC->EndPage();                
    pDC->EndDoc();        
    // delete the printer device handle          
    pDC->DeleteDC();        
    pDC->Detach();                
    // clean up memory        
    delete pDC;                  
    font.DeleteObject();
}
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 newton1@hotmail.com
-or- EMiller@zetec.com.

Thanks alot for all of your help.
0
newton1
Asked:
newton1
  • 3
  • 3
  • 2
  • +1
1 Solution
 
plarocheCommented:
1) Try replacing your line:
pDC->Attach(dlg.m_pd.hDC);
with
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.
0
 
newton1Author Commented:
plaroche,
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.

0
 
wyy_cqCommented:
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!!! =)


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
plarocheCommented:
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?  
0
 
newton1Author Commented:
wyy_cq,
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
Newton1
0
 
BridgeCommented:
It might be due to the mapping mode you are using. Try setting the y-axis value to negative
0
 
plarocheCommented:
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! ;-)

0
 
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!

0
 
wyy_cqCommented:
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
0

Featured Post

Technology Partners: 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!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now