PDA

View Full Version : Userform Opacity



AndrewKent
02-06-2009, 02:10 AM
I wonder if anyone could help me,

I have a number of userforms that all open a "toolbox" userform. This is smaller in size on screen and sits over the userform that opened it (until the toolbox is closed when control returns to the previous userform).

Now, when the "toolbox" is opened, I set the opacity of the userfrom that opened it to 200. Using this code...


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
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 crey As Byte, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Const LWA_ALPHA = &H2&
Public hWnd As Long

Private Sub imgToolbox_Click()
' =========================================================================== ==================
' This macro will load the toolbox which allows the user to carry out various tasks within the
' program.
' =========================================================================== ==================
Dim bytOpacity As Byte
bytOpacity = 200 ' variable keeping opacity setting
hWnd = FindWindow("ThunderDFrame", Me.Caption)
Call SetWindowLong(Me.hWnd, GWL_EXSTYLE, GetWindowLong(Me.hWnd, GWL_EXSTYLE) Or WS_EX_LAYERED)
Call SetLayeredWindowAttributes(Me.hWnd, 0, bytOpacity, LWA_ALPHA)
Load frmToolbox
frmToolbox.Show

End Sub

...however when I close the toolbox, I cannot seem to get it to set the Opacity of any open userform to 255...


Private Sub imgClose_Click()
' =========================================================================== ==================
' This image closes the userform.
' =========================================================================== ==================
Dim frm As UserForm

For Each frm In UserForms
Dim bytOpacity As Byte
bytOpacity = 255 ' variable keeping opacity setting
hWnd = FindWindow("ThunderDFrame", Me.Caption)
Call SetWindowLong(Me.hWnd, GWL_EXSTYLE, GetWindowLong(Me.hWnd, GWL_EXSTYLE) Or WS_EX_LAYERED)
Call SetLayeredWindowAttributes(Me.hWnd, 0, bytOpacity, LWA_ALPHA)
Next frm

End Sub

...it needs to be ANY open userform because the "toolbox" can be accessed from a number of different userforms.

Can anyone help?

Andy Pope
02-06-2009, 02:24 AM
Hi,

Don't you just need to reset opacity of the form that launched the toolbar form?



Private Sub imgToolbox_Click()
' =========================================================================== ==================
' This macro will load the toolbox which allows the user to carry out various tasks within the
' program.
' =========================================================================== ==================
Dim bytOpacity As Byte
bytOpacity = 200 ' variable keeping opacity setting
hWnd = FindWindow("ThunderDFrame", Me.Caption)
Call SetWindowLong(Me.hWnd, GWL_EXSTYLE, GetWindowLong(Me.hWnd, GWL_EXSTYLE) Or WS_EX_LAYERED)
Call SetLayeredWindowAttributes(Me.hWnd, 0, bytOpacity, LWA_ALPHA)
Load frmToolbox
frmToolbox.Show

bytOpacity = 255 ' variable keeping opacity setting
Call SetLayeredWindowAttributes(Me.hWnd, 0, bytOpacity, LWA_ALPHA)

End Sub




Private Sub imgClose_Click()
' =========================================================================== ==================
' This image closes the userform.
' =========================================================================== ==================
Unload Me
End Sub

AndrewKent
02-06-2009, 02:29 AM
You're spot on, I edited the code to do so and it worked a treat. thanks.