Results 1 to 9 of 9

Thread: TextBox Transparent

  1. #1
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,364
    Reputation
    977

    TextBox Transparent

    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.
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  2. #2
    daniness's Avatar
    daniness is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0
    Join Date
    Feb 2010
    Posts
    49
    Reputation
    60

    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

  3. #3
    Solitaire is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    New York
    Posts
    460
    Reputation
    180
    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.

  4. #4
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    588
    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.

    Code:
    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 by MattP; 06-17-2010 at 6:09 PM.

  5. #5
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,364
    Reputation
    977
    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?
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  6. #6
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    588
    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.

    Code:
    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

  7. #7
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    267
    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.
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  8. #8
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,364
    Reputation
    977
    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.
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  9. #9
    Conejo is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.5
    Join Date
    Jul 2013
    Location
    USA
    Posts
    65
    Reputation
    19
    this tutorial shows how to make transparent textbox controls in vb Transparent textbox + richtextbox Controls

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking