Printing Selected Text

Posted on 1998-08-17
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 200 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.

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

813 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

12 Experts available now in Live!

Get 1:1 Help Now