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
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
  • 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.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

800 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