Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Transparency based on color

Is there a way to make a form and parts of controls on the form transparent based on a single color, like a green screen? Like if there is green on the form it would be transparent and if there was green in the control that would be transparent too?

I have a control that I want to put on the form and I want parts of the control to be transparent and all of the form to be transparent. The control has irregular shapes that change from time to time so a simple rectangle, etc. wouldn't work. I'm using VB6. Any help would be appreciated, but I'm a newbie so please be gentle.
0
nealparr
Asked:
nealparr
  • 4
  • 2
  • 2
  • +2
1 Solution
 
Shaka913Commented:
Simple code below... notice the <-- i put in to show you where to make a change to pick a color.  The way this works is create a form, put a picture on the canvas of the form, and point it to the color you want transparent, and viola it is transparent.

Good Luck


Public Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long
Public Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Public Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Public Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function ReleaseCapture Lib "user32" () As Long
Public Function MakeRegion(picSkin As Form) As Long
    hDC = picSkin.hDC
    PicWidth = picSkin.ScaleWidth
    PicHeight = picSkin.ScaleHeight
    InFirstRegion = True: InLine = False
       
    Transparent = GetPixel(hDC, 0, 0)  '<-- pick any pixel to make that color transparent.
    For Y = 0 To PicHeight - 1
        For X = 0 To PicWidth - 1
        If GetPixel(hDC, X, Y) = Transparent Or X = PicWidth Then
            If InLine Then
            InLine = False
            LineRegion = CreateRectRgn(LineX, Y, X, Y + 1)
                If InFirstRegion Then
                Full = LineRegion
                InFirstRegion = False
                Else
                CombineRgn Full, Full, LineRegion, 2
                End If
            End If
        Else
            If Not InLine Then
            InLine = True
            LineX = X
            End If
        End If
        Next
    Next
    MakeRegion = Full
End Function
0
 
nealparrAuthor Commented:
To Shaka913:

How would I call this function from the form and does the form or image need to be named anything special?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
inthedarkCommented:
It is possible to map a byte array to the memory used by the image of your control and also another array to the image of the underlying form.  You can scan the array (representing your control's image) and if you find a specifc pixel value merge the pixel from the form's image at the same position.  Becuase the arrays are directly mapped to the actual memory used for the images the process is very fast.

But if you want a quick and easy way you just need some code in your control's paint event.  Capture the image of the form with the control invisible and use paintpicture to place the form's image onto your user control.  Any other controls you have should just float to the top.
0
 
Shaka913Commented:
nealparr
Here is how I would call it.

This is from the load event on the form:

Private Sub Form_Load()
Dim WindowRegion As Long
With Me
    WindowRegion = MakeRegion(frmMainForm) '<-- use the name of the form that you call it...
    SetWindowRgn .hWnd, WindowRegion, True
End With
End Sub
' This allows you to move it after the transparency operation
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ReleaseCapture
    SendMessage Me.hWnd, &HA1, 2, 0&
End Sub
0
 
Koro_das_MasterCommented:
Declare Function SetLayeredWindowAttributes lib "msimg32" (byval hWnd as Long, byval crKey as Long, byval bAlpha as Long, byval dwFlags as Long) as Long

just call
SetLayeredWindowAttributes(Me.hWnd,RGB(your color here),0,LWA_COLORKEY)

The above sample will only work on Windows 2000 or later
And you'll have to lookup the value of LWA_COLORKEY in the SDK headers (for that I don't have them here)
0
 
Richie_SimonettiIT OperationsCommented:
Const LWA_COLORKEY=&H1
0
 
Richie_SimonettiIT OperationsCommented:
I have w2k and got an error in that function.
0
 
Koro_das_MasterCommented:
Oops, I forgot, you also need to set the WS_EX_LAYERED extended style to your window after calling SetLayeredWindowAttributes:

Declare Function SetWindowLong alias "SetWindowLongA" lib "user32" (ByVal hWnd as Long, byval nIndex as Long, byval dwNewLong as Long) as Long

Declare Function GetWindowLong alias "GetWindowLongA" lib "user32" (ByVal hWnd as Long, byval nIndex as Long) as Long

Const GWL_EXSTYLE=(-20)
Const WS_EX_LAYERED = ??? ' Lookup value in Platform SDK headers

SetWindowLong(Me.hWnd,GWL_EXSTYLE,GetWindowLong(Me.hWnd,GWL_EXSTYLE) Or WS_EX_LAYERED)

And if you call SetLayeredWindowAttributes again after setting the exstyle, you will need to clear WS_EX_LAYERED and set it again to have the settings applied.
0
 
Richie_SimonettiIT OperationsCommented:
The problem is with library also. That function is in user32 not msimg32.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now