Skip to main content
Email Password   helpLost your password?

Introduction

The problem is that, it seems that the TextBox and the RichTextBox do not accept Color.Transparent for their BackColor property. I developed a solution for the RichTextBox control and a synthesized solution from Bob Bradley's article for the TextBox control.

This is not a clear, nice solution. I am just contributing. Beginners may have a tough time understanding this. If you feel at home with OOP and C#, then this article should be a light snack for you.

RichTextBox

class TransparentControl : Control
{
   public TransparentControl()
   {
      base.SetStyle( ControlStyles.UserPaint, true );
      base.SetStyle( ControlStyles.DoubleBuffer, true );
      base.SetStyle( ControlStyles.SupportsTransparentBackColor, true );
   }
}

class TransparentRichTextBox : RichTextBox
{
   public TransparentRichTextBox()
   {
      base.ScrollBars = RichTextBoxScrollBars.None;
   }

   override protected CreateParams CreateParams
   {
      get
      {
         CreateParams cp = base.CreateParams;
         cp.ExStyle |= 0x20;
         return cp;
      }
   }

   override protected void OnPaintBackground( PaintEventArgs e )
   {
   }
}

TextBox

public class TransparentTextBox : TextBox
{
   PictureBox pictureBox = new PictureBox();

   public TransparentTextBox()
   {
      pictureBox.Dock = DockStyle.Fill;
      this.Controls.Add( pictureBox ); 
   }
   protected override void WndProc( ref Message m )
   {
      base.WndProc( ref m );
      switch( m.Msg )
      {
         case Win32.WM_PAINT:

            Bitmap bmpCaptured = 
              new Bitmap( this.ClientRectangle.Width, this.ClientRectangle.Height );
            Bitmap bmpResult = 
              new Bitmap( this.ClientRectangle.Width,this.ClientRectangle.Height );
            Rectangle r = 
              new Rectangle( 0, 0, this.ClientRectangle.Width, 
              this.ClientRectangle.Height );
 
            CaptureWindow( this, ref bmpCaptured ); 
            this.SetStyle( ControlStyles.SupportsTransparentBackColor, true );
            this.BackColor = Color.Transparent;

            ImageAttributes imgAttrib = new ImageAttributes();

            ColorMap[] colorMap = new ColorMap[ 1 ];

            colorMap[ 0 ] = new ColorMap();

            colorMap[ 0 ].OldColor = Color.White;

            colorMap[ 0 ].NewColor = Color.Transparent;

            imgAttrib.SetRemapTable( colorMap ); 

            Graphics g = Graphics.FromImage( bmpResult );

            g.DrawImage( bmpCaptured, r, 0 , 0, this.ClientRectangle.Width, 
                this.ClientRectangle.Height, GraphicsUnit.Pixel, imgAttrib );

            g.Dispose();

            pictureBox.Image = ( Image )bmpResult.Clone(); 
         break;


         case Win32.WM_HSCROLL:

         case Win32.WM_VSCROLL:

            this.Invalidate(); // repaint

           // if you use scrolling then add these two case statements


         break;
   }
}

private static void CaptureWindow( Control control, ref Bitmap bitmap )
{
   Graphics g = Graphics.FromImage( bitmap );
   int i = ( int )( Win32.PRF_CLIENT | Win32.PRF_ERASEBKGND );
   IntPtr iPtr = new IntPtr( 14 );
   IntPtr hdc = g.GetHdc();
   Win32.SendMessage( control.Handle, Win32.WM_PRINT, hdc, iPtr ); 
   g.ReleaseHdc( hdc );
   g.Dispose();
}
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralVery well Pin
gajatko
1:06 5 Jul '07  
GeneralRe: Very well Pin
Rene Dohan
1:51 31 Jul '07  
GeneralWorking example Pin
Đonny
9:58 19 May '07  
GeneralIf you want a working example... Pin
jmw
8:44 5 Jul '06  
GeneralRe: If you want a working example... Pin
Gx Lam
17:07 4 May '08  
GeneralWhen typing text the control is not refreshed, text looks bad Pin
cscholes
12:54 28 Jul '05  
GeneralRe: When typing text the control is not refreshed, text looks bad Pin
Jeff W
8:29 13 Sep '05  
AnswerRe: When typing text the control is not refreshed, text looks bad Pin
Jimsy
14:57 9 Jan '06  
GeneralRe: When typing text the control is not refreshed, text looks bad Pin
janro
22:46 25 Sep '08  
GeneralAnymore Progress on this? Pin
Anonymous
10:40 4 Feb '05  
Generaldoesn't work for me VS 2003 Pin
BillWoodruff
22:24 20 Nov '04  
GeneralRe: doesn't work for me VS 2003 Pin
dudubravo
3:14 22 Nov '04  
GeneralSTILL: doesn't work for me VS 2003 Pin
BillWoodruff
14:37 22 Nov '04  
GeneralRe: STILL: doesn't work for me VS 2003 Pin
dudubravo
4:00 23 Nov '04  
Generalbug Pin
GoAn
16:31 19 Nov '04  
GeneralRe: bug Pin
Anonymous
6:33 20 Nov '04  
GeneralRe: bug Pin
dudubravo
3:15 22 Nov '04  
GeneralNot working Pin
Anonymous
12:33 19 Nov '04  
GeneralRe: Not working Pin
kuerbis
14:10 19 Nov '04  
GeneralRe: Not working Pin
GoAn
16:13 19 Nov '04  
GeneralPost an example Pin
ilpadrino79
7:32 26 Nov '04  


Last Updated 19 Nov 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009