Results 1 to 9 of 9

Thread: Transparent Text Box Control

  1. #1

    Thread Starter
    Addicted Member scotty85_2000's Avatar
    Join Date
    Feb 2001
    Posts
    198

    Transparent Text Box Control

    Hey,

    I've searched through the forums on this topic and I couldn't find anything of use, but I do apologise if this is something that has been discussed before.

    Basically, I would like to know where, if anywhere, I can get my hands on a transparent text box control, i.e. a text box with a transparent background and no borders but with opaque text.

    Has anyone ever come across such a thing?

    Thanking you in advance...
    100 posts... All of them have been "How Do I's", "Can You's", "HELP ME!!'s"...
    Still, at least they've been relevant..

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    well this might be as close as you will get

    http://www.planetsourcecode.com/vb/s...23090&lngWId=1

  3. #3
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560
    Here's one by Aaron Young. I tried to find the link to where I grabbed it from on this forum but coul'nd find it.
    I'm pretty sure I only added the txtEdit_KeyDown bit. Hope you don't mind me posting it Aaron?
    Cheers!

    VB Code:
    1. 'inside a module
    2. '>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<
    3. '         Example of Transparent Textbox
    4. '
    5. '       Written by Aaron Young, June 2001
    6. '
    7. '          Email: [email][email protected][/email]
    8. '>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<
    9. Option Explicit
    10.  
    11. Private Type RECT
    12.         Left As Long
    13.         Top As Long
    14.         Right As Long
    15.         Bottom As Long
    16. End Type
    17.  
    18. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    19. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    20. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    21. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    22. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    23. Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    24. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    25. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    26. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    27. Private Declare Function CreatePatternBrush Lib "gdi32" (ByVal hBitmap As Long) As Long
    28. Private Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
    29.  
    30. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    31.  
    32. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    33. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    34.  
    35. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    36. Private Declare Function UpdateWindow Lib "user32" (ByVal hwnd As Long) As Long
    37. Private Declare Function InvalidateRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT, ByVal bErase As Long) As Long
    38.  
    39. Private Const GWL_WNDPROC = (-4)
    40.  
    41. Private Const SW_HIDE = 0
    42. Private Const SW_SHOW = 5
    43.  
    44. Private Const WM_CTLCOLOREDIT = &H133
    45. Private Const WM_MOVE = &H3
    46. Private Const WM_COMMAND = &H111
    47.  
    48. Private Const TRANSPARENT = 1
    49. Private Const OPAQUE = 2
    50.  
    51. Private Const EM_GETRECT = &HB2
    52.  
    53. Private Const SRCCOPY = &HCC0020
    54.  
    55. Private lFormHwnd As Long
    56. Private oEdit As TextBox
    57. Private lBackGround As Long
    58. Private lBitmap As Long
    59. Private lOldObj As Long
    60. Private lWindowFunc As Long
    61. Private lBrush As Long
    62.  
    63. Public Sub MakeTransparent(ByVal hwnd As Long, ByRef oTextEdit As TextBox)
    64.     ' Subclass the WindowProc of the Form Window and
    65.     ' Get a Reference to the Textbox we want to make appear Transparent
    66.     lFormHwnd = hwnd
    67.     Set oEdit = oTextEdit
    68.     lWindowFunc = SetWindowLong(lFormHwnd, GWL_WNDPROC, AddressOf WindowCallBack)
    69. End Sub
    70.  
    71. Public Sub ReleaseTransparency()
    72.     ' Release the Subclass on the Forms WindowProc
    73.     ' And Release the reference to the Textbox
    74.     Call SetWindowLong(lFormHwnd, GWL_WNDPROC, lWindowFunc)
    75.     Set oEdit = Nothing
    76.     ' Clean up the Brush, Bitmap and HDC created for the Transparency effect
    77.     Call SelectObject(lBackGround, lOldObj)
    78.     Call DeleteObject(lBitmap)
    79.     Call DeleteObject(lBrush)
    80.     Call DeleteDC(lBackGround)
    81. End Sub
    82.  
    83. Public Sub GetBackGround(ByVal hwnd As Long)
    84.     ' Make the Textbox appear transparent, by painting it with the image
    85.     ' from the Desktop currently under the Textbox control.
    86.     Dim tWIN As RECT
    87.     Dim lObj As Long
    88.     Dim lDeskDC As Long
    89.     Dim bNew As Boolean
    90.    
    91.     ' Hide the Form containing the Textbox to expose the DeskTop underneath
    92.     ' To get rid of the "Focus" effect, consider moving the Form off screen instead
    93.     ' So that it retains focus.
    94.     Call ShowWindow(lFormHwnd, SW_HIDE)
    95.    
    96.     DoEvents
    97.    
    98.     ' Track whether we're doing this for the first time.
    99.     bNew = (lBitmap = 0)
    100.    
    101.     ' If it's not the First time, delete the old Brush, Bitmap and HDC
    102.     ' To preserve GDI resources
    103.     If Not bNew Then
    104.         Call DeleteObject(lBrush)
    105.         Call SelectObject(lBackGround, lOldObj)
    106.         Call DeleteObject(lBitmap)
    107.         Call DeleteDC(lBackGround)
    108.     End If
    109.    
    110.     ' Get the area occupied by the Textbox
    111.     Call GetWindowRect(oEdit.hwnd, tWIN)
    112.     With tWIN
    113.         ' Grap a DC for the Desktop
    114.         lDeskDC = GetDC(0)
    115.         ' Create a compatible Bitmap with the Desktop DC the size of the Textbox Area
    116.         lBitmap = CreateCompatibleBitmap(lDeskDC, .Right - .Left, .Bottom - .Top)
    117.         ' Create a compatible DC to store the current transparency image in.
    118.         lBackGround = CreateCompatibleDC(lDeskDC)
    119.         ' Select the Bitmap into the DC
    120.         lOldObj = SelectObject(lBackGround, lBitmap)
    121.         ' Now copy the currenty image from the Desktop to the Temp. DC
    122.         BitBlt lBackGround, 0, 0, .Right - .Left, .Bottom - .Top, lDeskDC, .Left, .Top, SRCCOPY
    123.     End With
    124.        
    125.     ' Create a Brush using the Bitmap to be used by the Textbox when repainting.
    126.     lBrush = CreatePatternBrush(lBitmap)
    127.    
    128.     ' Release the Desktop DC and Show the Form again.
    129.     Call ReleaseDC(0, lDeskDC)
    130.     Call ShowWindow(lFormHwnd, SW_SHOW)
    131. End Sub
    132.  
    133. Private Function WindowCallBack(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    134.     Dim tRECT As RECT
    135.    
    136.     ' Trap any unforeseen errors
    137.     On Error Resume Next
    138.    
    139.     Select Case Msg
    140.     Case WM_CTLCOLOREDIT
    141.     ' The WM_CTLCOLOREDIT is sent to the Form when the Editbox is about to be repainted
    142.     ' By Returning a Brush handle we can control what's used to paint the background
    143.     ' To make the Box appear transparent, pass a Bitmap of the area under the Form
    144.        
    145.         ' Set the Background Mode to Transparent so that the "TextOut" operations
    146.         ' Don't fill the spaces around the Text with a solid background color.
    147.         Call SetBkMode(wParam, TRANSPARENT)
    148.         WindowCallBack = lBrush
    149.    
    150.     Case WM_MOVE
    151.     ' Triggered when the Form has been moved, at this point we want to get a fresh
    152.     ' Snapshot of what's under the Form to maintain the illusion of Transparency.
    153.        
    154.         Call GetBackGround(lFormHwnd)
    155.         WindowCallBack = CallWindowProc(lWindowFunc, hwnd, Msg, wParam, lParam)
    156.    
    157.     Case WM_COMMAND
    158.     ' Triggered when the Form recieves a command for the Textbox, if you have more than
    159.     ' The Textbox on the Form, you'll want to single out your Textbox from the messages
    160.            
    161.         ' Call the default behaviour first in this case, so we have the opportunity
    162.         ' To clean up unwanted side-effects of this implementation.
    163.         WindowCallBack = CallWindowProc(lWindowFunc, hwnd, Msg, wParam, lParam)
    164.            
    165.         ' Part of the Problem with this method is the Textbox only updates the line it's on
    166.         ' This causes ugly graphical side effects when you backup over text or scroll down
    167.         ' The Textbox outside the visible area.
    168.         ' To get around this, I'm issuing a "Refresh" after each "WM_COMMAND" to update the
    169.         ' whole textbox area, this can give an annoying flicker, but like I said, it aint
    170.         ' Perfect, but it's a starting point.
    171.        
    172.         ' To make this nicer you could narrow down to the calls that cause the annoying
    173.         ' Glitches and only refresh in those instances.
    174.         Call SendMessage(lParam, EM_GETRECT, 0, tRECT)
    175.         Call InvalidateRect(lParam, tRECT, 1)
    176.         Call UpdateWindow(lParam)
    177.                
    178.     Case Else
    179.    
    180.         WindowCallBack = CallWindowProc(lWindowFunc, hwnd, Msg, wParam, lParam)
    181.    
    182.     End Select
    183.    
    184. End Function
    185.  
    186. ' Some basic Word manipulation functions, these can be used to extract
    187. ' The windows message sent with the "WM_COMMAND" message, see API Help
    188. ' Form detais.
    189. Public Function LoWord(ByVal lValue As Long) As Long
    190.     LoWord = lValue And &HFFFF&
    191. End Function
    192.  
    193. Public Function HiWord(ByVal lValue As Long) As Long
    194.     HiWord = (lValue / &H10000) And &HFFFF&
    195. End Function
    196.  
    197. Public Function MakeLong(ByVal lLo As Long, ByVal lHi As Long) As Long
    198.     MakeHiLoWord = (lHi * &H10000) + lLo
    199. End Function
    VB Code:
    1. 'on a form with a textbox named txtEdit
    2.  
    3. Private Sub Form_Initialize()
    4.     ' Get the initial background image
    5.     GetBackGround hwnd
    6. End Sub
    7. Private Sub Form_Load()
    8.     ' Make the Textbox appear transparent
    9.     MakeTransparent hwnd, txtEdit
    10.     txtEdit.Text = "hello how are you today?" & vbCrLf & "(press escape to exit)"
    11.     txtEdit.FontSize = 18
    12. End Sub
    13.  
    14. Private Sub Form_Resize()
    15.     ' Resize the Textbox to fill the whole form
    16.     ' and grab the desktop image under the form.
    17.     txtEdit.Move 0, 0, ScaleWidth, ScaleHeight
    18.     GetBackGround hwnd
    19. End Sub
    20.  
    21. Private Sub Form_Unload(Cancel As Integer)
    22.     ' Remove transparency...
    23.     ' IMPORTANT >> DO NOT CLOSE VIA THE STOP BUTTON IN THE IDE <<
    24.     ' DOING SO WILL CAUSE VB TO CRASH, THIS CODE NEEDS TO BE TRIGGERED FIRST
    25.     ' TO REMOVE THE SUBCLASSING.
    26.     ReleaseTransparency
    27. End Sub
    28.  
    29. Private Sub txtEdit_KeyDown(KeyCode As Integer, Shift As Integer)
    30.     Select Case KeyCode
    31.     Case vbKeyEscape
    32.     Unload Me
    33.     End Select
    34. End Sub
    Give your music collection a whole new life with PartyTime Jukebox

  4. #4
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Cool, this works nicely Well done Aaron, if anybody had trouble making it from the code above, i zipped an example up here.

    Thank Aaron though or the writer for the code
    Attached Files Attached Files

  5. #5
    Fanatic Member jian2587's Avatar
    Join Date
    Aug 2000
    Location
    I bet u need a fusion powered shuttle to reach my place...
    Posts
    963
    I thought there's something like Microsoft Forms 2.0 component.
    It has a transparent textbox control.
    ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
    Programming is fun, but only if you're not on a tight deadline
    So I consider all those working engineers sad people

    VB FTP class
    3 page PHP crash course
    Crash Course on DX9 Managed with VB.NET covering basics till terrain creation

  6. #6
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Yeah there is lol, thats the 3rd time you have mentioned about that control

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    you cant distribute that control with your applications. the end user would HAVE to have office installed to make use of it

  8. #8
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Is that the same for all controls under that license group?

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by Madboy
    Is that the same for all controls under that license group?
    it goes for the entire ms forms 2.0 component

Posting Permissions

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



Click Here to Expand Forum to Full Width