Transparent Forms

Ever had a form that you had to keep moving out of the way so you could see the data behind it? The following code sets the userform at a 50% transparency (see Figure 21.19) so that you can see the data behind without moving the form somewhere else on the screen (and blocking more data).

Figure 21.19

Create a 50% transparent form to view the data on the sheet behind it.

Place the following in the declarations section of the userform:

Private Declare Function GetActiveWindow Lib "USER32" () 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 GetWindowLong Lib "USER32" Alias _

"GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetLayeredWindowAttributes Lib "USER32" _ (ByVal hWnd As Long, ByVal crKey As Integer, _ ByVal bAlpha As Integer, ByVal dwFlags As Long) As Long Private Const WS_EX_LAYERED = &H80000 Private Const LWA_COLORKEY = &H1 Private Const LWA_ALPHA = &H2 Private Const GWL_EXSTYLE = &HFFEC Dim hWnd As Long

Place the following behind a userform. When the form is activated, the transparency will be set:

Private Sub UserForm_Activate() Dim nIndex As Long hWnd = GetActiveWindow nIndex = GetWindowLong(hWnd, GWL_EXSTYLE) SetWindowLong hWnd, GWL_EXSTYLE, nIndex Or WS_EX_LAYERED

'50% semitransparent

SetLayeredWindowAttributes hWnd, 0, (255 * 50) / 100, LWA_ALPHA End Sub

0 0

Post a comment