Click here to Skip to main content
12,890,424 members (58,984 online)
Click here to Skip to main content
Add your own
alternative version


133 bookmarked
Posted 23 Jun 2003

AlphaBlendTextBox - A transparent/translucent textbox for .NET

, 23 Jun 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
A transparent/translucent textbox for .NET

Sample Image - AlphaBlendTextBox1.gif


While working on another multi-media program, I found myself in need of a transparent edit control (TextBox) for .NET. I soon discovered that if there is one out there, then it must be so transparent that I just can't find it. In my searches, I did find that there is an AlphaBlend filter available for web buttons hosted by IE, but I could find none for Win Forms. So I set out to write one.

I present here version 1.0 of AlphaBlendTextBox written in C#. Be warned that it is not fully tested, not fully optimized and is not yet in use in any production system. However, I wanted to submit it here, hoping that some of you will find the control and it's source code useful.

A big note: I have used Visual Studio .NET 2003 and .NET framework 1.1 to create this control and the sample projects. I will repost a VS2002 .NET 1.0 version as soon as I can.


While designing this control, I decided that the best method would be to subclass the existing TextBox control, so that the new control could be used anywhere that the original TextBox control could. A quick discovery was that the TextBox control is a real pain to subclass. Several programmers out there have noted that it can be an all-or-nothing process.

This is because in the current versions of .NET, the TextBox control is just a wrapper for the old Win32 control. This makes it difficult to modify. If you override the OnPaint method for the control, it is ignored unless you change the control's style to UserPaint. However, doing this keeps the control from drawing itself at all, until the user selects some text, and even then painting is erratic.

Back to my original problem. I wanted a TextBox control that was transparent to the background. While looking through the help files, I noticed that the .NET controls have a new style called SupportsTransparentBackColor that would seem to do the trick. However, this style requires that the UserPaint style be set, and as noted above this keeps the TextBox control from drawing itself. Worse yet, when the TextBox did paint, it still had a solid background.

After several weeks of frustration, I came up with this solution: subclass the TextBox control and add a PictureBox to the control that gets displayed, instead of the original TextBox.

When the control needs to be painted, it does the following:

  • temporarily sets the UserPaint style to false
  • captures a bitmap of the original TextBox by sending a WM_PRINT message
  • switches back to UserPaint style
  • uses a ColorMap to convert the solid background into a transparent/alpha background
  • figures out where the caret should be and draws it if necessary
  • then copies the new alpha image to the PictureBox, which when drawn will be blended with the background

In addition, the PictureBox had to be overridden so that any mouse event sent to it would be redirected to the TextBox. Also, several public properties and methods of the TextBox had to be overridden, such as Font, MultiLine, etc.

Using the code

This control can be used in the same manner as the standard TextBox control. I have compiled the control as a control library (DLL) file. To use this control from the Visual Studio designer, simply right click on the tool bar and choose "Add/Remove Items" and then click "Browse" and browse to the AlphaBlendTextBox.dll file. After doing this, the AlphaBlendTextBox control should appear in your tool bar (under "My User Controls" if you have VS2003). Now you can drag the control to a form and use it just like the original TextBox control. The control has a new property named BackAlpha that will allow you to set the control's background alphablend value. The range is 0 for transparent, 255 for opaque, or in between for translucent.

Properties snap shot

Another way to use the control is to add it to your project, using the "Project:Add Reference" menu. The control is under the ZBobb namespace so you can then use code like below:

// Sample C# code
private ZBobb.AlphaBlendTextBox alphaBlendTextBox1; 
private void Form1_Load(object sender, System.EventArgs e)
    alphaBlendTextBox1 = new ZBobb.AlphaBlendTextBox();
    alphaBlendTextBox1.Location = new System.Drawing.Point(32, 16);
    this.alphaBlendTextBox1.Text = "Hello";
    alphaBlendTextBox1.BackAlpha = 0; // Totally transparent

The control can also be used in Visual Basic .NET and I have included a sample VB project.


This is my first posting to the Code Project. The Code Project has been a great source of help to me in my programming and I am glad to finally be able to give something back. I look forward to hearing your comments and suggestions, and I hope that this posting can be useful to you.


  • Version 1.0 released June 24, 2003.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bob Bradley
Web Developer
United States United States
Bob Bradley is a full time Computer Science instructor at the University of Tennessee at Martin. He has been preoccupied with computers since his first Commodore Vic-20. He is currently interested in XNA/XBox-360 programming!

You may also be interested in...

Comments and Discussions

JokeRe: EASY Option Pin
titwan6-Jun-07 22:40
membertitwan6-Jun-07 22:40 
GeneralRe: EASY Option Pin
xxjnxx29-Oct-07 19:48
memberxxjnxx29-Oct-07 19:48 
Stephen_Lahti11-Jan-07 15:50
memberStephen_Lahti11-Jan-07 15:50 
Generalcursor in RTL Pin
Mohammad Hammad17-Dec-06 4:56
memberMohammad Hammad17-Dec-06 4:56 
GeneralSomething similar for Compact Framework Pin
Bennne24-Nov-06 4:01
memberBennne24-Nov-06 4:01 
QuestionHELP!!!!!!!!!!!!!!!!!!!!!!!!! Pin
ItsMe!!!13-Nov-06 17:58
memberItsMe!!!13-Nov-06 17:58 
GeneralPicture behind Pin
ItsMe!!!13-Nov-06 17:30
memberItsMe!!!13-Nov-06 17:30 
QuestionProblem with Update of the Control Pin
FreewareFire19-Sep-06 11:34
memberFreewareFire19-Sep-06 11:34 
Hi all!

Currently i have a problem with the AlphaBlendTextBox Cry | :((

I'd put the control on my form. Then i draw a image animation (using a timer) with something like that:

<br />
                Graphics g = this.CreateGraphics();<br />
                g.DrawImage((Image)Skins.BitmapArray[CPicNum], 0, 0);<br />

where BitmapArray is an Array of Bitmaps... and CPicNum a counter var!

The Problem is that the Background of the control does not Update! I tried this.refresh, this.invalidate , AlphaBlendTextBox.invalidate() and so on! The form updates itself without any problems, just the AlphaBlendTextBox does not Update.

Does anyone has an idea why the background of the control does not update? Any suggestions?

Please let me know!

Thanks Big Grin | :-D


FreewareFire is in the House!

GeneralIt dosen't work when forecolor and backcolor is the same. Pin
o_intania8117-Apr-06 18:29
membero_intania8117-Apr-06 18:29 
Generalnot working! Pin
Assaf Koren20-Feb-06 22:33
memberAssaf Koren20-Feb-06 22:33 
GeneralRe: not working! Pin
Aaron Dilliard21-Feb-06 8:53
memberAaron Dilliard21-Feb-06 8:53 
QuestionProblem with double click event Pin
Ravi KP22-Dec-05 17:31
memberRavi KP22-Dec-05 17:31 
NewsA Better Caret (Carrot???) Pin
santoii31-Oct-05 10:04
membersantoii31-Oct-05 10:04 
GeneralBorder Pin
Carl Mercier4-Aug-05 10:50
memberCarl Mercier4-Aug-05 10:50 
GeneralCtrl+A Keyboard Shortcut Pin
iheartbash3-Aug-05 4:09
sussiheartbash3-Aug-05 4:09 
QuestionSelectAll() on GotFocus? Pin
dingussquatbird26-Jul-05 12:09
memberdingussquatbird26-Jul-05 12:09 
GeneralNew DrawCaret property Pin
Carl Mercier22-Jul-05 6:15
memberCarl Mercier22-Jul-05 6:15 
GeneralI think I've come up with a better solution Pin
neal andrews26-Feb-05 9:18
memberneal andrews26-Feb-05 9:18 
GeneralRe: I think I've come up with a better solution Pin
Bjorn Ahlstedt3-May-05 0:08
sussBjorn Ahlstedt3-May-05 0:08 
GeneralRe: I think I've come up with a better solution Pin
Daniel Cardoso26-Apr-05 4:15
sussDaniel Cardoso26-Apr-05 4:15 
GeneralRe: I think I've come up with a better solution Pin
anant126-Mar-08 18:39
memberanant126-Mar-08 18:39 
GeneralMovin' it Pin
Le_MuLoT10-Feb-05 11:00
memberLe_MuLoT10-Feb-05 11:00 
QuestionMemory Leaks? Pin
Kuling24-Jan-05 0:49
memberKuling24-Jan-05 0:49 
AnswerRe: Memory Leaks? Pin
Brian Block18-Dec-09 8:52
memberBrian Block18-Dec-09 8:52 
GeneralThis is a beutiful thing. Pin
holon3-Oct-04 20:39
memberholon3-Oct-04 20:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 24 Jun 2003
Article Copyright 2003 by Bob Bradley
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid