我重载了RichTextBox的OnPaint方法,怎么运行时不被调用?
重载代码:
protected override void OnPaint(PaintEventArgs e) 
        {
            base.OnPaint(e);            Graphics grp = e.Graphics;             Rectangle rect = new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);
            Brush brush = new SolidBrush(Color.Black);            Pen myPen = new Pen(brush);            myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            myPen.Width = 2;
            grp.DrawRectangle(myPen, rect);
        }
代码是在我自己定义的类里面,把这个空间放在Form,运行,在这个函数中打断点,走不到这里.是怎么回事呢?

解决方案 »

  1.   

    要这样:using System;
    using System.Windows.Forms;namespace MyControls
    {
        class RichTextBox : System.Windows.Forms.RichTextBox
        {
             protected override void OnPaint(PaintEventArgs e) 
            { 
                base.OnPaint(e);             Graphics grp = e.Graphics;             Rectangle rect = new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height); 
                Brush brush = new SolidBrush(Color.Black);             Pen myPen = new Pen(brush);             myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; 
                myPen.Width = 2; 
                grp.DrawRectangle(myPen, rect); 
            } 
        }
    }使用时:using System;
    using System.Windows.Forms;
    using MyControlsclass Test
    {
        RichTextBox richTextBox1;
    }
      

  2.   

    我代码是这样一来的:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace AXS.Board.Core.Control
    {
        public class RichTextBox : System.Windows.Forms.RichTextBox
        {
           [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
            static extern IntPtr LoadLibrary(string lpFileName);        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
            private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);        public EventHandler BdTextChanged = null;
            public RichTextBox()
            {
                this.TextChanged += new EventHandler(RichEdit50_TextChanged);
            }        private void RichEdit50_TextChanged(object sender, EventArgs e)
            {
                int EM_GETLINECOUNT = 0x00BA;//获取总行数的消息号 
                int lc = SendMessage(this.Handle, EM_GETLINECOUNT, IntPtr.Zero, "");
                int sf = this.Font.Height * (lc + 1) + this.Location.Y + 5;            System.Drawing.Graphics grp = default(System.Drawing.Graphics);
                System.Drawing.SizeF TextSize = new System.Drawing.SizeF();            grp = this.CreateGraphics();
                TextSize = grp.MeasureString(this.Text, this.Font);            if (lc * this.Font.Height > this.Height - 10)
                {
                    this.Size = new System.Drawing.Size(this.Width, this.Height + this.Font.Height);
                }            if (BdTextChanged != null)
                {
                    BdTextChanged(sender, e);
                }
            }        protected override void OnPaint(PaintEventArgs e) 
            {
                base.OnPaint(e);            Graphics grp = e.Graphics;             Rectangle rect = new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);
                Brush brush = new SolidBrush(Color.Black);            Pen myPen = new Pen(brush);            myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                myPen.Width = 2;
                grp.DrawRectangle(myPen, rect);
            }
            //protected override CreateParams CreateParams
            //{
            //    get
            //    {
            //        CreateParams prams = base.CreateParams;
            //        if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
            //        {
            //            prams.ExStyle |= 0x020; // transparent 
            //            prams.ClassName = "RICHEDIT50W";
            //        }
            //        return prams;
            //    }
            //}
        }
    }
    用时仍OnPaint起不到作用.