I have been wanted to create a custom text box and so I have found two methods which would allow me to achieve this although they aren't the best. One of the them is to actually draw text on the picturebox itself, but I wasn't able to find any tutorials or code to allow me to create this.
But I did find something for a Alpha channel textbox which although didn't allow me to put the control over other controls, I was able to work with it. The code works but my only problem now that is when other people use it the text is blurry.
Like this: http://puu.sh/4hzQM.png
This is how it looks like for me: http://puu.sh/4hzUD.jpg
I am currently using this code, which I converted to vb.net: http://www.codeproject.com/Articles/4390/AlphaBlendTextBox-A-transparent-translucent-textbo
I was wondering if there is a way to fix this blurry text on other computers or if there is an easier way of doing this, because that code is 10 years old and everywhere I search, I can't find anything else.
I found this MSDN link proposing a pretty simple way to make a
TextBox transparent: making its
BackColor equal to the one of the container Control. This sounds as an acceptable approach for most of the situations. Here you have a custom TextBox relying on this idea:
Public Class transparentTxtBox Inherits TextBox Public Sub New() MyBase.BorderStyle = BorderStyle.None End Sub Public Overrides Property BackColor() As Color Get Dim curColor As Color = Color.White If (MyBase.Parent IsNot Nothing) Then curColor = MyBase.Parent.BackColor End If Return curColor End Get Set(ByVal Value As Color) MyBase.BackColor = Value End Set End Property End Class
If you add this code to your project, you would be able to use it as a normal TextBox (view "Design View" by taking it from the ToolBox) or via code by writing something like:
Dim textBox1 As transparentTxtBox = New transparentTxtBox() Me.Controls.Add(textBox1)
RichTextBox transparent via WinAPI call; code adapted from the one found in one of the answers in this link. Start a new project, add a RichTextBox (
RichTextBox1) to it and write this code:
Private Const GWL_EXSTYLE = (-20) Private Const WS_EX_TRANSPARENT = &H20& Private Declare Auto Function SetWindowLong Lib "User32.Dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load RichTextBox1.Text = "Text I want to see on a transparent background" SetWindowLong(RichTextBox1.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT) End Sub Private Sub RichTextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles RichTextBox1.TextChanged Dim curPos As Integer = RichTextBox1.SelectionStart RichTextBox1.Select(0, RichTextBox1.TextLength) RichTextBox1.Select(curPos, 0) RichTextBox1.ScrollToCaret() End Sub
NOTE: aparently this WinAPI call provokes a "freezing of the cursor" which an be avoided with the code under the
TextChanged Event (just by selecting all the text). This is a temporary fix (which delivers a good performance though) and thus this code might be updated in the future.
UPDATE (10-Nov-2016): IInspectable has criticised various parts of this answer (read the comments below); also included this link which seems (I didn't read it) to explain why using
WS_EX_TRANSPARENT isn't a good idea. As explained in the comments below, my interest in all this is now (i.e., 3 years after having written the answer) inexistent. In fact, if I could delete this post I would do it (I cannot because the OP chose it as the right one).