Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42441

[VB2010] - usercontrol transparent

$
0
0
i have tested, and the backcolor=transparent isn't real transparent(but copy the parent image):(
can i clean complety the usercontrol(then i draw what i need, inclued a transparent bitmap)?
i have these code:
in module:
Code:

Module mdlTransparencyOpacy
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
    Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As IntPtr, ByVal crKey As Integer, ByVal bDefaut As Byte, ByVal dwFlags As Integer) As Integer

    Private Const GWL_EXSTYLE As Long = (-20)
    Private Const LWA_COLORKEY As Long = &H1
    Private Const LWA_Defaut As Long = &H2
    Private Const WS_EX_LAYERED As Long = &H80000

    '   
    Public Function Transparency(ByVal hWnd As IntPtr, Optional ByVal Col As Integer = -1, _
        Optional ByVal PcTransp As Byte = 255, Optional ByVal TrMode As Boolean = True) As Boolean
        ' Return : True if there is no error.   
        ' hWnd  : hWnd of the window to make transparent   
        ' Col : Color to make transparent if TrMode=False   
        ' PcTransp  : 0 à 255 >> 0 = transparent  -:- 255 = Opaque   
        Dim DisplayStyle As Integer
        Dim VoirStyle As Integer

        VoirStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
        If DisplayStyle <> (DisplayStyle Or WS_EX_LAYERED) Then
            DisplayStyle = (DisplayStyle Or WS_EX_LAYERED)
            Call SetWindowLong(hWnd, GWL_EXSTYLE, DisplayStyle)
        End If
        Transparency = (SetLayeredWindowAttributes(hWnd, Col, PcTransp, IIf(TrMode, LWA_COLORKEY Or LWA_Defaut, LWA_COLORKEY)) <> 0)


    End Function

    Public Sub ActiveTransparency(ByVal M As IntPtr, ByVal d As Boolean, ByVal F As Boolean, ByVal T_Transparency As Integer, Optional ByVal Color As Integer = -1)
        Dim B As Boolean
        If d And F Then
            'Makes color (here the background color of the shape) transparent   
            'upon value of T_Transparency   
            B = Transparency(M, Color, T_Transparency, False)
        ElseIf d Then
            'Makes form, including all components, transparent   
            'upon value of T_Transparency   
            B = Transparency(M, 0, T_Transparency, True)
        Else
            'Restores the form opaque.   
            B = Transparency(M, , 255, True)
        End If
    End Sub
End Module

and in usercontrol:
Code:

Public Property Transparent As Boolean

        Get
            Return blnTransparent
        End Get
        Set(ByVal value As Boolean)
            blnTransparent = value
            'Me.UpdateStyles()
            'Me.Update()
            If blnTransparent = True Then
                Transparency(ColorTranslator.ToWin32(Me.BackColor), 255, True)
            Else
                Transparency(ColorTranslator.ToWin32(Me.BackColor), 0, True)
            End If
        End Set
    End Property


 'Draw it's image
    Protected Overrides Sub OnPaintBackground(ByVal pevent As PaintEventArgs)
        'do not allow the background to be painted
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        If Me.BackgroundImage Is Nothing Then Exit Sub
        If Transparent = False Then
            MyBase.OnPaintBackground(e)
            Transparency(ColorTranslator.ToWin32(Me.BackColor), 0, True)
        Else
            MyBase.OnPaintBackground(e)
            Transparency(ColorTranslator.ToWin32(Me.BackColor), 255, True)
        End If
    End Sub

but stills copy the parent image:(

Viewing all articles
Browse latest Browse all 42441

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>