Solved

Printing without built-in support (PLEASE HELP)!

Posted on 1998-11-11
9
563 Views
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;
  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
Comment
Question by:newton1
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:plaroche
ID: 1324503
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
 

Author Comment

by:newton1
ID: 1324504
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
 
LVL 2

Expert Comment

by:wyy_cq
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!!! =)


0
 
LVL 3

Expert Comment

by:plaroche
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?  
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:newton1
ID: 1324507
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
 
LVL 1

Expert Comment

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

Accepted Solution

by:
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! ;-)

0
 

Author Comment

by:newton1
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!

0
 
LVL 2

Expert Comment

by:wyy_cq
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Separate files macro - Errors on larger data set 18 65
notReplace  challenge 53 116
java ^ examples 8 62
NotAlone Challenge 20 79
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

16 Experts available now in Live!

Get 1:1 Help Now