[Webinar] Streamline your web hosting managementRegister Today


Printing RichEdit with colored background

Posted on 1998-08-09
Medium Priority
Last Modified: 2013-11-20

currently, I face the following problem:

I want to print a RichEdit-window with a non-white background. I use
CRichEdit::SetBackgroundColor() to modify the background color.

This works fine for onscreen display, however, when I print the contents
of the control using CRichEdit::FormatRange() and CRichEdit::DisplayBand()
the background is ALWAYS white. So I tried to emulate the background color
(during Printing) using CDC::FillSolidRect(). Unfortunately, once the back-
ground is filled, the DisplayBand() method wont output the text any longer.
I don't know why. Even using CDC::SaveDC() before and calling
CDC::RestoreDC(-1) after the FillSolidRect call wont help here.
DisplayBand does not output anything OVER the previously painted background.

Any help will be greatly appreciated.

Thank you,

Question by:bogie2

Expert Comment

ID: 1320467
Please try to use pDC->SetBkColor (RGB()); to set the CDC background color. Good luck.


Author Comment

ID: 1320468
Sorry, but this does not help. If I set the DC's background color using CDC::SetBkColor()
to any other value then RGB(255,255,255), all output during printing is completely black
(during print preview it works fine, but not during printing). In addition, only the part
containing text is painted with the background color.

Author Comment

ID: 1320469
Any other ideas?
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.


Expert Comment

ID: 1320470
     I couldn't see any problem when i was doing similar things.. please go through the code given below.. I used it to print colored edit box contents.. Please ignore any unnecessary data.. i just cut and paste from my source,,,

          LOGBRUSH lbrush;
      lbrush = m_Document->GetLogBrush();
      bool bbrush = SetBrush(lbrush);

      LOGPEN lpen;
      lpen = m_Document->GetLogPen();
      bool bpen = SetPen(lpen);

      COLORREF prevclr = m_PrintDC->SetBkColor( lbrush.lbColor);

      RECT rcRect = m_Document->GetCtrlRect();

      bool bfont = SetFont(m_Document->GetLogFont());
      int caps = m_PrintDC->GetDeviceCaps(LOGPIXELSY) ;

      rcRect.left = (rcRect.left) + (long)m_XOffset-m_iXMargin;
      rcRect.left = (rcRect.left) + 31 ;
      rcRect.top = (rcRect.top)+ (long)m_YOffset-m_iYMargin;
      rcRect.right = (rcRect.right)+ (long)m_XOffset-m_iXMargin;
      rcRect.bottom = (rcRect.bottom) + (long)m_YOffset-m_iYMargin;


            Please ignore unnecessary variables.. This should work as i am getting it..
           Best wishes..

Author Comment

ID: 1320471
Sorry, but your answer does not help at all. We're talking about a RichEdit-Control, which
is, in its nature of supporting different Fonts in different styles and different sizes etc.,
completely different from an EditControl. As I wrote, I do the output using the
CRichEdit::FormatRange() method rather than a CDC:: call.Maybe the problem has do soemthing with Banding, I don't know.

Expert Comment

ID: 1320472
As you may have awared, RichEdit is quite different from all the other objects from which it was derived. I have tried "all" the kinds of method to set the background color of it, while most of them work for a generic Cedit, will not work for RichEdit. I guess that is why MS provide a special function to set its own bkd color, but it does not work for printing. I think the funtionality of the RichEdit is not complete.
So I think that the only way to work it out is to set the print DC bkd color. Although it may not have influence on where the text is not displayed, it will change the bkd color with the text. I would like to know what kind of printer you are using. If it is a B&W printer, you should set the mode as color. So that the bkd blackness will change with the color you selected. Pease try it. Or you may still hope a thourough method to solve it. I also hope it.

Expert Comment

ID: 1320473
Try printing a color bitmap, first of all thru Paintbrush or a similar program.

Assuming you have no problems, it means you can print a color bitmap to your printer.

Now here's the solution.

1. Make a memory bitmap (using CreateCompatibleBitmap)
2. Create a DC for it (using CreateCompatibleDC)
3. Clean bitmap using FillSolidRect
4. Using FormatRange/DisplayBand to transfer segements of the rich text on to the bitmap.
5. Send the bitmap to the printer (using BitBlt or StretchBlt or one of the similar calls)
6. repeat steps 4-5 until done for a page
7. Clean up the DC and memory bitmap.

If doing multiple pages you can either do all the steps for every page, or re-use the bitmap between pages (create it once at the start of printing, reuse steps 4-6 for each page, and clean up when printing is done).


Author Comment

ID: 1320474
I get the idea, but, alas, it's of no use to me. Many many printers do not support BitBlt, but I need a solution working with ANY printer driver. Does anyone know why CDC::FormatRange won't output to any previously painted region? I mean, how does the method know that there is already something? Even if I paint the area white, it won't output text any longer. That's the most confusing aspect. I know that a solution (or a workaround) is quite difficult, that's why I just increased the points to 400. RichEdit really is different, unfortunately. Please help!

Accepted Solution

dmunoz earned 800 total points
ID: 1320475
solution for a CRichEditView derived view
(this work fine with a simple Wizard-generated application)

on OnInitialUpdate, add for a yellow background:

void CRichEditViewView::OnInitialUpdate()

  // Set the printing margins (720 twips = 1/2 inch).
  SetMargins(CRect(720, 720, 720, 720));

  GetRichEditCtrl ().SetBackgroundColor(FALSE,RGB(255,255,0));

add function OnPrint to the interface (with ClassWizard if you want)

void CRichEditViewView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
  // TODO: Add your specialized code here and/or call the base class
  int iHORZRES=pDC->GetDeviceCaps (HORZRES);
  int iVERTRES=pDC->GetDeviceCaps (VERTRES);
  pDC->FillSolidRect (CRect (CPoint (0,0), CSize (iHORZRES,iVERTRES)),RGB(255,255,0));
  pDC->SetBkColor (RGB(255,255,0));

  CRichEditView::OnPrint(pDC, pInfo);

FillSolidRect is to have a page totally filled with color,  
and not only the background of the RTF text
Please note that if your current printer have color printing capabilities,
all should be drawn OK in the print preview.
But, if it's a laser for example, background text could be previewed in white
(or black if your background color density if dark). This should not influence
the final print, because at the end the printer driver convert color to gray shades.
This gray shade conversion is not handled by standard CRichEditCtrl drawing.


Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

590 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