This function allows you to set the opacity for a PopUp form. Normally, forms are fully opaque and you cannot see through the form. When it is partially opaque, you can partially see what is behind the form.
I use it for smoothly fading in and out forms in my Microsoft Access application (specifically, a full-screen picture viewer).
- Save this code into a new module called FormHelper.
- Use FormHelper.SetOpacity as follows:
- Call FormHelper.SetOpacity Form_YourForm, 0 to make your form fully transparent.
- Call FormHelper.SetOpacity Form_YourForm, 100 to make your form fully opaque.
- Call FormHelper.SetOpacity Form_YourForm, Opacity where Opacity is any value between 1 and 99 to make your form partially opaque (or partially transparent!).
The code is fully commented and contains more details on implementation and what the functions can and can’t do.
Option Compare Database Option Explicit Private Const Namespace$ = "FormHelper" ''' ''' Used by SetOpacity and SetTransparentColor ''' Private Const GWL_EXSTYLE = (-20) Private Const LWA_COLORKEY = 1 Private Const LWA_ALPHA = 2 Private Const WS_EX_LAYERED = &H80000 Private Declare Function apiGetWindowLong Lib "user32" Alias "GetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long) As Long Private Declare Function apiSetLayeredWindowAttributes Lib "user32" Alias "SetLayeredWindowAttributes" ( _ ByVal hwnd As Long, _ ByVal color As Long, _ ByVal AlphaPercent As Byte, _ ByVal alpha As Long) As Boolean Private Declare Function apiSetWindowLong Lib "user32" Alias "SetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long, _ ByVal dwNewLong As Long) As Long ''' ''' Set opacity of form ''' ''' ''' Setting OpacityPercent to zero makes the form fully transparent. ''' Setting OpacityPercent to 100 makes the form fully opaque. ''' ''' This only has an affect on forms whose PopUp property is True. ''' Public Sub SetOpacity( _ frm As Access.Form, _ OpacityPercent As Byte) ' Perform checks on arguments ' Ensure frm is a PopUp form. Raise an error if it is not. If Not frm.PopUp Then Err.Raise 5, , "Invalid argument." & vbCrLf & Namespace$ & " cannot SetOpacity on form " & frm.Name & ". PopUp form required." Exit Sub End If ' Ensure OpacityPercent is between 0 and 100. ' Do not raise an error if out of range, simply fix it. If OpacityPercent < 0 Then OpacityPercent = 0 ElseIf OpacityPercent > 100 Then OpacityPercent = 100 End If ' -------------------------------------------------- ' If we reach here, all arguments have been accepted ' -------------------------------------------------- ' Convert supplied percentage value to one ranging between 0 and 255 for apiSetLayeredWindowAttributes Dim iAlpha As Integer iAlpha = (OpacityPercent / 100) * 255 ' Get forms current extended attributes Dim attrib As Long attrib = apiGetWindowLong(frm.hwnd, GWL_EXSTYLE) ' Set form to have extended layered attribute apiSetWindowLong frm.hwnd, GWL_EXSTYLE, attrib Or WS_EX_LAYERED ' Set opacity apiSetLayeredWindowAttributes frm.hwnd, RGB(0, 0, 0), iAlpha, LWA_ALPHA End Sub
You can also download FormHelper.bas. This is always the latest complete version of this module and contains more functions than detailed here. The functions are fully commented.
This code was written in Visual Basic for Applications (VB, VBA) for Microsoft Access 2002 (XP) on Windows XP.