Click here to Skip to main content
11,802,076 members (55,977 online)
Click here to Skip to main content

AlphaBlendTextBox - A transparent/translucent textbox for .NET

, 23 Jun 2003 CPOL 381.8K 19.5K 133
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

Questionproblem in font Pin
Member 1144657826-Jun-15 0:20
memberMember 1144657826-Jun-15 0:20 
QuestionHow can I change the blinking cursor custom in alphablend Textbox? Pin
WaqasGhouri14-Apr-15 8:29
memberWaqasGhouri14-Apr-15 8:29 
Questionhow can we take this textbox multiple times? Pin
AhsanNajam6-Mar-14 7:18
memberAhsanNajam6-Mar-14 7:18 
QuestionThanks Pin
Member 102694573-Mar-14 0:55
memberMember 102694573-Mar-14 0:55 
QuestionProblem in Windows 7 Pin
jdebabrata18-Sep-13 15:05
memberjdebabrata18-Sep-13 15:05 
QuestionNot see text when inheritance from RichTextBox Pin
Poledo8-Aug-13 17:28
memberPoledo8-Aug-13 17:28 
GeneralMy vote of 5 Pin
behzad_khan7-Aug-13 3:04
memberbehzad_khan7-Aug-13 3:04 
GeneralMy vote of 5 Pin
karim_orooji25-Jul-13 21:54
memberkarim_orooji25-Jul-13 21:54 
QuestionGood Concept But there are other easy ways Pin
Nochdarus2-Jun-13 10:17
memberNochdarus2-Jun-13 10:17 
QuestionThe text alignment is buggy Pin
dgogoasa7-Apr-13 13:27
memberdgogoasa7-Apr-13 13:27 
GeneralMy vote of 5 Pin
pcs041417-Dec-12 2:38
memberpcs041417-Dec-12 2:38 
QuestionCannot Download Pin
Arben Myrtaj27-Oct-12 12:35
memberArben Myrtaj27-Oct-12 12:35 
QuestionAlphaBlendTextBox.dll Pin
vlad78113-Jul-12 20:36
groupvlad78113-Jul-12 20:36 
AnswerRe: AlphaBlendTextBox.dll Pin
JasonHoliday7613-Aug-12 11:00
memberJasonHoliday7613-Aug-12 11:00 
QuestionVSC# 2010 Pin
vlad78112-Jul-12 21:46
groupvlad78112-Jul-12 21:46 
QuestionGreat job! Thx Pin
Lamar Seifuddin5-Jul-12 13:13
memberLamar Seifuddin5-Jul-12 13:13 
GeneralMy vote of 5 Pin
Big D29-Jun-12 6:00
memberBig D29-Jun-12 6:00 
QuestionAnimation/video under the text box? Pin
briandavid7820-Jun-12 2:17
memberbriandavid7820-Jun-12 2:17 
GeneralMy Vote of 5 Pin
RaviRanjankr22-Sep-11 8:45
memberRaviRanjankr22-Sep-11 8:45 
QuestionMemory Leak Clarification. Pin
vibe3d25-Jun-11 5:11
membervibe3d25-Jun-11 5:11 
GeneralAlphaBlendTextBox Multifont property Pin
Member 78830021-May-11 20:41
memberMember 78830021-May-11 20:41 
QuestionWPF Version? Pin
trimeteus9-Apr-11 13:03
membertrimeteus9-Apr-11 13:03 
QuestionTextBox Click event Pin
Jesse English21-Feb-11 4:52
memberJesse English21-Feb-11 4:52 
GeneralThank you very much! Pin
nichill78914-Feb-11 14:41
membernichill78914-Feb-11 14:41 
GeneralClear type bug Pin
nuriyasov18-Sep-10 3:16
membernuriyasov18-Sep-10 3:16 
GeneralMemory leak. Pin
hibidiblahtimmeh11-Aug-10 3:52
memberhibidiblahtimmeh11-Aug-10 3:52 
GeneralRe: Memory leak. Pin
Roey C25-Jul-12 20:58
memberRoey C25-Jul-12 20:58 
GeneralMouseDoubleClick can not active... Pin
flynn1985112822-Jun-10 20:36
memberflynn1985112822-Jun-10 20:36 
Generalthank you Pin
gaoyuan1212118-Jun-10 19:21
membergaoyuan1212118-Jun-10 19:21 
GeneralIntermittent get width problem Pin
Lee Longenberger12-May-10 12:12
memberLee Longenberger12-May-10 12:12 
GeneralThanks so much! Pin
Le Van Long6-May-10 22:53
memberLe Van Long6-May-10 22:53 
GeneralI need something like this Pin
hang_em5-May-10 1:13
memberhang_em5-May-10 1:13 
I need to make something like this, a text box which can display a paragraph or two and behind it I would like to put a picture. Can anyone suggest or help me in this. I use 2003.

Would be very grateful if someone help me on this.

Generalworking great! Pin
superman0128-Feb-10 10:16
membersuperman0128-Feb-10 10:16 
GeneralSuper Bob! Pin
Loi KG7-Nov-09 1:32
memberLoi KG7-Nov-09 1:32 
Generalgetting Object reference not set to an instance of an object. in OnLostFocus event handler when entering a field Pin
Jim Darrah21-Jul-09 10:48
memberJim Darrah21-Jul-09 10:48 
GeneralThanks. Pin
joeyprest1-Mar-09 17:59
memberjoeyprest1-Mar-09 17:59 
GeneralLicense Pin
Lexx212-Dec-08 1:34
memberLexx212-Dec-08 1:34 
GeneralRe: License Pin
Bob Bradley15-Dec-08 6:30
memberBob Bradley15-Dec-08 6:30 
AnswerRe: License Pin
Lexx216-Dec-08 5:55
memberLexx216-Dec-08 5:55 
GeneralRe: License Pin
chafferm6-May-11 6:21
memberchafferm6-May-11 6:21 
QuestionEvent Pin
brajoh25-Sep-08 3:31
memberbrajoh25-Sep-08 3:31 
Questionbegg Pin
Isakku27-Aug-08 9:02
memberIsakku27-Aug-08 9:02 
Questionwhy that works? Pin
dennis.zeng25-Aug-08 1:26
memberdennis.zeng25-Aug-08 1:26 
GeneralIf I use RichTextBox, then I need to modify what part so that the control can work. Pin
xuezt10-Jun-08 4:19
memberxuezt10-Jun-08 4:19 
QuestionAlphablendTextBox Pin
anant124-Mar-08 20:32
memberanant124-Mar-08 20:32 
GeneralProblem with Forecolor Pin
jeff_v27-Feb-08 21:54
memberjeff_v27-Feb-08 21:54 
GeneralRe: Problem with Forecolor Pin
coocooletmoi24-Aug-10 6:13
membercoocooletmoi24-Aug-10 6:13 
GeneralNice Work Pin
JamesParsons29-Jan-08 9:25
memberJamesParsons29-Jan-08 9:25 
GeneralEASY Option Pin
deanpugh18-Jan-07 3:02
memberdeanpugh18-Jan-07 3:02 
JokeRe: EASY Option Pin
titwan6-Jun-07 22:40
membertitwan6-Jun-07 22:40 

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

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

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