Resolved TextBox Transparent

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
Is it possible to make a TextBox transparent?

I have a form that has a gradient background and I need the textbox to allow the user to type into and it shows the text and the blinking cursor carrot when it has focus, other than that it's totally transparent, like a label.
 
BackColor Property

Hi JuggaloBrotha,

There is an option for textbox properties, called "BackColor". If you click on the dropdown arrow and select the "Web" tab, at the top of the list is an option for "Transparent".

Hope that helps :)
 
The Transparent BackColor property of a textbox is invalid -- don't know why it was even included.

To make the textbox look more like a label, change the BorderStyle property to None, and the backcolor to Control.
 
In C# but should convert nicely I'd think

How to make a TextBox/RichTextBox transparent - CodeProject

Edit: Think this will work as a conversion.

VB.NET:
Public Class TransparentTextBox
    Inherits TextBox

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function

    Public Enum Win32
        WM_PAINT = &HF
        WM_PRINT = &H317
        WM_HSCROLL = &H114
        WM_VSCROLL = &H115
        PRF_CLIENT = &H4
        PRF_ERASEBKGND = &H8
    End Enum

    Private pictureBox As New PictureBox()

    Public Sub New()
        pictureBox.Dock = DockStyle.Fill
        Me.Controls.Add(pictureBox)
    End Sub
    Protected Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)
        Select Case m.Msg
            Case Win32.WM_PAINT

                Dim bmpCaptured As New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height)
                Dim bmpResult As New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height)
                Dim r As New Rectangle(0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height)

                CaptureWindow(Me, bmpCaptured)
                Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
                Me.BackColor = Color.Transparent

                Dim imgAttrib As New ImageAttributes()
                Dim colorMap As ColorMap() = New ColorMap(0) {}
                colorMap(0) = New ColorMap()
                colorMap(0).OldColor = Color.White
                colorMap(0).NewColor = Color.Transparent
                imgAttrib.SetRemapTable(colorMap)

                Dim g As Graphics = Graphics.FromImage(bmpResult)
                g.DrawImage(bmpCaptured, r, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height, _
                    GraphicsUnit.Pixel, imgAttrib)
                g.Dispose()

                pictureBox.Image = DirectCast(bmpResult.Clone(), Image)
                Exit Select

            Case Win32.WM_HSCROLL, Win32.WM_VSCROLL

                Me.Invalidate()
                ' repaint
                ' if you use scrolling then add these two case statements
                Exit Select
        End Select
    End Sub

    Private Shared Sub CaptureWindow(ByVal control As Control, ByRef bitmap As Bitmap)
        Dim g As Graphics = Graphics.FromImage(bitmap)
        Dim i As Integer = CInt(Win32.PRF_CLIENT Or Win32.PRF_ERASEBKGND)
        Dim iPtr As New IntPtr(14)
        Dim hdc As IntPtr = g.GetHdc()
        SendMessage(control.Handle, Win32.WM_PRINT, hdc, iPtr)
        g.ReleaseHdc(hdc)
        g.Dispose()
    End Sub
End Class
 
Last edited:
Thanks Matt, unfortunately it really doesn't work at all, well it does but it takes a few seconds for it to update the text on the PictureBox that it uses to display things. Also it doesn't show a cursor either.

What I'm thinking now is, how hard could it be to build a TextBox, except it wouldn't be in a control at all, it'd be an area on the form, maybe I could draw the text (as the user types) and move the blinking cursor around as they type? Anyone know of any articles for drawing the cursor carrot and moving it around?
 
I'd take a look at using P/Invoke. CreateCaret, SetCaretPos, DestroyCaret, ShowCaret, & HideCaret are all in user32.dll

This gets me a nice big fat caret in my textbox.

VB.NET:
Imports System.Runtime.InteropServices

Public Class Form1

    <DllImport("user32.dll")> _
    Private Shared Function CreateCaret(ByVal hWnd As IntPtr, ByVal hBitmap As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As Boolean
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function ShowCaret(ByVal hWnd As IntPtr) As Boolean
    End Function

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        CreateCaret(TextBox1.Handle, IntPtr.Zero, 8, TextBox1.Height)
        ShowCaret(TextBox1.Handle)
    End Sub
End Class
 
If you use GDI+ and draw the border and a small rectangle(caret) you can use a timer to make a caret-rectangle show or hide following the the last letter - using e.Graphics.MeasureString method to place this small rectangle. Just a thought.
 
I'll be looking into this, this week. I'll see what all I can come up with, it'll be the highlighting of text and positioning the cursor that'll be the hard part. Not to mention the cut, copy and paste operations.
 
Back
Top