473,545 Members | 2,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Translucent Forms & Fade Effects

31 New Member
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 SetLayeredWindo wAttributes function, by using the following code:

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
  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
  14. Public Const GWL_EX_STYLE = -20
  15. Public Const LWA_ALPHA = &H2
  16. Public Const WS_EX_LAYERED = &H80000
  18. Public BlendVal As Long
  19. Public LayWinAttribRef As Long
  20. Public WinVer As OSVERSIONINFO
(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 SetLayeredWindo wAttributes in the Form_Activate procedure prevents a black-fade effect from occurring)

General Code
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Activate()
  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
  9. End Sub
  11. Private Sub imgTitleStetch_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  13.     Select Case Button
  14.     Case 1
  15.         ...
  16.         If WinVer.dwMajorVersion >= 5 Then Call TransFormOut(hWnd, 7)
  17.     End Select
  19. End Sub
  21. Private Sub imgTitleStetch_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  23.     Select Case Button
  24.     Case 1
  25.         If WinVer.dwMajorVersion >= 5 Then Call TransFormIn(hWnd, 7)
  26.     End Select
  28. End Sub
  30. Public Sub TransFormIn(frmHWND As Long, FazeSpeed As Long)
  32.     For Faze = 90 To 255 Step FazeSpeed
  33.         SetLayeredWindowAttributes frmHWND, 0, Faze, LWA_ALPHA
  34.     Next Faze
  36.     If Faze <> 255 Then
  37.         SetLayeredWindowAttributes frmHWND, 0, 255, LWA_ALPHA
  38.     End If
  40. End Sub
  42. Public Sub TransFormOut(frmHWND As Long, FazeSpeed As Long)
  44.     For Faze = 255 To 90 Step -FazeSpeed
  45.         SetLayeredWindowAttributes frmHWND, 0, Faze, LWA_ALPHA
  46.     Next Faze
  48.     If Faze <> 90 Then
  49.         SetLayeredWindowAttributes frmHWND, 0, 90, LWA_ALPHA
  50.     End If
  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 UpdateLayeredWi ndow function (in theory). At current, the best code I have managed to produce using this function is as follows:

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
  9. Public Type BLENDFUNCTION
  10.     BlendOp As Byte
  11.     BlendFlags As Byte
  12.     SourceConstantAlpha As Byte
  13.     AlphaFormat As Byte
  14. End Type
  16. Public Type POINT
  17.     x As Long
  18.     y As Long
  19. End Type
  21. Public Type SIZE
  22.     cx As Long
  23.     cy As Long
  24. End Type
  26. Public Const GWL_EX_STYLE = -20
  27. Public Const ULW_ALPHA = &H2
  28. Public Const WS_EX_LAYERED = &H80000
  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
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()
  3.     BlendVal = GetWindowLong(hWnd, GWL_EXSTYLE) Or WS_EX_LAYERED
  4.     SetWindowLong hWnd, GWL_EXSTYLE, BlendVal
  6.     SourceDC = CreateCompatibleDC(hDC)
  8.     BlendPic = CreateCompatibleBitmap(hDC, ScaleWidth, ScaleHeight)
  10.     SelectObject SourceDC, BlendPic
  12.     BitBlt SourceDC, 0, 0, ScaleWidth, ScaleHeight, hDC, 0, 0, SRCCOPY
  14.     With BlendFunc
  15.         .AlphaFormat = 0
  16.         .BlendFlags = 0
  17.         .BlendOp = AC_SRC_OVER
  18.         .SourceConstantAlpha = 90
  19.     End With
  21.     WindowSize.cx = ScaleWidth
  22.     WindowSize.cy = ScaleHeight
  24.     UpdateLayeredWindow hWnd, 0, ByVal 0, WindowSize, SourceDC, SourcePoint, 0, BlendFunc, ULW_ALPHA
  25.     ScaleMode = 1 'Change back to twips
  27. End Sub
As claimed by Microsoft, using the UpdateLayeredWi ndow function is supposedly more efficient than using the SetLayeredWindo wAttributes 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 UpdateLayeredWi ndow function to achieve translucency on an entire form (controls included), I would very much appreciate any answers offered.
Apr 3 '07 #1
0 2543

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

Similar topics

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 showing were we could get the sources of these two "fade" effects. Text links are blue, but when you put your mouse on them, they turn white (from...
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, CreateSolidBrush ignore the higher byte. Any idea? I though I might do some trick with CreateDIBPatternBrush but I'm not sure
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, green, blue) { // private variable initialization this.red = red; this.green = green;
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 for it but Im having problems with IE... If you look that page in IE, the upper fades do not work
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 Aquisition Device (2) Color Aqusition Device (3) Sound Aquistion Device (4) Smell Aquisition Device (5) Touch Aquisition Device (6) Art...
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 usercontrol. Thanx
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 milliseconds. A normal call to the function therefore looks like this:
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 a traslucent background, for example text messages on TV. Or look at the title bar text of Aero. Background is translucent but the text is opaque....
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.