Click here to Skip to main content
6,295,667 members and growing! (12,409 online)
Email Password   helpLost your password?
Desktop Development » Edit Controls » General     Intermediate

How to make a TextBox/RichTextBox transparent

By dudubravo

This article shows how to create transparent TextBox control in C#.
C#.NET 1.1, Win2K, WinXPVS.NET2003, Dev
Posted:19 Nov 2004
Views:88,244
Bookmarked:41 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
19 votes for this article.
Popularity: 3.20 Rating: 2.50 out of 5
8 votes, 42.1%
1
4 votes, 21.1%
2

3
4 votes, 21.1%
4
3 votes, 15.8%
5

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

dudubravo


Member

Location: United States United States

Other popular Edit Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
GeneralVery well Pinmembergajatko1:06 5 Jul '07  
GeneralRe: Very well PinmemberRene Dohan1:51 31 Jul '07  
GeneralWorking example PinmemberĐonny9:58 19 May '07  
GeneralIf you want a working example... Pinmemberjmw8:44 5 Jul '06  
GeneralRe: If you want a working example... PinmemberGx Lam17:07 4 May '08  
GeneralWhen typing text the control is not refreshed, text looks bad Pinmembercscholes12:54 28 Jul '05  
GeneralRe: When typing text the control is not refreshed, text looks bad PinmemberJeff W8:29 13 Sep '05  
AnswerRe: When typing text the control is not refreshed, text looks bad PinmemberJimsy14:57 9 Jan '06  
GeneralRe: When typing text the control is not refreshed, text looks bad Pinmemberjanro22:46 25 Sep '08  
GeneralAnymore Progress on this? PinsussAnonymous10:40 4 Feb '05  
Generaldoesn't work for me VS 2003 PinmemberBillWoodruff22:24 20 Nov '04  
GeneralRe: doesn't work for me VS 2003 Pinmemberdudubravo3:14 22 Nov '04  
GeneralSTILL: doesn't work for me VS 2003 PinmemberBillWoodruff14:37 22 Nov '04  
GeneralRe: STILL: doesn't work for me VS 2003 Pinmemberdudubravo4:00 23 Nov '04  
Generalbug PinmemberGoAn16:31 19 Nov '04  
GeneralRe: bug PinsussAnonymous6:33 20 Nov '04  
GeneralRe: bug Pinmemberdudubravo3:15 22 Nov '04  
GeneralNot working PinsussAnonymous12:33 19 Nov '04  
GeneralRe: Not working Pinmemberkuerbis14:10 19 Nov '04  
GeneralRe: Not working PinmemberGoAn16:13 19 Nov '04  
GeneralPost an example Pinmemberilpadrino797:32 26 Nov '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Nov 2004
Editor: Sumalatha K.R.
Copyright 2004 by dudubravo
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project