Results 1 to 3 of 3

Thread: transparent picture boxes (API?)

  1. #1


    i have 2 identical picture boxes one directly on top of the other, named Upper and Lower.

    can somebody please tell me a way to make the Upper picturebox transparent, so that the Lower picture shows through. the reason for this is so i can have a circle moving around on the Upper and drawing a line behind it on the Lower, i dont want the circle to "damage" the image on Lower.

    I know there is an API thingy for this, but i dont have a clue how to use API stuff.

    (analogy...) the circle looks like it is on a overhead projector overlay.

    I would be MEGA thankful!


    Email me:

    [This message has been edited by wossname (edited 12-12-1999).]

  2. #2
    Lively Member
    Join Date
    Nov 1999


    in a .bas module put this code:

    Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
    Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
    Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long

    Sub PicLoad(pic As PictureBox)

    Dim maskcolor As Long 'declare variable for the transparent color

    'set transparent color as great pink
    maskcolor = RGB(255, 0, 255)

    'perform function to make pictures transparent
    TransBack 0, 0, pic.Width / 15, pic.Height / 15, maskcolor, pic.hdc, pic.hwnd

    End Sub

    Private Sub TransBack(ByVal xstart As Long, ByVal ystart As Long, ByVal xend As Long, ByVal yend As Long, ByVal bgcolor As Long, ByVal thdc As Long, ByVal thWnd As Long)

    'declare region variables
    Dim rgn2 As Long, rgn3 As Long, rgn4 As Long

    'declare substitute variables for the function parameters
    Dim Top As Long, Left As Long, Right As Long, Bottom As Long, temptop As Long

    'create some region buffers
    rgn = CreateRectRgn(0, 0, 0, 0)
    rgn2 = CreateRectRgn(0, 0, 0, 0)
    rgn3 = CreateRectRgn(0, 0, 0, 0)

    'this loop picks out the transparent colors,
    'there MUST be three loops or Windows has a hard
    'time handling the complex regions

    Left = xstart
    Right = (xend - xstart) + 1: Bottom = (yend - ystart) + 1

    Do While Left < Right 'go through and scan left to right
    Top = ystart
    Do While Top < Bottom 'go through and scan top to botom
    If GetPixel(thdc, Left, Top) <> bgcolor Then
    temptop = Top
    Do While GetPixel(thdc, Left, Top + 1) <> bgcolor
    Top = Top + 1
    If Top = Bottom Then Exit Do
    rgn4 = CreateRectRgn(Left, temptop, Left + 1, Top + 1)
    CombineRgn rgn3, rgn2, rgn2, 5
    CombineRgn rgn2, rgn4, rgn3, 2
    DeleteObject rgn4
    End If
    Top = Top + 1
    CombineRgn rgn3, rgn, rgn, 5
    CombineRgn rgn, rgn2, rgn3, 2
    Left = Left + 1

    DeleteObject rgn2
    SetWindowRgn thWnd, rgn, True

    End Sub
    then in your form put this code:


    (picture1 = name of your picturebox)

    hope this helps. happy programming!!


  3. #3
    New Member
    Join Date
    Jan 2012

    Re: transparent picture boxes (API?)

    Thank you funkheads, this is what I was looking for too.

    However, I couldn't get it to work and there are also bits of code I couldn't understand. So I persevered and with through trial and error I got it to work.

    I use VB6 and here are the changes I made:
    Changed each occurrence of &lt;&gt; TO <> (not equal to)

    Changed each occurrence of &lt; TO < (less than)

    Changed picload(picture1) TO picload picture1 (removed brackets)

    Moved Top = ystart TO line BEFORE previous line (Do While Left &lt; Right 'go through and scan left to right)

    Hope this helps

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