Tranparent color...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Tranparent color...

  1. #1
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    72

    Unhappy Tranparent color...

    I found a way to make a color transparent for any controls in a form. The problem is that you can see through the form and click through it. I want to know how to make a color transparent in a control so that I can see what's behind the control in the form. I have searched A LOT on google and nothing. I would really love to know how to do this.

    Thanks,

    David Richard

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: Tranparent color...

    Here's one way:

    Code:
    Option Explicit
    
    Dim DoIt As Boolean
    
    Private Const BDR_SUNKENOUTER = &H2
    Private Const BDR_RAISEDINNER = &H4
    Private Const DT_EDITCONTROL = &H2000&
    Private Const EDGE_ETCHED = (BDR_SUNKENOUTER Or BDR_RAISEDINNER)
    Private Const BF_BOTTOM = &H8
    Private Const BF_LEFT = &H1
    Private Const BF_RIGHT = &H4
    Private Const BF_TOP = &H2
    Private Const BF_RECT = (BF_LEFT Or BF_TOP Or BF_RIGHT Or BF_BOTTOM)
    Private Const DT_LEFT = &H0
    Private Const DT_TOP = &H0
    
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Private Declare Function BitBlt Lib "gdi32" _
        (ByVal hDCDest As Long, ByVal XDest As Long, ByVal YDest As Long, _
         ByVal nWidth As Long, ByVal nHeight As Long, ByVal hDCSrc As Long, _
         ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    
    Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
    
    Private Declare Function DrawText Lib "user32" Alias "DrawTextA" _
        (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, _
         lpRect As RECT, ByVal wFormat As Long) As Long
    
    Private Declare Function DrawCaption Lib "user32" _
        (ByVal hWnd As Long, ByVal hdc As Long, pcRect As RECT, ByVal un As Long) As Long
    
    Private Declare Function DrawEdge Lib "user32" _
        (ByVal hdc As Long, qrc As RECT, ByVal edge As Long, ByVal grfFlags As Long) As Long
    
    Private Declare Function SetRect Lib "user32" _
        (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    
    Sub TextTrans(MyTB As Object)
    '==============================
    Dim TempDC As Long
    Dim Temp As String
    Dim MyLoc As RECT
    
    On Error Resume Next
    
        Temp = MyTB.Text
        MyLoc.Left = MyTB.Left
        MyLoc.Top = MyTB.Top
        MyLoc.Right = MyLoc.Left + MyTB.Width
        MyLoc.Bottom = MyLoc.Top + MyTB.Height
        'MyTB.Parent.Cls
        MyTB.Parent.ForeColor = MyTB.ForeColor
        Set MyTB.Parent.Font = MyTB.Font
        DrawText MyTB.Parent.hdc, Temp, Len(Temp), MyLoc, DT_EDITCONTROL
        TempDC = GetDC(MyTB.hWnd)
        BitBlt TempDC, 0, 0, MyTB.Width, MyTB.Height, MyTB.Parent.hdc, MyTB.Left, MyTB.Top, vbSrcCopy
    
    End Sub
    
    Private Sub cmdMakeTransparent_Click()
        TextTrans txtInfo
        TextTrans RichTextBox1
        TextTrans Picture1
        
        Dim MyRect As RECT
        Dim X1&, Y1&, X2&, Y2&
        Dim strText As String
        
        Me.Cls
        Me.ScaleMode = vbPixels
        X1 = Frame1.Left - 2
        Y1 = Frame1.Top - 2
        X2 = X1 + Frame1.Width + 4
        Y2 = Y1 + Frame1.Height + 4
        SetRect MyRect, X1, Y1, X2, Y2
        'Me.ScaleMode = vbTwips
        SetRect MyRect, X1, Y1, X2, Y2
        DrawEdge Me.hdc, MyRect, EDGE_ETCHED, BF_RECT
        
        strText = Frame1.Caption
        SetRect MyRect, X1 + 10, Y1 - 5, X2, Y2
        DrawText Me.hdc, strText, Len(strText), MyRect, DT_LEFT Or DT_TOP
        
        TextTrans Frame1
        
        TextTrans List1
    
    End Sub
    
    Private Sub Form_Activate()
        txtInfo.SetFocus
    End Sub
    
    Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
        Picture1.Move X, Y
        TextTrans Picture1
    End Sub
    
    
    Private Sub Form_Load()
        'cmdMakeTransparent_Click
    End Sub
    
    
    Private Sub Frame1_Click()
        MsgBox "hi"
    End Sub
    
    Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Picture1.Drag vbBeginDrag
    End Sub
    
    
    Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Picture1.Drag vbEndDrag
    End Sub
    
    
    Private Sub txtInfo_Change()
        If Not DoIt Then Exit Sub
        TextTrans txtInfo
    End Sub
    
    
    Private Sub txtInfo_KeyPress(KeyAscii As Integer)
        If Not DoIt Then Exit Sub
        TextTrans txtInfo
    End Sub
    
    Private Sub txtInfo_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Not DoIt Then Exit Sub
        TextTrans txtInfo
    End Sub
    The other is Alpha Blending, which is probably better. Search for it
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    72

    Cool Re: Tranparent color...

    OK, maybe I was not clear enough. Here is what I want to to. I want to put a picture box over a text box and set a specific background color to the PictureBox which will be transparent. That means, if I apply a picture to the picture box, I will see the image but not the picture box's backcolor. I will see the texte box behind too or what ever other controls that could be hidden behind.

    Here is the code I found to make a color totally tranparent but that is NOT what I want. I want to see the contents of the form behind the control NOT through the form itself.

    Code:
    Private Declare Function SetWindowLongptr Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    
    Private Const GWL_EXSTYLE = (-20) ' Transparent color
    Private Const GWL_STYLE = (-16) ' Expandable borders
    Private Const WS_EX_LAYERED = &H80000
    Private Const WS_EX_TRANSPARENT = &H20
    Private Const WS_EX_APPWINDOW = &H40000
    Private Const LWA_ALPHA = &H2
    Private Const LWA_COLORKEY = &H1
    Private Const COL_TRANS = &HFF     ' Transparent Red
    
    Private Sub Form_Load()
    
      Dim Alpha As Byte
      Dim MaskColour As Long
    
      Alpha = 200
      MaskColour = COL_TRANS
      
      ' Whatever control you want to make the backcolor transparent
      ' Set the backcolor to the transparent color
      Picture1.BackColor = MaskColour
      Option1.BackColor = MaskColour
      Check1.BackColor = MaskColour
      Me.BackColor = MaskColour
      Text1.BackColor = MaskColour
      Command1.BackColor = MaskColour
      
      ' Apply Transparency
      SetWindowLongptr Me.hwnd, GWL_EXSTYLE, WS_EX_LAYERED Or WS_EX_APPWINDOW
      SetLayeredWindowAttributes Me.hwnd, MaskColour, Alpha, LWA_COLORKEY ' Or LWA_ALPHA
      
    End Sub
    I want the exact same effect but I want to see the form's contents instead of seeing through it!

    Thanks!
    David Richard

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Tranparent color...

    Are you using Me.hWnd? If so, then the effect will be applied to the form. Have you tried applying it only to a control on the form?
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  5. #5
    Join Date
    Apr 2009
    Posts
    394

    Re: Tranparent color...

    Code:
      ' Apply Transparency
      SetWindowLongptr Me.hwnd, GWL_EXSTYLE, WS_EX_LAYERED Or WS_EX_APPWINDOW
      SetLayeredWindowAttributes Me.hwnd, MaskColour, Alpha, LWA_COLORKEY ' Or LWA_ALPHA
    Code:
      ' Apply Transparency
      SetWindowLongptr Picture1.hwnd, GWL_EXSTYLE, WS_EX_LAYERED Or WS_EX_APPWINDOW
      SetLayeredWindowAttributes Picture1.hwnd, MaskColour, Alpha, LWA_COLORKEY ' Or LWA_ALPHA
    Good Luck

  6. #6
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    72

    Unhappy Re: Tranparent color...

    Are you using Me.hWnd? If so, then the effect will be applied to the form. Have you tried applying it only to a control on the form?
    It does not work! It would have been really fun and simple but sorry!
    David Richard

  7. #7
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: Tranparent color...

    What Operating System are you using?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    72

    Post Re: Tranparent color...

    Windows XP and Vista.
    David Richard

  9. #9
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: Tranparent color...

    Then, you can do Alpha Blending. Here's a module from a pal

    Code:
    '---------------------------------------------------------------------------------------
    ' Form AlphaBlendingDemo.FMain
    '  [Penagate]
    ' 19/04/2005
    '---------------------------------------------------------------------------------------
    
    Option Base 0
    Option Compare Text
    Option Explicit
    
    
    Private Sub Form_Load()
        Dim lngStyle        As Long
        Dim udtOSVer        As OSVERSIONINFO
    
        ' Get the OS version
        With udtOSVer
            .dwOSVersionInfoSize = Len(udtOSVer)
    
            GetVersionEx udtOSVer
    
            If .dwMajorVersion < 5 Then
                MsgBox "Sorry, alpha blending is only supported on Windows 2000 or higher.", vbCritical + vbOKOnly
    
                Unload Me
                Exit Sub
            End If
        End With
        
        ' Get the current window flags
        lngStyle = GetWindowLong(Me.hWnd, GWL_EXSTYLE)
    
        ' Update them to include the Layered flag
        SetWindowLong Me.hWnd, GWL_EXSTYLE, lngStyle Or WS_EX_LAYERED
        SetLayeredWindowAttributes Me.hWnd, 0&, 255, LWA_ALPHA
    
        Me.Refresh
    End Sub
    
    
    Private Sub sldAlpha_Change()
        Dim bAlpha          As Byte
        
        bAlpha = CByte(sldAlpha.Value)
        
        ' Set the new alpha level
        SetLayeredWindowAttributes Me.hWnd, 0&, bAlpha, LWA_ALPHA
        
        Me.Refresh
    End Sub
    Attached Files Attached Files
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    72

    Red face Re: Tranparent color...

    I dont want the complete form to be alpha blended, I know how to do that. I want a picture box's Background to be totally transparent to see any controls hidden behind it. I still want to see the borders and the picture if I added one. Here is what I want.
    Last edited by avidichard; June 1st, 2009 at 09:45 AM.
    David Richard

  11. #11
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    72

    Post Re: Tranparent color...

    Sorry, here are the screenshots

    Presently:


    Effect wanted:
    Attached Images Attached Images   
    David Richard

  12. #12
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,723

    Re: Tranparent color...

    Take a look at Davids AlphaBlending example.
    There are several calls to perform the blending
    Code:
        lngStyle = GetWindowLong(Me.hWnd, GWL_EXSTYLE)
    
        ' Update them to include the Layered flag
        SetWindowLong Me.hWnd, GWL_EXSTYLE, lngStyle Or WS_EX_LAYERED
        SetLayeredWindowAttributes Me.hWnd, 0&, 255, LWA_ALPHA
    ' and
        SetLayeredWindowAttributes Me.hWnd, 0&, bAlpha, LWA_ALPHA
    Try to replace Me.hWnd in the above calls with the window handle of your PictureBox PictureBox1.hWnd
    I'd imagine that should work on any control having a hWnd.

  13. #13
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    72

    Exclamation Re: Tranparent color...

    As I said above, It does not work when changing Me.hWnd to Picture1.hWnd. See attachment.

    And besides, you will see that when you alpha blend a form, the whole contents of the form alpha blends itself, even the title bar. What I want to be transparent is the background only, not the whole container and its contents. See the example code I posted a bit earlier in this thread. It makes any color of the form totally transparent, that is the effect I want but I want to see behind the PictureBox not through the form, see images above fo example. I've tried with the Picture1.hWnd methode but it does not work.
    Attached Files Attached Files
    David Richard

  14. #14
    Join Date
    Apr 2009
    Posts
    394

    Re: Tranparent color...


  15. #15
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    72

    Arrow Re: Tranparent color...

    Nothing to do with that, still the same examples with a semi or completely transparent form. I am not searching for a user control with a transparent background, I want to do what I mentionned above! I want to put a color on a form totally transparent. See pictures attached, it is self explanatory.
    David Richard

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center