Page 1 sur 2

SetWindowsTransparency

Publié : mer. 12/avr./2017 22:31
par Shadow
Salut, cette procédure ne fonctionne plus !

Code : Tout sélectionner

; Procédure pour changer l'oppacité de la Fenêtre.
Procedure SetWindowsTransparency(WindowNomber, level) 
  If level>=0 And level<101 
    hLib = LoadLibrary_("user32.dll") 
    If hLib 
      adr = GetProcAddress_(hLib,"SetLayeredWindowAttributes") 
      If adr
        SetWindowLong_(WindowID(WindowNomber),#GWL_EXSTYLE,GetWindowLong_(WindowID(WindowNomber),#GWL_EXSTYLE)|$00080000) ; #WS_EX_LAYERED = $00080000 
        CallFunctionFast(adr,WindowID(WindowNomber),0,255*level/100,2) 
      EndIf 
      FreeLibrary_(hLib) 
    EndIf 
  EndIf 
EndProcedure 
C'est cette ligne qui pose problème on dirait:

Code : Tout sélectionner

adr = GetProcAddress_(hLib,"SetLayeredWindowAttributes") 
C'est ça qui est chiant avec Windows et tous ses truc là, ça marche jamais longtemps !

Re: SetWindowsTransparency

Publié : mer. 12/avr./2017 23:19
par falsam

Code : Tout sélectionner

; Procédure pour changer l'oppacité de la Fenêtre.
Procedure SetWindowsTransparency(WindowNomber, level) 
  If level>=0 And level<101 
    hLib = LoadLibrary_("user32.dll") 
    If hLib 
      adr = GetProcAddress_(hLib,  Ascii("SetLayeredWindowAttributes")) 
      If adr
        SetWindowLong_(WindowID(WindowNomber),#GWL_EXSTYLE,GetWindowLong_(WindowID(WindowNomber),#GWL_EXSTYLE)|$00080000) ; #WS_EX_LAYERED = $00080000 
        CallFunctionFast(adr,WindowID(WindowNomber),0,255*level/100,2) 
      EndIf 
      FreeLibrary_(hLib) 
    EndIf 
  EndIf 
EndProcedure 

OpenWindow(0, 0, 0, 800, 600, "Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

SetWindowsTransparency(0, 50)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: SetWindowsTransparency

Publié : mer. 12/avr./2017 23:26
par celtic88
O Plus Simple

Code : Tout sélectionner


Procedure SetWindowTrans(WinId,Trandeg.l,iColor.l)
  SetLayeredWindowAttributes_(WinId,iColor,Trandeg,$03)
EndProcedure

OpenWindow(0, 0, 0, 390, 130, "", #PB_Window_SystemMenu)
SetWindowColor(0, RGB(0,0,0))
SetWindowLong_(WindowID(0),#GWL_EXSTYLE,GetWindowLong_(WindowID(0),#GWL_EXSTYLE)|$00080000)

SetWindowTrans(WindowID(0),100, 0) 

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow


Re: SetWindowsTransparency

Publié : mer. 12/avr./2017 23:45
par Shadow
Je vous remercie :)

@celtic88, Icolor et $03 servent a quoi ?

Re: SetWindowsTransparency

Publié : jeu. 13/avr./2017 9:24
par Ar-S
Quand tu as une question sur les api, regarde chez crosoft.
ici : https://msdn.microsoft.com/fr-fr/librar ... s.85).aspx
A COLORREF structure that specifies the transparency color key to be used when composing the layered window. All pixels painted by the window in this color will be transparent. To generate a COLORREF, use the RGB macro.
Type: DWORD
An action to be taken. This parameter can be one or more of the following values.
je te laisse utiliser les traducteurs.

Pour chaque API tu as une page dédiée. ça peut servir.

Pour la mise en oeuvre il serait plus aisé de mettre la proce sous cette forme.

Code : Tout sélectionner

Procedure SetWindowTrans(Win, Trandeg.l,iColor.l)
  WinId = WindowID(Win)
  SetWindowColor(WIN, iColor)
  SetWindowLong_(WindowID(0),#GWL_EXSTYLE,GetWindowLong_(WindowID(Win),#GWL_EXSTYLE)|$00080000)
  SetLayeredWindowAttributes_(WinId,iColor,Trandeg,$03)
EndProcedure

OpenWindow(0, 0, 0, 390, 130, "", #PB_Window_SystemMenu)

SetWindowTrans(0,150, 0)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: SetWindowsTransparency

Publié : jeu. 13/avr./2017 10:20
par celtic88
merci google translate:P


Fonction :

SetLayeredWindowAttributes_(hwnd.i,
crKey.l,
bAlpha.b,
dwFlags.l)

Définit la clé de couleur d'opacité et de transparence d'une fenêtre en couches.

Paramètres

Hwnd [in]
Type: integer
Une poignée sur la fenêtre en couches. Une fenêtre en couches est créée en spécifiant #WS_EX_LAYERED lors de la création de la fenêtre avec la fonction CreateWindowEx_() ou en configurant #WS_EX_LAYERED via SetWindowLong_() une fois la fenêtre créée.
Windows 8: le style #WS_EX_LAYERED est pris en charge pour les fenêtres principales et les fenêtres enfant. Les versions précédentes de Windows supportent #WS_EX_LAYERED uniquement pour les fenêtres de niveau supérieur.

CrKey [in]
Type: Long
spécifie la clé de couleur de transparence à utiliser lors de la composition de la fenêtre en couches. Tous les pixels peints par la fenêtre de cette couleur seront transparents. Pour générer un couleur, utilisez la "RGB(0,0,0)".

BAlpha [in]
Type: BYTE
Valeur alpha utilisée pour décrire l'opacité de la fenêtre en couches. Lorsque bAlpha est 0, la fenêtre est complètement transparente. Lorsque bAlpha est 255, la fenêtre est opaque.

DwFlags [in]
Type: Long
Une action à prendre. Ce paramètre peut être une ou plusieurs des valeurs suivantes:

#LWA_ALPHA($00000002):Utilisez bAlpha pour déterminer l'opacité de la fenêtre en couches.
#LWA_COLORKEY($00000001):Utilisez crKey comme couleur de transparence.

Valeur de retour
Type: BOOL : 0=échoue|1=Ok


Exemple 1

Code : Tout sélectionner

Procedure Window_Set_extended_style(Hwnd.i, IExStyle.l)
  SetWindowLong_(Hwnd,#GWL_EXSTYLE,GetWindowLong_(Hwnd,#GWL_EXSTYLE)|IExStyle)
EndProcedure

OpenWindow(0, 0, 0, 500, 300, "SetLayeredWindowAttributes Exemple", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SetWindowColor(0, RGB(0,0,255))
ButtonGadget(1,200,150,80,20,"Test")
TextGadget(3, 45, 105, 200, 100, "Exemple")
SetGadgetColor(3, #PB_Gadget_BackColor,RGB(0,0,255))
LoadFont(3, "Arial", 28)
SetGadgetFont(3, FontID(3)) 

 ContainerGadget(2, 0, 0, 100, 100)
 SetGadgetColor(2, #PB_Gadget_BackColor,RGB(255,0,0))
 
Window_Set_extended_style(WindowID(0), #WS_EX_LAYERED)

SetLayeredWindowAttributes_(WindowID(0),
  RGB(0,0,255),
  100,
  #LWA_ALPHA|#LWA_COLORKEY)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Exemple 2

Code : Tout sélectionner

Procedure Window_Set_extended_style(Hwnd.i, IExStyle.l)
  SetWindowLong_(Hwnd,#GWL_EXSTYLE,GetWindowLong_(Hwnd,#GWL_EXSTYLE)|IExStyle)
EndProcedure

OpenWindow(0, 0, 0, 500, 300, "SetLayeredWindowAttributes Exemple", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SetWindowColor(0, RGB(0,0,255))
ButtonGadget(1,200,150,80,20,"Test")
TextGadget(3, 45, 105, 200, 100, "Exemple")
SetGadgetColor(3, #PB_Gadget_BackColor,RGB(0,0,255))
LoadFont(3, "Arial", 28)
SetGadgetFont(3, FontID(3)) 

 ContainerGadget(2, 0, 0, 100, 100)
 SetGadgetColor(2, #PB_Gadget_BackColor,RGB(255,0,0))
 
Window_Set_extended_style(WindowID(0), #WS_EX_LAYERED)

SetLayeredWindowAttributes_(WindowID(0),
  0,
  100,
  #LWA_ALPHA)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Exemple 3

Code : Tout sélectionner

Procedure Window_Set_extended_style(Hwnd.i, IExStyle.l)
  SetWindowLong_(Hwnd,#GWL_EXSTYLE,GetWindowLong_(Hwnd,#GWL_EXSTYLE)|IExStyle)
EndProcedure

OpenWindow(0, 0, 0, 500, 300, "SetLayeredWindowAttributes Exemple", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SetWindowColor(0, RGB(0,0,255))
ButtonGadget(1,200,150,80,20,"Test")
TextGadget(3, 45, 105, 200, 100, "Exemple")
SetGadgetColor(3, #PB_Gadget_BackColor,RGB(0,0,255))
LoadFont(3, "Arial", 28)
SetGadgetFont(3, FontID(3)) 

 ContainerGadget(2, 0, 0, 100, 100)
 SetGadgetColor(2, #PB_Gadget_BackColor,RGB(255,0,0))
 
Window_Set_extended_style(WindowID(0), #WS_EX_LAYERED)

SetLayeredWindowAttributes_(WindowID(0),
  RGB(0,0,255),
  0,
  #LWA_COLORKEY)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Exemple 4

Code : Tout sélectionner

Procedure Window_SetTransparency(WinHwnd.i,Op.w)
  Protected WinExtendedStyle.l = GetWindowLong_(WinHwnd,#GWL_EXSTYLE)
  If Not WinExtendedStyle &#WS_EX_LAYERED
    SetWindowLong_(WinHwnd,#GWL_EXSTYLE,WinExtendedStyle|#WS_EX_LAYERED)
  EndIf
  SetLayeredWindowAttributes_(WinHwnd,
                              0,
                              Op,
                              #LWA_ALPHA)
EndProcedure

Procedure TrackBarEv()
  Protected EvWin  =EventWindow()
  Protected EvGadget=EventGadget()
  Protected WinHwnd = WindowID(EvWin)
  Protected GetTrackVal.w = GetGadgetState(EvGadget)
  Window_SetTransparency(WinHwnd,GetTrackVal)
EndProcedure

OpenWindow(0, 0, 0, 470, 210, "SetLayeredWindowAttributes Exemple", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TrackBarGadget(1, 100, 110, 230, 25, 0, 255)
BindGadgetEvent(1,@TrackBarEv(),#PB_EventType_LeftClick  )
SetGadgetState(1,255)


Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: SetWindowsTransparency

Publié : jeu. 13/avr./2017 11:00
par Micoute
Merci celtic88 pour le partage.

Re: SetWindowsTransparency

Publié : jeu. 13/avr./2017 11:03
par JohnJohnsonSHERMAN
Pour ma part je procéde comme ceci :

Code : Tout sélectionner

Procedure SetWindowTransparency(Window, Alpha)
  SetWindowLong_(WindowID(Window),#GWL_EXSTYLE, #WS_EX_LAYERED)
  SetLayeredWindowAttributes_(WindowID(Window), RGB(80,80,80),Alpha,#LWA_ALPHA)
EndProcedure
Les constantes sont les suivantes :

Code : Tout sélectionner

#GWL_EXSTYLE = -20
#GWL_STYLE = -16
#WS_EX_LAYERED = 524288
#LWA_ALPHA = 2
#LWA_COLORKEY = 1
      • Remarque : on peut aussi utiliser SetLayeredWindowAttributes_ avec le paramétre #LWA_COLORKEY, auquel cas, la couleur placée en 2éme paramétre (RGB(80,80,80) dans mon exemple) sera "blittée" avec l'arriére plan et rendue transparente, permettant de faire des "trous" dans la fenêtre, par exemple pour faire des fenêtres avec des formes (horloges rondes, formes arrondies, ou même des boutons flottant au milieu de nulle part...). De plus il est possible d'utiliser a la fois #LWA_COLORKEY et #LWA_ALPHA, à l'aide de l'opérateur d'addition binaire "|", et ainsi faire des fenêtres avec des formes tout en étant transparentes :)
Voici un petit exemple montrant les possibilités de SetLayeredWindowAttributes_ :

Code : Tout sélectionner

#GWL_EXSTYLE = -20
#GWL_STYLE = -16
#WS_EX_LAYERED = 524288
#LWA_ALPHA = 2
#LWA_COLORKEY = 1

Prototype.i Proto_SetWindowLong_(hWnd,nIndex.i, NewLong.l)
Prototype.b Proto_SetLayeredWindowAttributes_(hWnd,ColorKey,Alpha.b,Flags.w)

OpenLibrary(1,"user32.dll")
Global SetWindowLong_.Proto_SetWindowLong_ = GetFunction(1,"SetWindowLongW") ;on charge la fonction
Global SetLayeredWindowAttributes_.Proto_SetLayeredWindowAttributes_ = GetFunction(1,"SetLayeredWindowAttributes")
CloseLibrary(1)

Procedure SetWindowTransparency(Window, Alpha)
  SetWindowLong_(WindowID(Window),#GWL_EXSTYLE, #WS_EX_LAYERED)
  SetLayeredWindowAttributes_(WindowID(Window), RGB(237,28,36),Alpha,#LWA_ALPHA | #LWA_COLORKEY)
EndProcedure

UsePNGImageDecoder()
LoadImage(1,"M4_Sherman_BlitFilter.png") ;<<<<Vérifiez ici que vous avez soit le png, soit le bmp

width = ImageWidth(1)
height = ImageHeight(1)

OpenWindow(1,0,0,width,height,"Exemple de fenêtre avec colorkey",#PB_Window_ScreenCentered | #PB_Window_BorderLess)
CanvasGadget(1,0,0,width,height)

SetGadgetAttribute(1,#PB_Canvas_Image,ImageID(1))
GadgetToolTip(1,"Glissez pour déplacer ce Sherman")

SetWindowTransparency(1,220)

Repeat
  event = WaitWindowEvent()
  
  If EventGadget() = 1
    Select EventType()
      Case #PB_EventType_MouseEnter
        SetWindowTransparency(1,255)
        
      Case #PB_EventType_MouseLeave
        SetWindowTransparency(1,128)
        
      Case #PB_EventType_LeftButtonDown
        isPressed = 1
        init_x = WindowMouseX(1)
        init_y = WindowMouseY(1)
        
      Case #PB_EventType_LeftButtonUp
        isPressed = 0
        
      Case #PB_EventType_MouseMove
        If isPressed
;           delta_x = DesktopMouseX() - init_x
;           delta_y = DesktopMouseY() - init_y
          ResizeWindow(1, DesktopMouseX()-init_x, DesktopMouseY()-init_y, #PB_Ignore, #PB_Ignore)
        EndIf 
        
    EndSelect
  EndIf
  
Until event = #PB_Event_CloseWindow


L'image à utiliser est celle-ci :
Image
M4_Sherman_BlitFilter.bmp

Edit : cliquez sur l'image pour accéder au BMP (lien Google Docs), en PNG ca ne fonctionnera probablement pas
Edit 2 : En fait si ca marche en Png aussi :)
Voila :)

Re: SetWindowsTransparency

Publié : jeu. 13/avr./2017 11:46
par Ar-S
Ton image est une PNG une fois uppée, je te laisse modifier ton post en conséquence ;)

Re: SetWindowsTransparency

Publié : jeu. 13/avr./2017 11:59
par JohnJohnsonSHERMAN
Ar-S a écrit :Ton image est une PNG une fois uppée, je te laisse modifier ton post en conséquence ;)
Gnaaaaaa :(

Lien Google docs : https://drive.google.com/open?id=0B-Wg3 ... GJBaEgxLWs
Précédent post mis à jour : lien intégré a l'image

Merci Ar-S

Re: SetWindowsTransparency

Publié : jeu. 13/avr./2017 13:41
par Shadow
Merci à tous pour vos réponses et exemples très explicite :)

Re: SetWindowsTransparency

Publié : jeu. 13/avr./2017 13:46
par JohnJohnsonSHERMAN
@ Shadow : De rien :)

@ Tous : Correctif : la transparence marche aussi en utilisant une image png :) Pas besoin d'avoir un bmp pour que #LWA_COLORKEY fonctionne :) Vous pouvez donc directement utiliser l'image fournie

Re: SetWindowsTransparency

Publié : jeu. 13/avr./2017 14:07
par Ar-S
Oui je parlais de modifier le code :mrgreen: j'aurai du préciser ^^

Re: SetWindowsTransparency

Publié : jeu. 13/avr./2017 14:46
par JohnJohnsonSHERMAN
Ce qui est fait du coup :)

Re: SetWindowsTransparency

Publié : jeu. 13/avr./2017 21:13
par venom
Sympa ces codes. Merci

Par contre c'est possible de rendre la fenêtre transparente mais ni la barre de titre ni les gadget a l’intérieur ?
Pour par exemple avoir une interface avec des gadget "effet flottant" :lol:
Une idée comme sa qui me traverse l'esprit. Mais il me semble avoir déjà vu ça sur le forum mais pas sur





@++