Can a form have a transparent background (1 Viewer)

darbid

Registered User.
Local time
Today, 07:52
Joined
Jun 26, 2008
Messages
1,428
You may find this a little strange but I have a great brushedmetal.png file with rounded edges, dropshadow bla bla.

I was thinking that if I could embed this into a form with a transparent background then I would have a fantastic looking form. :cool:

It seems as soon as I insert a form the backgound or detail section goes to black and you cannot change the colour.

I am using Access 2003.

Anyone :)
 

darbid

Registered User.
Local time
Today, 07:52
Joined
Jun 26, 2008
Messages
1,428
maybe this thread might fit the VBA section more mods?

I have found out that by using WIN 32 API you can modify the transparency of a standard Access form.

http://blogs.msdn.com/access/archive/2008/04/28/modal-dialogs-with-transparent-backgrounds.aspx

This effect is like if you have VISTA and you are required to confirm your choice...... UAC stuff. The whole sceen dims a little and in the middle of the screen you have one bright window.

Back to Access - the command to do this (which changes the opacity of the form) is (other than the module stuff.....0 would be transperant. :)

SetFormOpacity Me, 0.7

I am wondering if we can use this to change the opacity of Me.Detail.BackColor?
 
Local time
Today, 00:52
Joined
Mar 4, 2008
Messages
3,856
Take a look at some of the samples posted in the Samples forum by Dom DXecutioner. He does some interesting stuff. One in particular caught my attention: ACC2000: Custom InterFace - Transparent Floating Panes

Your hypothesis sounds interesting and a bit fun...but I have no clue how to pull it off and don't have time to research it. Let us know what you come up with.
 

darbid

Registered User.
Local time
Today, 07:52
Joined
Jun 26, 2008
Messages
1,428
Thanks for the link.

It seems that VB can do this easily with for example the TransparencyKey. Where you can allocate a color to be transparent. You can even click through the transperant section to something below it.

But this is all not in VBA.

I think, I guess, to change a form is VBA I am going to have to pull out some API stuff. For me that is going to be a miracle, but I will have a poke around.

For a beginner Google, and try, test and try and test again are my methods.
 

darbid

Registered User.
Local time
Today, 07:52
Joined
Jun 26, 2008
Messages
1,428
First Attempt

From what I have read eg http://msdn.microsoft.com/en-us/library/ms633540(VS.85).aspx

and seen from the example above the "SetLayeredWindowAttributes Function" is the key.

So I created a simple example as follows

this went into a module
Code:
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long) As Long
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 SetLayeredWindowAttributes Lib "user32" _
  (ByVal hwnd As Long, _
   ByVal crKey As Long, _
   ByVal bAlpha As Byte, _
   ByVal dwFlags As Long) As Long
Private Const LWA_COLORKEY As Long = &H1
Private Const LWA_ALPHA     As Long = &H2
Private Const GWL_EXSTYLE   As Long = -20
Private Const WS_EX_LAYERED As Long = &H80000
Public Sub SetFormOpacity(frm As Form, trns As Long, sngOpacity As Single)
    Dim lngStyle As Long
 
    ' get the current window style, then set transparency
    lngStyle = GetWindowLong(frm.hwnd, GWL_EXSTYLE)
    'SetWindowLong frm.hwnd, GWL_EXSTYLE, lngStyle Or WS_EX_LAYERED
    SetLayeredWindowAttributes frm.hwnd, trns, (sngOpacity * 255), LWA_ALPHA Or LWA_COLORKEY
End Sub

the make a a form and this this in the on load
Code:
Dim transcolor As Long
 
    transcolor = RGB(100, 200, 100)
 
 
    Me.Detail.BackColor = transcolor
 
 
    Me.Painting = False
 
    SetFormOpacity Me, transcolor, 1
    Me.Painting = True

the form also should be

pop-up - yes
record selector - no
borders - none
Detail.BackColor = 6604900 (which should be RGB(100,200,100)
I added a simple close button.

Then open your form. You will get a transparent form except for your button. :)

Now if I add a nice picture to this with a shadow and transperant otherwise it should appear to be "the form", put controls on it and I have my "skin" eyecandy
 
Last edited:
Local time
Today, 00:52
Joined
Mar 4, 2008
Messages
3,856
From what I have read eg http://msdn.microsoft.com/en-us/library/ms633540(VS.85).aspx

and seen from the example above the "SetLayeredWindowAttributes Function" is the key.

So I created a simple example as follows

this went into a module
Code:
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long) As Long
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 SetLayeredWindowAttributes Lib "user32" _
  (ByVal hwnd As Long, _
   ByVal crKey As Long, _
   ByVal bAlpha As Byte, _
   ByVal dwFlags As Long) As Long
Private Const LWA_COLORKEY As Long = &H1
Private Const LWA_ALPHA     As Long = &H2
Private Const GWL_EXSTYLE   As Long = -20
Private Const WS_EX_LAYERED As Long = &H80000
Public Sub SetFormOpacity(frm As Form, trns As Long, sngOpacity As Single)
    Dim lngStyle As Long
 
    ' get the current window style, then set transparency
    lngStyle = GetWindowLong(frm.hwnd, GWL_EXSTYLE)
    'SetWindowLong frm.hwnd, GWL_EXSTYLE, lngStyle Or WS_EX_LAYERED
    SetLayeredWindowAttributes frm.hwnd, trns, (sngOpacity * 255), LWA_ALPHA Or LWA_COLORKEY
End Sub

the make a a form and this this in the on load
Code:
Dim transcolor As Long
 
    transcolor = RGB(100, 200, 100)
 
 
    Me.Detail.BackColor = transcolor
 
 
    Me.Painting = False
 
    SetFormOpacity Me, transcolor, 1
    Me.Painting = True

the form also should be

pop-up - yes
record selector - no
borders - none
Detail.BackColor = 6604900 (which should be RGB(100,200,100)
I added a simple close button.

Then open your form. You will get a transparent form except for your button. :)

Now if I add a nice picture to this with a shadow and transperant otherwise it should appear to be "the form", put controls on it and I have my "skin" eyecandy

Pretty slick. It'd be cool if you posted a sample database in the samples forum.
 

darbid

Registered User.
Local time
Today, 07:52
Joined
Jun 26, 2008
Messages
1,428
I will put a sample in there for everyone.

There is one thing though. I do not suppose anyone can get VBA and GDI+ to "draw" a png image with ARGB colors (alpha channels) That would really make these transparent forms come alive with eyecandy.
 

The Nine Inch Ninja

New member
Local time
Today, 06:52
Joined
Apr 17, 2009
Messages
3
Re: First Attempt

Hi Darbid, does this work in access 2003, I've tried your example but no joy!
 

timothyl

Registered User.
Local time
Today, 00:52
Joined
Jun 4, 2009
Messages
92
Hello, I am using access 2003 and can not get this to work,
Detail.BackColor = 6604900 (which should be RGB(100,200,100)/QUOTE] turns the back ground green, any Ideas on what to do. Thanks
 
Last edited:

timothyl

Registered User.
Local time
Today, 00:52
Joined
Jun 4, 2009
Messages
92
Shadow, thanks for the response. I tried using the code from the form that has the transparent holes, I imported all the modules and the form itself when I open the imported from if works fine, I have pasted into my form all the code from the imported form, I keep geting

apiRGB RGBx3(0), lngColor, 4

apiRGB sub or function not defined, can not figure out why as again the imported form works fine, any insight into why?
 

shadow9449

Registered User.
Local time
Today, 01:52
Joined
Mar 5, 2004
Messages
1,037
The code is not mine and I've never used it. However, just browsing through the project, I see that there's a form called frmCombineRgn which has a function declaration for apiRGB. Do you have this in your project?

SHADOW
 

darbid

Registered User.
Local time
Today, 07:52
Joined
Jun 26, 2008
Messages
1,428
Hi it has been a long time since I did that. If you post an example
mdb I will have a look for you.
 

thenman

Registered User.
Local time
, 22:52
Joined
Aug 29, 2009
Messages
23
Sorry to dig up an old thread, but I had something to add, georgedwilkinson code wasn't working for me, here is a code that is almost the same, but works.


For the Module:
Code:
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long) As Long
 
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 SetLayeredWindowAttributes Lib "user32" _
  (ByVal hwnd As Long, _
   ByVal crKey As Long, _
   ByVal bAlpha As Byte, _
   ByVal dwFlags As Long) As Long
 
Private Const LWA_ALPHA     As Long = &H2

Private Const GWL_EXSTYLE   As Long = -20

Private Const WS_EX_LAYERED As Long = &H80000
 
Public Sub SetFormOpacity(frm As Form, sngOpacity As Single, TColor As Long)
    Dim lngStyle As Long

    ' get the current window style, then set transparency
    lngStyle = GetWindowLong(frm.hwnd, GWL_EXSTYLE)
    SetWindowLong frm.hwnd, GWL_EXSTYLE, lngStyle Or WS_EX_LAYERED
    SetLayeredWindowAttributes frm.hwnd, TColor, (sngOpacity * 255), LWA_ALPHA
End Sub

And in the From on the Form_Load()
Code:
Private Sub Form_load()
Dim Transp As Long
Transp = RGB(0, 0, 0) 'This is the color you want your background to be
 
Me.Detail.BackColor = Transp
 
Me.Painting = False
SetFormOpacity Me, 0.5, Transp
Me.Painting = True
End Sub
 

thenman

Registered User.
Local time
, 22:52
Joined
Aug 29, 2009
Messages
23
The only differences I could find were:

Private Const LWA_COLORKEY As Long = &H1

and in the SetFormOpacy sub:
SetLayeredWindowAttributes frm.hwnd, TColor, (sngOpacity * 255), LWA_ALPHA or LWA_COLORKEY

When I added the two of these to my code, the form was no longer visible, and the TColor adjusted the transparency of the controls on the form, not the form itself.
 

regikono

New member
Local time
Today, 02:52
Joined
Oct 14, 2012
Messages
3
Sorry to dig up an old thread, but I had something to add, georgedwilkinson code wasn't working for me, here is a code that is almost the same, but works.


For the Module:
Code:
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long) As Long
 
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 SetLayeredWindowAttributes Lib "user32" _
  (ByVal hwnd As Long, _
   ByVal crKey As Long, _
   ByVal bAlpha As Byte, _
   ByVal dwFlags As Long) As Long
 
Private Const LWA_ALPHA     As Long = &H2

Private Const GWL_EXSTYLE   As Long = -20

Private Const WS_EX_LAYERED As Long = &H80000
 
Public Sub SetFormOpacity(frm As Form, sngOpacity As Single, TColor As Long)
    Dim lngStyle As Long

    ' get the current window style, then set transparency
    lngStyle = GetWindowLong(frm.hwnd, GWL_EXSTYLE)
    SetWindowLong frm.hwnd, GWL_EXSTYLE, lngStyle Or WS_EX_LAYERED
    SetLayeredWindowAttributes frm.hwnd, TColor, (sngOpacity * 255), LWA_ALPHA
End Sub

And in the From on the Form_Load()
Code:
Private Sub Form_load()
Dim Transp As Long
Transp = RGB(0, 0, 0) 'This is the color you want your background to be
 
Me.Detail.BackColor = Transp
 
Me.Painting = False
SetFormOpacity Me, 0.5, Transp
Me.Painting = True
End Sub

This last adjustment has worked fine in my access 2003. Thank you for your contribution guys!
 

Users who are viewing this thread

Top Bottom