By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,097 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,097 IT Pros & Developers. It's quick & easy.

Translucent Forms & Fade Effects

P: 31
I have been attempting to solve this problem for a few weeks now, but I'm not having much luck...

I would like to create a seamless fading effect on each form/window in my program; one that, when an image is clicked with a mouse and held, the window fades away by approximately 50% transparency, and when the mouse button is released, fades back to opaque.

I have been able to do this to an extent, with the SetLayeredWindowAttributes function, by using the following code:

Declarations
Expand|Select|Wrap|Line Numbers
  1. Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
  2. Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
  3. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  4.  
  5. Public Type OSVERSIONINFO
  6.     dwOSVersionInfoSize As Long
  7.     dwMajorVersion As Long
  8.     dwMinorVersion As Long
  9.     dwBuildNumber As Long
  10.     dwPlatformId As Long
  11.     szCSDVersion As String * 128
  12. End Type
  13.  
  14. Public Const GWL_EX_STYLE = -20
  15. Public Const LWA_ALPHA = &H2
  16. Public Const WS_EX_LAYERED = &H80000
  17.  
  18. Public BlendVal As Long
  19. Public LayWinAttribRef As Long
  20. Public WinVer As OSVERSIONINFO
  21.  
(The version check is included, so that the program can be used in Windows operating systems prior to Windows 2000, without the program causing errors. The initial call to SetLayeredWindowAttributes in the Form_Activate procedure prevents a black-fade effect from occurring)

General Code
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Activate()
  2.  
  3.     If WinVer.dwMajorVersion >= 5 Then
  4.         BlendVal = GetWindowLong(hWnd, GWL_EXSTYLE) Or WS_EX_LAYERED
  5.         LayWinAttribRef = SetWindowLong(hWnd, GWL_EXSTYLE, BlendVal)
  6.         SetLayeredWindowAttributes hWnd, 0, 255, LWA_ALPHA
  7.     End If
  8.  
  9. End Sub
  10.  
  11. Private Sub imgTitleStetch_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  12.  
  13.     Select Case Button
  14.     Case 1
  15.         ...
  16.         If WinVer.dwMajorVersion >= 5 Then Call TransFormOut(hWnd, 7)
  17.     End Select
  18.  
  19. End Sub
  20.  
  21. Private Sub imgTitleStetch_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  22.  
  23.     Select Case Button
  24.     Case 1
  25.         If WinVer.dwMajorVersion >= 5 Then Call TransFormIn(hWnd, 7)
  26.     End Select
  27.  
  28. End Sub
  29.  
  30. Public Sub TransFormIn(frmHWND As Long, FazeSpeed As Long)
  31.  
  32.     For Faze = 90 To 255 Step FazeSpeed
  33.         SetLayeredWindowAttributes frmHWND, 0, Faze, LWA_ALPHA
  34.     Next Faze
  35.  
  36.     If Faze <> 255 Then
  37.         SetLayeredWindowAttributes frmHWND, 0, 255, LWA_ALPHA
  38.     End If
  39.  
  40. End Sub
  41.  
  42. Public Sub TransFormOut(frmHWND As Long, FazeSpeed As Long)
  43.  
  44.     For Faze = 255 To 90 Step -FazeSpeed
  45.         SetLayeredWindowAttributes frmHWND, 0, Faze, LWA_ALPHA
  46.     Next Faze
  47.  
  48.     If Faze <> 90 Then
  49.         SetLayeredWindowAttributes frmHWND, 0, 90, LWA_ALPHA
  50.     End If
  51.  
  52. End Sub
The above methods works to a degree. Mid-range computers or slower will perform the fade effect fine, but fast computers change the speed of the effect, so that it becomes slower to fade in and out (which doesn't make any sense).

Another method of achieving the fading windows effect is to use the UpdateLayeredWindow function (in theory). At current, the best code I have managed to produce using this function is as follows:

Declarations
Expand|Select|Wrap|Line Numbers
  1. Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  2. Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
  3. Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
  4. Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
  5. Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
  6. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  7. Public Declare Function UpdateLayeredWindow Lib "user32" (ByVal hWnd As Long, ByVal hdcDst As Long, pptDst As Any, psize As Any, ByVal hdcSrc As Long, pptSrc As Any, ByVal crKey As Long, ByRef pblend As BLENDFUNCTION, ByVal dwFlags As Long) As Long
  8.  
  9. Public Type BLENDFUNCTION
  10.     BlendOp As Byte
  11.     BlendFlags As Byte
  12.     SourceConstantAlpha As Byte
  13.     AlphaFormat As Byte
  14. End Type
  15.  
  16. Public Type POINT
  17.     x As Long
  18.     y As Long
  19. End Type
  20.  
  21. Public Type SIZE
  22.     cx As Long
  23.     cy As Long
  24. End Type
  25.  
  26. Public Const GWL_EX_STYLE = -20
  27. Public Const ULW_ALPHA = &H2
  28. Public Const WS_EX_LAYERED = &H80000
  29.  
  30. Public BlendFunc As BLENDFUNCTION
  31. Public BlendPic As Long
  32. Public BlendVal As Long
  33. Public SourceDC As Long
  34. Public SourcePoint As POINT
  35. Public WindowSize As SIZE
  36.  
For the following code to work properly, the form's "AutoRedraw" property and "HasDC" property must be set to True, and the form's '"ScaleMode" property must be set to Pixels.

General Code
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Activate()
  2.  
  3.     BlendVal = GetWindowLong(hWnd, GWL_EXSTYLE) Or WS_EX_LAYERED
  4.     SetWindowLong hWnd, GWL_EXSTYLE, BlendVal
  5.  
  6.     SourceDC = CreateCompatibleDC(hDC)
  7.  
  8.     BlendPic = CreateCompatibleBitmap(hDC, ScaleWidth, ScaleHeight)
  9.  
  10.     SelectObject SourceDC, BlendPic
  11.  
  12.     BitBlt SourceDC, 0, 0, ScaleWidth, ScaleHeight, hDC, 0, 0, SRCCOPY
  13.  
  14.     With BlendFunc
  15.         .AlphaFormat = 0
  16.         .BlendFlags = 0
  17.         .BlendOp = AC_SRC_OVER
  18.         .SourceConstantAlpha = 90
  19.     End With
  20.  
  21.     WindowSize.cx = ScaleWidth
  22.     WindowSize.cy = ScaleHeight
  23.  
  24.     UpdateLayeredWindow hWnd, 0, ByVal 0, WindowSize, SourceDC, SourcePoint, 0, BlendFunc, ULW_ALPHA
  25.     ScaleMode = 1 'Change back to twips
  26.  
  27. End Sub
As claimed by Microsoft, using the UpdateLayeredWindow function is supposedly more efficient than using the SetLayeredWindowAttributes function.

At the moment, I'm testing the translucent effect (on Windows XP) by starting the program with the first window at a translucency of 90. The code appears to only replicate the background picture of the window, but not the form's controls.

If anyone knows how to use the UpdateLayeredWindow function to achieve translucency on an entire form (controls included), I would very much appreciate any answers offered.
Apr 3 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.