• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 740
  • Last Modified:

Printing RichEdit with colored background


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,

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

bogie2Author Commented:
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.
bogie2Author Commented:
Any other ideas?
Get your problem seen by more experts

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

     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..
bogie2Author Commented:
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.
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.
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).

bogie2Author Commented:
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!
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now