Printing RichEdit with colored background

Posted on 1998-08-09
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?
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.


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 ; = ( (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 400 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand to include initial dialog with two choices. 9 78
java ^ examples 8 66
Modbus - whats the maximum I can store in one register? 4 80
scoreUp challenge 14 63
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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