Hi RASHAD! Thanks for your code example. I have managed to get the fading routine to work with SetLayeredWindowAttributes(), but I'm trying to get it to work with UpdateLayeredWindow(), which draws a transparent shaped image. I have changed just a few lines of
this code by Le Soldat Inconnu, to demonstrate what I mean (to test this code, use any transparent PNG image, or
download this one):
Code:
;===================================================
; Skin window with PNG (Alpha channel support)
;
; by Le Soldat Inconnu - Sep 16, 2009
;===================================================
UsePNGImageDecoder()
Procedure SetLayeredWindow2(WindowID) ; Mettre l'attribut WS_EX_LAYERED à la fenêtre
SetWindowLong_(WindowID, #GWL_EXSTYLE, GetWindowLong_(WindowID, #GWL_EXSTYLE) | #WS_EX_LAYERED) ; Mettre l'attribut WS_EX_LAYERED à la fenêtre
EndProcedure
Procedure AlphaImageWindow2(WindowID, ImageID, Alpha) ; Mettre une image PNG comme fond d'une fenêtre
Protected Image_HDC, Image_Bitmap.BITMAP, Image_BitmapInfo.BITMAPINFO, ContextOffset.POINT, Blend.BLENDFUNCTION
Protected xx, yy, x, y, Rouge, Vert, Bleu, AlphaChannel
; Précalcul
Protected Dim Echelle.f($FF)
For x = 0 To $FF
Echelle(x) = x / $FF
Next
; Chargement du HDC
Image_HDC = CreateCompatibleDC_(#NULL)
Image_Ancienne = SelectObject_(Image_HDC, ImageID)
; Dimension de l'image
GetObject_(ImageID, SizeOf(BITMAP), @Image_Bitmap)
Image_BitmapInfo\bmiHeader\biSize = SizeOf(BITMAPINFOHEADER)
Image_BitmapInfo\bmiHeader\biWidth = Image_Bitmap\bmWidth
Image_BitmapInfo\bmiHeader\biHeight = Image_Bitmap\bmHeight
Image_BitmapInfo\bmiHeader\biPlanes = 1
Image_BitmapInfo\bmiHeader\biBitCount = 32
; Zone mémoire pour copier l'image
xx = Image_Bitmap\bmWidth - 1
yy = Image_Bitmap\bmHeight - 1
Protected Dim Image.l(xx, yy)
; Copie de l'image en mémoire
GetDIBits_(Image_HDC, ImageID, 0, Image_Bitmap\bmHeight, @Image(), @Image_BitmapInfo, #DIB_RGB_COLORS)
; Modification de l'image en mémoire
For x = 0 To xx
For y = 0 To yy
Couleur = Image(x, y)
AlphaChannel = Couleur >> 24 & $FF
If AlphaChannel < $FF
Rouge = (Couleur & $FF) * Echelle(AlphaChannel)
Vert = (Couleur >> 8 & $FF) * Echelle(AlphaChannel)
Bleu = (Couleur >> 16 & $FF) * Echelle(AlphaChannel)
Image(x, y) = Rouge | Vert << 8 | Bleu << 16 | AlphaChannel << 24
EndIf
Next
Next
; Transfert de la mémoire dans la l'image de base
SetDIBits_(Image_HDC, ImageID, 0, Image_Bitmap\bmHeight, @Image(), @Image_BitmapInfo, #DIB_RGB_COLORS)
; L'image est mise en skin de la fenêtre
;Blend\SourceConstantAlpha = Alpha ; niveau de transparence
Blend\AlphaFormat = 1 ; Support de la couche alpha
Blend\BlendOp = 0
Blend\BlendFlags = 0
;===================================================================
;the transparency decreases from 1 to 255 to fade the window in
For L = 1 To 255
Blend\SourceConstantAlpha = L
UpdateLayeredWindow_(WindowID, 0, 0, @Image_BitmapInfo + 4, Image_HDC, @ContextOffset, 0, @Blend, 2)
Delay(6)
Next L
;reversing the transparency progression from 255 to 1 does not work
;===================================================================
; Fermeture du HDC
SelectObject_(Image_HDC, Image_Ancienne)
DeleteDC_(Image_HDC)
EndProcedure
Fichier.s = OpenFileRequester("Image", "", "PNG|*.png", 1)
If Fichier
If LoadImage(0, Fichier)
If OpenWindow(0, 0, 0, ImageWidth(0), ImageHeight(0), "Test", #PB_Window_BorderLess | #PB_Window_ScreenCentered | #PB_Window_Invisible)
SetLayeredWindow2(WindowID(0))
;========================================================================
;moved these two line up to unhide and keep window topmost while fading
StickyWindow(0, 1) : HideWindow(0, 0)
;========================================================================
AlphaImageWindow2(WindowID(0), ImageID(0), 200)
Repeat
Event = WaitWindowEvent()
If Event = #WM_LBUTTONDOWN
SendMessage_(WindowID(0), #WM_NCLBUTTONDOWN, #HTCAPTION, 0)
EndIf
Until Event = #PB_Event_CloseWindow
EndIf
EndIf
EndIf
I know it's not the best way, but I've used the For/Next loop & Delay for a Q&D demonstration. You'll notice that the transparency can be decreased, but not increased. Any way to fix this?
Thanks!