本文介绍了RichTextBox和UserPaint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在RichTextBox上绘画,但是唯一的方法是调用 OnPaint / OnPaintBackground

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

问题是,除非打开 UserPaint标志,否则不会调用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!

我该如何解决?

推荐答案

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

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


}

这篇关于RichTextBox和UserPaint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 02:26