Improve company productivity with a Business Account.Sign Up


Printing Selected Text

Posted on 1998-08-17
Medium Priority
Last Modified: 2010-04-10
I've managed to enable "Print Selected"in CPrintDialog by using pCPrintInfo->m_pPD->m_pd.Flags &= ~PD_NOSELECTION in OnPreparePrinting().  However despite clicking on "Print Selected", my selection is ignored and everything is printed.  Any clues on forcing print to use only selected text in a CRichEditView?

Question by:gkibble
  • 3
  • 2

Expert Comment

ID: 1170563
I have figured out the way to realize printing the selected text in a CRichEditView. Add the following codes in your OnPrint() function:
      CString cstrnBody;
      cstrnBody = GetRichEditCtrl().GetSelText();
      pDC->TextOut(10, 10, cstrnBody);

      //CRichEditView::OnPrint(pDC, pInfo);

You can make modification as you want to get desired output. Good luck.


Author Comment

ID: 1170564
The TextOut command is far too simple for what I need. We have lots of formatting, fonts, weights, etc that are lost with CString.  I really want to avoid writing code to support formatting since it works so well now for full pages.  Isn't there some way to set a starting and ending character position for OnPrint()?


Accepted Solution

Answers2000 earned 600 total points
ID: 1170565
In your OnPrint
1. Get the control window using GetRichEditCtrl()
2. Use GetSel member of CRichEditCtrl to get the selection range into a variable of type CHARRANGE
3. Fill in a FORMATRANGE structure with the results of #2
4. Call CRichEditCtrl::FormatRange with this structure
5. Call CRichEditCtrl::Display band

Steps 4 and 5 are used to output to the printer DC.  To avoid outputting too much (e.g. going off the bottom of the page), use FormatRange to do measurements first, then adjust for each page in sequence using 4 and 5 in a loop.

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.


Expert Comment

ID: 1170566
I think the principle of An...'s answer is similiar to mine except that he used a defferent function.

Author Comment

ID: 1170567
Almost an A grade!   I'm still fuzzy on the last part "use FormatRange to do measurements first, then adjust for each page in sequence using 4 and 5 in a loop.".  But for short selections it works perfectly.

Some other useful details I found elsewhere:
       FORMATRANGE fr;
        ::ZeroMemory(&fr, sizeof(FORMATRANGE));

        HDC hTarget = pDC->GetSafeHdc();
        fr.hdc           = hTarget;
        fr.hdcTarget = hTarget;


Author Comment

ID: 1170568
When I used pDC->TextOut(10, 10, cstrnBody);  many things were lost besides just formatting.  Tabs and line feeds were printed as non-displayable which munged everything into one long line.   With FormatRange() all symbols displayed properly.


Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

606 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