Solved

Printing Selected Text

Posted on 1998-08-17
6
251 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Expert Comment

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

Author Comment

by:gkibble
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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

11 Experts available now in Live!

Get 1:1 Help Now