473,769 Members | 3,857 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:

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 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()
  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 UpdateLayeredWi ndow 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 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 2557

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

Similar topics

1
2023
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 the left to the right). Example: http://www.digik.net/ And here too, another one, putting your...
7
3335
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
4
3533
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;
1
2130
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
10
10791
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 Aquisition Device
2
1887
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
5
3281
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:
3
5014
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. Is it possible to use 100% opaque text on a traslucent form?
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7410
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3962
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
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.