• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

How to make a form transparent.


Does anyone know how to make the background of a form transparent?

I have a program that I don't want to have to copy all the info from the main form onto subsequent forms but have several other forms that pop-up for other stuff. It would be nice if I could just make the form transparent so the main form's background shows through. The only other option I could think of myself would be to cut out the section of the main form's BG and set it to the BG of the subsequent forms. If I have to do this, does someone know how to load the picture once and then just use it for each form so it doesn't have to take up memory for each form?

BTW I can't use MDI unless someone knows how to get rid of the title bar and line around the forms.


1 Solution
Try this:

Private Declare Function CreateRectRgn Lib _
    "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, _
    ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CombineRgn Lib _
    "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, _
    ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function SetWindowRgn Lib _
    "user32" (ByVal hWnd As Long, ByVal hRgn As Long, _
    ByVal bRedraw As Boolean) As Long
Private Declare Function DeleteObject Lib _
    "gdi32" (ByVal hObject As Long) As Long

' Constants used by the CombineRgn function
Private Const RGN_AND = 1
Private Const RGN_OR = 2
Private Const RGN_XOR = 3
Private Const RGN_DIFF = 4
Private Const RGN_COPY = 5

Private Sub cmdExit_Click()
    Unload Me
End Sub

Private Sub Form_Activate()
    Dim rgnForm As Long, rgnCombined As Long
    Dim rgnControl As Long, x As Long
    Dim formWidth As Single, formHeight As Single
    Dim borderWidth As Single, titleHeight As Single
    Dim ctlLeft As Single, ctlTop As Single
    Dim ctlWidth As Single, ctlHeight As Single
    Dim ctl As Control

    ' Calculate the form area
    borderWidth = (Me.Width - Me.ScaleWidth) / 2
    titleHeight = Me.Height - Me.ScaleHeight - borderWidth
    ' Convert to Pixels
    borderWidth = ScaleX(borderWidth, vbTwips, vbPixels)
    titleHeight = ScaleY(titleHeight, vbTwips, vbPixels)
    formWidth = ScaleX(Me.Width, vbTwips, vbPixels)
    formHeight = ScaleY(Me.Height, vbTwips, vbPixels)
    ' Create a region for the whole form
    rgnForm = CreateRectRgn(0, 0, formWidth, formHeight)
    rgnCombined = CreateRectRgn(0, 0, 0, 0)
    ' Make the graphical area transparent by combining the two regions
    x = CombineRgn(rgnCombined, rgnForm, rgnForm, RGN_DIFF)

    ' Make the controls visible
    For Each ctl In Controls
        ' Make the regions of controls whose container is the form visible
        If TypeOf ctl.Container Is Form Then
            ctlLeft = ScaleX(ctl.Left, vbTwips, vbPixels) + borderWidth
            ctlTop = ScaleX(ctl.Top, vbTwips, vbPixels) + titleHeight
            ctlWidth = ScaleX(ctl.Width, vbTwips, vbPixels) + ctlLeft
            ctlHeight = ScaleX(ctl.Height, vbTwips, vbPixels) + ctlTop
            rgnControl = CreateRectRgn(ctlLeft, ctlTop, ctlWidth, ctlHeight)
            x = CombineRgn(rgnCombined, rgnCombined, rgnControl, RGN_OR)
        End If
    Next ctl

    ' Set the clipping area of the window using the resulting region
    SetWindowRgn hWnd, rgnCombined, True
    ' Tidy up
    x = DeleteObject(rgnCombined)
    x = DeleteObject(rgnControl)
    x = DeleteObject(rgnForm)
End Sub

'hope it helps.
Use .NET
Hello Naylin

Download this control.  Dont worry its free

The set the window state of the form to Maximized
Then the Border style of the window to None

The put the controls in the center of the form

then Use this control  I set the Name propertie to Capture

put this code into the Form_Load() event of your form

Me.Picture = Capture.CaptureFullScreen

and that should do it.

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.

Is this any good

Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_TRANSPARENT = &H20&
Private Const SWP_FRAMECHANGED = &H20
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const HWND_NOTOPMOST = -2

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 SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Sub Form_Load()
   SetWindowPos Me.hwnd, HWND_NOTOPMOST, 0&, 0&, 0&, 0&, SWP_SHOWME
End Sub
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
NaylinAuthor Commented:
I ended up finding an ocx that did the job better than I could've imagined.
NaylinAuthor Commented:
I didn't use the control that snowbordingexe suggested, but as s/he was the closest to what I did, I'll award the points there.


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now