How to make a form transparent.

Posted on 2003-03-09
Medium Priority
Last Modified: 2010-04-07

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.


Question by:Naylin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8098221
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.

Expert Comment

ID: 8102376
Use .NET

Accepted Solution

snowbordingexe earned 75 total points
ID: 8103603
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.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8104005
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

Expert Comment

ID: 8900457
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?

Author Comment

ID: 8902855
I ended up finding an ocx that did the job better than I could've imagined.

Author Comment

ID: 8902902
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question