sadegh
sadegh

Reputation: 1642

Transparent background in Windows Forms?

I would like to transparent red color form background of visual basic 6 program.

I use this code to make the background of the form transparent:

Option Explicit

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

Private Declare Function GetWindowLong Lib "user32" Alias _
    "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex 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 Declare Function SetLayeredWindowAttributes Lib "user32" _
    (ByVal hWnd As Long, ByVal crKey As Long, ByVal bDefaut As Byte, _
    ByVal dwFlags As Long) As Long

Private Sub Form_Load()
  Me.BackColor = RGB(254,0,0)
  Transparency Me.hWnd, Me.BackColor, 255
End Sub

Private Sub Transparency(ByVal hWnd As Long, ByVal lngTransparentColor As Long, _
ByVal bytTransparency As Byte)

    Dim lngwindowstyle As Long

    lngwindowstyle = GetWindowLong(hWnd, GWL_EXSTYLE)
    If (lngwindowstyle And WS_EX_LAYERED) <> WS_EX_LAYERED Then
        SetWindowLong hWnd, GWL_EXSTYLE, lngwindowstyle Or WS_EX_LAYERED
    End If

    SetLayeredWindowAttributes hWnd, lngTransparentColor, bytTransparency, _
    LWA_COLORKEY Or LWA_Defaut
End Sub

But as you can see in the picture the red noise remains :

transparent background vb6

How can I remove this noise?


I saved the picture with .png extension and use AlphaImageControl.ocx to show it.

The red noise is removed but a red line under form remains :

transparent background png

Upvotes: 2

Views: 428

Answers (1)

innuendomaximus
innuendomaximus

Reputation: 353

As the commenters already said, your "red" isn't always the same red. The line under the form remains, if you look closely you can see it: the red lines are fading from red to black. So even if your left pixel is 254,0,0 the next ones are not. I recommend using a blank/real transparent background, png offers you that :)

Upvotes: 2

Related Questions