RichTextBox and UserPaint

家住魔仙堡 提交于 2019-11-27 19:30:17

问题


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?


回答1:


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


}


来源:https://stackoverflow.com/questions/5041348/richtextbox-and-userpaint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!