473,326 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Translucent Forms & Fade Effects

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
0 2515

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

Similar topics

1
by: Krhis | last post by:
A friend of mine gave me a link to these two sites, he wanted to know the scripts that are used here... but it seems that not even I can help him. So I am hoping some one else could help us by...
7
by: Lloyd Dupont | last post by:
In my C# & MC++ app there is some drawing done through GDI (not +, but win32). I would like to be able to create a translucent HBRUSH. the RGB macro create only solid color... and, anyway,...
4
by: Chris Lieb | last post by:
Hi, I am writing a class in JavaScript to cause a repeating fade effect. The fade class takes care of the fading and the rgb class takes care of manipulating rgb values: function rgb(red,...
1
by: Luciano A. Ferrer | last post by:
Hello! Im trying to do a few fade effects here http://relojurbano.com.ar/scalda/baseporque.php using fadomatic ( http://chimpen.com/fadomatic/ ) I dont know if fadomatic is a good solution...
10
by: Immortalist | last post by:
Various aquisition devices that guide learning along particular pathways towards human biases. And as E.O. Wilson might say mental development appears to be genetically constrained. (1) Language...
2
Ali Rizwan
by: Ali Rizwan | last post by:
Hi all, I made a usercontrol and want to give fade effects to it. Now problem is that i m using alpha command as i use in my cool form. Nothing is happninig. How can i give fade effect to a...
5
by: Gretsch | last post by:
Hi, Can someone help me with the command format please. I have a function (called fade) that was 3 parameters: the first 2 are colours therefore formatted #123456 and the 3rd is a number of...
3
by: Sin Jeong-hun | last post by:
As far as I know if I change the form's opacity, every control on the form also inherits that opacity. But in many real applications (not application programs), we see opaque text (100% opacity) on...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.