RichTextBox和UserPaint

时间:2011-02-18 12:36:27

标签: c# winforms richtextbox

我正在尝试绘制RichTextBox,但我能做到的唯一方法就是调用OnPaint/OnPaintBackground

问题是除非“UserPaint”标志打开,否则不会调用OnPaint或OnPaintBackground,但是当此标志打开时 - 文本本身不会被绘制!

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:8)

这是我用来确保在RichTextBox首先处理绘画本身后调用OnPaint的代码:

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; }
    }


}