How to develop a readonly transparent Textbox control in Windows Forms

I saw several questions in MSDN forums asking for how to develop a transparent TextBox or RichTextBox control that is not only ReadOnly but also transparent and does not display a “caret” icon when clicked on it. He is some sample code that can get you started..

 public class TransparentTextBox : RichTextBox
    {
        public TransparentTextBox()
        {}

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams TransparentTextBoxParams = base.CreateParams;

                //0x20 for transparent style 
                TransparentTextBoxParams.ExStyle |= 0x20; 
                return TransparentTextBoxParams;
            }
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            //We dont want to paint anything in the back ground.
            //base.OnPaintBackground(pevent);
            //Do nothing
        }

     
        protected override void WndProc(ref Message message)
        {
            // Supress the below events (To hide the blinking caret in the RichTextBox)
            // WM_NCLBUTTONDOWN WM_LBUTTONUP WM_LBUTTONDOWN 
            // WM_LBUTTONDBLCLK WM_RBUTTONDOWN WM_RBUTTONUP WM_RBUTTONDBLCLK  
            if (!(base.ReadOnly && 
                  (message.Msg == 0xa1 || 
                   message.Msg == 0x201 || 
                   message.Msg == 0x202 || 
                   message.Msg == 0x203 || 
                   message.Msg == 0x204 || 
                   message.Msg == 0x205 || 
                   message.Msg == 0x206)))
                base.WndProc(ref message);
        }
    }

 

Happy Coding!