Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transparent Form Background 1

Status
Not open for further replies.

AJLoehr

Programmer
Oct 3, 2008
26
US
Ok, first off... I know this topic has been discussed probably more than any other topic EVER. But I still can't find any code to work.

I'm using Windows XP (at work we use Vista mainly and some XP). I'm building the program in Access 2007.

Here is the code I found... sorry no references, because I know the site I got it from didn't have references and the poster said he didn't know where it came from. (Thanks to someone out there).

By the way, the code goes directly into the forms code not a separate module.

Code:
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 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

This code doesn't give me any error... the problem is the background becomes white... not transparent. And I know for 100% sure that the .png has a transparent background.

What's even weirder is that the top left/right corners to the form are rounded, but the bottom ones are angled. I don't get that either. Anyway... any help would be greatly appreciated.
 
Have you tried a gif with a transparent background?

Boyd Trimmell aka HiTechCoach
Microsoft Access MVP
 
No I haven't tried a .gif. I thought if a .png wouldn't work... neither would a .gif or .bmp. But I guess that (although it may be right) is a stupid way of looking at it. I'll try some more file types before giving up on the idea. Thanks for the reminder.
 
This seems similar, and works for me. It is probably form dependant

Code:
Public Const RGN_AND = 1
Public Const RGN_COPY = 5
Public Const RGN_DIFF = 4
Public Const RGN_OR = 2
Public Const RGN_XOR = 3

Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) 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 CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Public Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Public Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long

Public Type POINTAPI
    tLng_Xloc As Long
    tLng_YLoc As Long
End Type

Public Type RECT
    tLng_Left As Long
    tLng_Top As Long
    tLng_Right As Long
    tLng_Bottom As Long
End Type

Public Sub MakeTransparent(rFrm_TheForm As Access.Form)

Dim lCtl_Control As Control
Dim lRct_WinFrame As RECT
Dim lRct_ClientArea As RECT
Dim lRct_ControlArea As RECT
Dim lPnt_TopLeft As POINTAPI
Dim lPnt_BottRight As POINTAPI
Dim lLng_CntlHandle As Long
Dim lLng_ClientHandle As Long
Dim lLng_FrameHandle As Long

GetWindowRect rFrm_TheForm.hwnd, lRct_WinFrame
GetClientRect rFrm_TheForm.hwnd, lRct_ClientArea

lPnt_TopLeft.tLng_Xloc = lRct_WinFrame.tLng_Left
lPnt_TopLeft.tLng_YLoc = lRct_WinFrame.tLng_Top
lPnt_BottRight.tLng_Xloc = lRct_WinFrame.tLng_Right
lPnt_BottRight.tLng_YLoc = lRct_WinFrame.tLng_Bottom

ScreenToClient rFrm_TheForm.hwnd, lPnt_TopLeft
ScreenToClient rFrm_TheForm.hwnd, lPnt_BottRight

With lRct_WinFrame
    .tLng_Left = lPnt_TopLeft.tLng_Xloc
    .tLng_Top = lPnt_TopLeft.tLng_YLoc
    .tLng_Right = lPnt_BottRight.tLng_Xloc
    .tLng_Bottom = lPnt_BottRight.tLng_YLoc
End With

With lRct_ClientArea
    .tLng_Left = Abs(lRct_WinFrame.tLng_Left)
    .tLng_Top = Abs(lRct_WinFrame.tLng_Top)
    .tLng_Right = .tLng_Right + Abs(lRct_WinFrame.tLng_Left)
    .tLng_Bottom = .tLng_Bottom + Abs(lRct_WinFrame.tLng_Top)
End With

With lRct_WinFrame
    .tLng_Right = .tLng_Right + Abs(.tLng_Left)
    .tLng_Bottom = .tLng_Bottom + Abs(.tLng_Top)
    .tLng_Top = 0
    .tLng_Left = 0
End With

With lRct_ClientArea
    lLng_ClientHandle = CreateRectRgn(.tLng_Left, .tLng_Top, .tLng_Right, .tLng_Bottom)
End With

With lRct_WinFrame
    lLng_FrameHandle = CreateRectRgn(.tLng_Left, .tLng_Top, .tLng_Right, .tLng_Bottom)
End With

CombineRgn lLng_FrameHandle, lLng_ClientHandle, lLng_FrameHandle, RGN_XOR

For Each lCtl_Control In rFrm_TheForm.Controls
    lRct_ControlArea.tLng_Left = lCtl_Control.Left \ 15
    lRct_ControlArea.tLng_Top = lCtl_Control.Top \ 15
    lRct_ControlArea.tLng_Right = (lCtl_Control.Left + lCtl_Control.Width - 15) \ 15
    lRct_ControlArea.tLng_Bottom = (lCtl_Control.Top + lCtl_Control.Height - 15) \ 15
    
    With lRct_ControlArea
        .tLng_Left = .tLng_Left + lRct_ClientArea.tLng_Left
        .tLng_Top = .tLng_Top + lRct_ClientArea.tLng_Top
        .tLng_Right = .tLng_Right + lRct_ClientArea.tLng_Left
        .tLng_Bottom = .tLng_Bottom + lRct_ClientArea.tLng_Top
        lLng_CntlHandle = CreateRectRgn(.tLng_Left, .tLng_Top, .tLng_Right, .tLng_Bottom)
    End With
    
    CombineRgn lLng_FrameHandle, lLng_CntlHandle, lLng_FrameHandle, RGN_OR
Next lCtl_Control

SetWindowRgn rFrm_TheForm.hwnd, lLng_FrameHandle, True

End Sub
also look at
 
Appreciate the attempt MajP... however it didn't work for me. My images with transparent backgrounds still showed as white. And even a Textbox I have .visible = false showed up white. That's weird. There were areas that were transparent though, anywhere that didn't have an object of anykind became transparent... so at least it's a step in the right direction. If you have any other ideas, please post them. I'm going to try the transparent .gif suggestion, since my files are .png. Just to see if it makes a difference. I'll post once I've tried it.
 
Just tried Transparent .gif.... Didn't work either =(
 
I have read and posted thousands of threads and have only seen this asked once. Have you seen other posts on this? You make it sound as if this is something popular to do. What exactly is the affect you want and why? Maybe there is another approach?
 
AJLoehr,

You said:
Ok, first off... I know this topic has been discussed probably more than any other topic EVER.

Not sure were you have seen all these posts. Did any of these many posts related to Access or Access 2007?

Whould you mind sharing a few links?

I agree with MajP that in my experience this s not very common with Access. I have rarely seen this ask. I would have to say it is more like one of the least discussed topics ever with regards to Access.

I have read over 40,000 unique Access related threads and have posted over 20,000 replies across multiple forums in the past 7+ years.

I have found that with Access 2007 there are things that change the way forms are displayed that worked in previous versions that no longer work in 2007. I am thnking there must be some change to the way Access 2007 displays stuff that do not allow some of these techniques to work.

I am curious to see if they work in 2010.

I will do some testing in Access 2003, 2007, and 2010 with your code.

Would you mind attaching a image file that you are trying use so I can test with it?


Boyd Trimmell aka HiTechCoach
Microsoft Access MVP
 
Boyd,
Here is an image with the "categories" form and the same form transparent over top of another form. It is pretty cool, but doubt I would ever have a need/desire to do this.

wgopkx.jpg



30blkj5.jpg
 
MajP,

If your form had a background image then it would be useful. For example as a splash screen form or a login form.

In the example your transparent form does not appear have a background image. Where is the form's background image?


Are you using 2007 or 2010?

Boyd Trimmell aka HiTechCoach
Microsoft Access MVP
 
Boyd,
Not sure what you are asking. It is obvious that the supplied code will not work on a background image, clearly the API code is making rectangular regions around all controls and form area. So not sure what a background image would do. I am on 2007 but I can not imagine it would handle the API differently.
So are you suggesting there is a way to do this with? If I use a regular image control it appears to float, as would be expected. Still not the desired affect because of the control itself. If you can do this a different method using a background image I would be curious as well.
 
MajP,

I'm not sure if this has been resolved since October but after experimenting with your code and Lebans code I've discovered what the issue if that AJLoehr has.

Your code makes those parts of the form's background where there are no objects transparent. But if the form contains an image which has transparent sections, you get the form background there; in other words, if you have a rectangular image object you'll still see a rectangular image.

I've tried different graphic formats and I still get the same result.

Migs01
 
Yes, that is what I was saying in the above post.
 
MajP (& AJLoehr),

I've done some more digging around in the internet and I found this link:
The code works for both the form backgound and transparent sections of images in the form. You may see a little hint of cyan around the edges of the image, but it's as good as I've got.

But if using Access 2010, I changed line 20 to read: Me.Detail.BackColor = vbCyan

Migs01

 
Thank you very much...

Sorry it took so long to reply. Been working on many different projects.

I did test the code from the link from Migs01. It worked great.

~A.J.
 
>and I found this link

Should have looked closer to home ;-) ... we've covered transparency in forms quite comprehensively and repeatedly over in the VB5/VB6 forum over the years - e.g. thread222-1034117
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top