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
Solved

Printing Selected Text

Posted on 1998-08-17
6
255 Views
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?

0
Comment
Question by:gkibble
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:trestan
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.

0
 

Author Comment

by:gkibble
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()?

0
 
LVL 8

Accepted Solution

by:
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.


0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 8

Expert Comment

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

Author Comment

by:gkibble
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;

0
 

Author Comment

by:gkibble
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.


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Would like to move button in a function 3 79
Best book to learn C++ 4 83
Safe conversion? 4 72
designing in object programming 12 94
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

791 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