4

I'm trying to paint over a RichTextBox but the only way I can do it is by calling OnPaint/OnPaintBackground.

The problem is the OnPaint or OnPaintBackground aren't called unless the "UserPaint" flag is on, but when this flag is on - the text itself won't be painted!

how can I solve this?

karthik
  • 17,453
  • 70
  • 78
  • 122
Idov
  • 5,006
  • 17
  • 69
  • 106
  • What kind of 'paint over' are you talking about? Changing back color? Drawing on top of the text? Under it? – John Arlen Mar 08 '11 at 21:47

1 Answers1

9

This is the code I use to ensure OnPaint is called after RichTextBox has handled the painting itself first:

class MyRichTextBox: RichTextBox
{
    private const int WM_PAINT = 15;
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
       base.WndProc (ref m);
       if (m.Msg == WM_PAINT && !inhibitPaint)
       {
           // raise the paint event
           using (Graphics graphic = base.CreateGraphics())
               OnPaint(new PaintEventArgs(graphic,
                base.ClientRectangle));
       }

   }

    private bool inhibitPaint = false;

    public bool InhibitPaint
    {
        set { inhibitPaint = value; }
    }


}
pgfearo
  • 2,087
  • 1
  • 22
  • 27