Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing over RichTextBox

I'm trying to draw borders around words and paragraphs in RichTextBox but when I turn on UserPaint it doesn't draw text anymore while my custom painting seems to work. May be I just forgot to turn on something else? Here is what I have

public partial class RichTextBoxEx : RichTextBox
{
    public RichTextBoxEx()
    {
        InitializeComponent();
        SetStyle(ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        //Do some painting here
    }
}

Using info from this question didn't help me

like image 807
Demarsch Avatar asked Jun 07 '13 17:06

Demarsch


1 Answers

This worked ok for me:

class RichBox : RichTextBox {
  private const int WM_PAINT = 15;

  protected override void WndProc(ref Message m) {
    if (m.Msg == WM_PAINT) {
      this.Invalidate();
      base.WndProc(ref m);
      using (Graphics g = Graphics.FromHwnd(this.Handle)) {
        g.DrawLine(Pens.Red, Point.Empty, 
                   new Point(this.ClientSize.Width - 1,
                             this.ClientSize.Height - 1));
      }
    } else {
      base.WndProc(ref m);
    }
  }
}
like image 136
LarsTech Avatar answered Sep 22 '22 05:09

LarsTech