OVH Cloud OVH Cloud

[Réponse/Solution] Form transpente (Alpha Blend)

4 réponses
Avatar
ng
Bonjour à tous,

On m'a posé la question tout à l'heure par email, je transmet donc ma
réponse sur le groupe, ca peut en interessé d'autres.


'//Mettre un CommandButton nommé Command1 sur la feuille et coller ce code :
'//Fait à partir d'un exemple de l'API-Guide
Option Explicit

Private Const LWA_ALPHA = &H2
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd
As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long)
As Long

Public Sub FixTransparence(lTrans As Long, lhWnd As Long)
Dim lRet As Long
lRet = GetWindowLong(lhWnd, GWL_EXSTYLE)
lRet = lRet Or WS_EX_LAYERED
Call SetWindowLong(lhWnd, GWL_EXSTYLE, lRet)
Call SetLayeredWindowAttributes(lhWnd, 0, lTrans, LWA_ALPHA)
End Sub

Private Sub Command1_Click()
Call FixTransparence(128, Me.hWnd)
End Sub



--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/
http://apisvb.europe.webmatrixhosting.net/

4 réponses

Avatar
François Picalausa
Hello,

Voir aussi l'article de la KB:
http://support.microsoft.com/default.aspx?kbid$9341

--
François Picalausa (MVP VB)
http://faq.vb.free.fr --- http://msdn.microsoft.com
http://apisvb.europe.webmatrixhosting.net

"ng" a écrit dans le message de
news:ecK$
Bonjour à tous,

On m'a posé la question tout à l'heure par email, je transmet donc ma
réponse sur le groupe, ca peut en interessé d'autres.


'//Mettre un CommandButton nommé Command1 sur la feuille et coller ce
code : '//Fait à partir d'un exemple de l'API-Guide
Option Explicit

Private Const LWA_ALPHA = &H2
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Declare Function GetWindowLong Lib "user32" Alias
"GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias
"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal
dwNewLong As Long) As Long Private Declare Function
SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal
crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Public Sub FixTransparence(lTrans As Long, lhWnd As Long)
Dim lRet As Long
lRet = GetWindowLong(lhWnd, GWL_EXSTYLE)
lRet = lRet Or WS_EX_LAYERED
Call SetWindowLong(lhWnd, GWL_EXSTYLE, lRet)
Call SetLayeredWindowAttributes(lhWnd, 0, lTrans, LWA_ALPHA)
End Sub

Private Sub Command1_Click()
Call FixTransparence(128, Me.hWnd)
End Sub


Avatar
Pascal B.
Bonjour,

Le code met toute la Form transparente (ou semi-transparente) ainsi que les contrôles!
Quelle est l'interrêt ???

Ce qui serait intéressant, ce serait d'avoir des zones non-transprentes...

Pascal

"François Picalausa" a écrit dans le message de news:
| Hello,
|
| Voir aussi l'article de la KB:
| http://support.microsoft.com/default.aspx?kbid$9341
|
| --
| François Picalausa (MVP VB)
| http://faq.vb.free.fr --- http://msdn.microsoft.com
| http://apisvb.europe.webmatrixhosting.net
|
| "ng" a écrit dans le message de
| news:ecK$
| > Bonjour à tous,
| >
| > On m'a posé la question tout à l'heure par email, je transmet donc ma
| > réponse sur le groupe, ca peut en interessé d'autres.
| >
| >
| > '//Mettre un CommandButton nommé Command1 sur la feuille et coller ce
| > code : '//Fait à partir d'un exemple de l'API-Guide
| > Option Explicit
| >
| > Private Const LWA_ALPHA = &H2
| > Private Const GWL_EXSTYLE = (-20)
| > Private Const WS_EX_LAYERED = &H80000
| > Private Declare Function GetWindowLong Lib "user32" Alias
| > "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
| > Private Declare Function SetWindowLong Lib "user32" Alias
| > "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal
| > dwNewLong As Long) As Long Private Declare Function
| > SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal
| > crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
| >
| > Public Sub FixTransparence(lTrans As Long, lhWnd As Long)
| > Dim lRet As Long
| > lRet = GetWindowLong(lhWnd, GWL_EXSTYLE)
| > lRet = lRet Or WS_EX_LAYERED
| > Call SetWindowLong(lhWnd, GWL_EXSTYLE, lRet)
| > Call SetLayeredWindowAttributes(lhWnd, 0, lTrans, LWA_ALPHA)
| > End Sub
| >
| > Private Sub Command1_Click()
| > Call FixTransparence(128, Me.hWnd)
| > End Sub
|
|
Avatar
ng
Salut,

Je sais un pas, mais ca fait un effet interessant pour une form-menu par
exemple (un peu transparente), j'ai déjà vu ca et l'effet est assez
surprenant.

On pourrait également s'en servir pour faire apparaitre/disparaitre la form
progressivement, place ce code dans une form :

Option Explicit
Private Const LWA_ALPHA = &H2
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd
As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long)
As Long

Dim bShow As Boolean

Public Sub FixTransparence(lTrans As Long, lhWnd As Long)
Dim lRet As Long
lRet = GetWindowLong(lhWnd, GWL_EXSTYLE)
lRet = lRet Or WS_EX_LAYERED
Call SetWindowLong(lhWnd, GWL_EXSTYLE, lRet)
Call SetLayeredWindowAttributes(lhWnd, 0, lTrans, LWA_ALPHA)
End Sub

Private Sub Form_Activate()
If Not bShow Then
bShow = True
Dim i As Long
For i = 1 To 255
Call FixTransparence(i, Me.hWnd)
DoEvents
Next
End If
End Sub

Private Sub Form_Load()

Call FixTransparence(0, Me.hWnd)

End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim i As Long
For i = 255 To 0 Step -1
Call FixTransparence(i, Me.hWnd)
DoEvents
Next
End Sub


--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/
http://apisvb.europe.webmatrixhosting.net/

Pascal B. <pascbr{_AROBASE_}hotmail{_POINT_}com> a écrit :

Bonjour,

Le code met toute la Form transparente (ou semi-transparente) ainsi
que les contrôles!
Quelle est l'interrêt ???

Ce qui serait intéressant, ce serait d'avoir des zones
non-transprentes...

Pascal

"François Picalausa" a écrit dans le message de
news:
Hello,

Voir aussi l'article de la KB:
http://support.microsoft.com/default.aspx?kbid$9341

--
François Picalausa (MVP VB)
http://faq.vb.free.fr --- http://msdn.microsoft.com
http://apisvb.europe.webmatrixhosting.net

"ng" a écrit dans le message de
news:ecK$
Bonjour à tous,

On m'a posé la question tout à l'heure par email, je transmet donc
ma réponse sur le groupe, ca peut en interessé d'autres.


'//Mettre un CommandButton nommé Command1 sur la feuille et coller
ce code : '//Fait à partir d'un exemple de l'API-Guide
Option Explicit

Private Const LWA_ALPHA = &H2
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Declare Function GetWindowLong Lib "user32" Alias
"GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias
"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal
dwNewLong As Long) As Long Private Declare Function
SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal
crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Public Sub FixTransparence(lTrans As Long, lhWnd As Long)
Dim lRet As Long
lRet = GetWindowLong(lhWnd, GWL_EXSTYLE)
lRet = lRet Or WS_EX_LAYERED
Call SetWindowLong(lhWnd, GWL_EXSTYLE, lRet)
Call SetLayeredWindowAttributes(lhWnd, 0, lTrans, LWA_ALPHA)
End Sub

Private Sub Command1_Click()
Call FixTransparence(128, Me.hWnd)
End Sub






Avatar
François Picalausa
Hello!

non pas tout transparent.. seulement partiellement!
Soit partiellement par le niveau de transparence, comme dans le post de ng
soit partiellement par zones de couleurs!

Private Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias _
"SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" _
(ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, _
ByVal dwFlags As Long) As Long

Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Const WS_EX_TRANSPARENT = &H20&
Private Const LWA_COLORKEY = &H1

Private Sub Form_Load()
Dim lOldStyle As Long
Dim bTrans As Byte ' The level of transparency (0 - 255)

bTrans = 255
lOldStyle = GetWindowLong(Me.hwnd, GWL_EXSTYLE)
SetWindowLong Me.hwnd, GWL_EXSTYLE, lOldStyle Or WS_EX_LAYERED

Me.BackColor = vbRed 'il faut une "vraie" couleur RGB et
'pas une couleur système
SetLayeredWindowAttributes Me.hwnd, Me.BackColor, 0, LWA_COLORKEY

End Sub

--
François Picalausa (MVP VB)
http://faq.vb.free.fr --- http://msdn.microsoft.com
http://apisvb.europe.webmatrixhosting.net

"Pascal B." <pascbr{_AROBASE_}hotmail{_POINT_}com> a écrit dans le
message de news:%
Bonjour,

Le code met toute la Form transparente (ou semi-transparente) ainsi
que les contrôles!
Quelle est l'interrêt ???

Ce qui serait intéressant, ce serait d'avoir des zones
non-transprentes...

Pascal

"François Picalausa" a écrit dans le message de
news:
Hello,

Voir aussi l'article de la KB:
http://support.microsoft.com/default.aspx?kbid$9341

--
François Picalausa (MVP VB)
http://faq.vb.free.fr --- http://msdn.microsoft.com
http://apisvb.europe.webmatrixhosting.net

"ng" a écrit dans le message de
news:ecK$
Bonjour à tous,

On m'a posé la question tout à l'heure par email, je transmet donc
ma réponse sur le groupe, ca peut en interessé d'autres.


'//Mettre un CommandButton nommé Command1 sur la feuille et coller
ce code : '//Fait à partir d'un exemple de l'API-Guide
Option Explicit

Private Const LWA_ALPHA = &H2
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Declare Function GetWindowLong Lib "user32" Alias
"GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias
"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal
dwNewLong As Long) As Long Private Declare Function
SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal
crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Public Sub FixTransparence(lTrans As Long, lhWnd As Long)
Dim lRet As Long
lRet = GetWindowLong(lhWnd, GWL_EXSTYLE)
lRet = lRet Or WS_EX_LAYERED
Call SetWindowLong(lhWnd, GWL_EXSTYLE, lRet)
Call SetLayeredWindowAttributes(lhWnd, 0, lTrans, LWA_ALPHA)
End Sub

Private Sub Command1_Click()
Call FixTransparence(128, Me.hWnd)
End Sub