Partage
  • Partager sur Facebook
  • Partager sur Twitter

[VB.NET]API Windows Win32

opacité fenetre

    3 juillet 2011 à 20:36:26

    Bonjour a tous, cela fais maintenant plusieurs heures que je cherche sur la toile mais je ne trouve pas mon bonheur. Je voudrais créer un programme qui gère la visibilité des fenêtres et programmes ouvert. JE m'explique, imaginons que j'ai par exemple ouvert Mozilla Firefox et une fenêtre windows quelconque, je voudrais savoir comment faire pour en faire disparaître une en changeant sa transparence ou son opacité. Je sais comment lister les fenêtres ouvertes sur l'ordinateur et les mettre dans une listbox. Ce que je ne sais pas faire c'est changer l'opacité de la fenêtre sélectionnée dans la listbox..
    Suis-je clair ? J'ai longuement cherché et je suis souvent tombé sur l'API WIN32 de windows mais je ne sais pas quoi faire d'autre je suis bloqué. SI vous voulez mon code pour lister n’hésitez pas ^^

    Merci pour tout
    • Partager sur Facebook
    • Partager sur Twitter
      4 juillet 2011 à 9:55:28

      Quand tu récupère les fenêtre ouvertes, c'est de quel type? Form?
      • Partager sur Facebook
      • Partager sur Twitter
        5 juillet 2011 à 0:17:24

        Merci de ta reponse, je te passe mon code car je ne comprend pas ta question:
        Imports System.Runtime.InteropServices
        Imports System
        Imports System.Drawing
        Imports System.Windows.Forms
        Imports System.Text
        Imports user32.dll
        
        Public Class Form1
        
            <DllImport("user32.dll")> _
            Private Shared Function IsWindowVisible(ByVal hWnd As IntPtr) As Boolean
            End Function
        
            Private Delegate Function EnumWindowsProc(ByVal hWnd As IntPtr, _
             ByVal lParam As IntPtr) As Int32
        
            <DllImport("user32.dll")> _
            Private Shared Function EnumWindows(ByVal lpEnumFunc As EnumWindowsProc, _
            ByVal lParam As IntPtr) As Boolean
            End Function
        
            <DllImport("user32.dll")> _
            Private Shared Function GetWindowTextLength(ByVal hWnd As IntPtr) As Int32
            End Function
        
            <DllImport("user32.dll")> _
            Private Shared Function GetWindowText(ByVal hWnd As IntPtr, <Out()> _
            ByVal lpString As StringBuilder, _
            ByVal cch As Int32) As Int32
            End Function
        
            Private Function EnumTitleDescription(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Int32
                If (IsWindowVisible(hWnd)) Then
                    Dim Length As Int32 = GetWindowTextLength(hWnd)
                    Dim sb As StringBuilder = New StringBuilder(Length + 1)
                    Dim Result As Int32 = GetWindowText(hWnd, sb, sb.Capacity)
        
                    If (Result > 0) Then
                        ListBox1.Items.Add(sb.ToString)
                    End If
                End If
                Return 1
            End Function
        
            Private Sub Button1Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                Dim cb As EnumWindowsProc = AddressOf EnumTitleDescription
                Call EnumWindows(cb, IntPtr.Zero)
            End Sub
        
            Private Sub ListBox1SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
                MessageBox.Show(ListBox1.SelectedItem, "Info")
            End Sub
        
            Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        
               Dim ProcessList As System.Diagnostics.Process()
                Dim Proc As System.Diagnostics.Process
                Dim Message As New StringBuilder
        
                ProcessList = System.Diagnostics.Process.GetProcesses()
                For Each Proc In ProcessList
                    Message.AppendLine(Proc.ProcessName)
                Next
        
                MessageBox.Show(Message.ToString)
        
            End Sub
            Private Const LWA_ALPHA = &H2
            Private Const GWL_EXSTYLE = (-20)
            Private Const WS_EX_LAYERED = &H80000
        
            'déclaration des apis
        
            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 GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex 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 Declare Function GetForegroundWindow Lib "user32" () As Long
            Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
            Dim hWd As Long
            Dim opacite As Integer
            Dim up As Boolean
        
            Sub Main() 'boucle de début du programme(dans projet, option, objet de démarage mettre main
                up = False
                opacite = 255
                Do While True 'boucle a l'infinie
                    If up = False Then
                        If opacite <= 0 Then 'controle pour que si l'opacité est a 0, on remonte
                            up = True
                            opacite = opacite + 1
                        Else
                            opacite = opacite - 1
                        End If
                    Else
                        If opacite >= 255 Then
                            up = False
                            opacite = opacite - 1
                        Else
                            opacite = opacite + 1
                        End If
                    End If
                    hWd = GetForegroundWindow 'on récupère le handle de la fenêtre active
                    DoEvents()
                    Call SetWindowOpacity(hWd, opacite) 'on mets l'opacité a celle ci
                    Sleep(10) ' une petite pause pour pas que ça aille trop vite tout de même 
                Loop
            End Sub
        
            Public Function SetWindowOpacity(ByVal hWnd As Long, ByVal Opacité As Integer) As Boolean ' fonction pour rendre une fenêtre
                ' transparente, trouvée ici http://www.vbfrance.com/article.aspx?ID=8961
        
                Dim Ret As Long
        
                On Error GoTo PROC_ERREUR
        
                Ret = GetWindowLong(hWnd, GWL_EXSTYLE)
        
                Opacité = Abs(Opacité)
                If Opacité > 255 Then Opacité = 255
        
                If Opacité = 255 Then
                    Ret = Ret And Not WS_EX_LAYERED
                    SetWindowLong(hWnd, GWL_EXSTYLE, Ret)
                    SetLayeredWindowAttributes(hWnd, 0, 0, LWA_ALPHA)
                Else
                    Ret = Ret Or WS_EX_LAYERED
                    SetWindowLong(hWnd, GWL_EXSTYLE, Ret)
                    SetLayeredWindowAttributes(hWnd, 0, Opacité, LWA_ALPHA)
                End If
        
                If Err Then GoTo PROC_ERREUR
        
                SetWindowOpacity = True
                Exit Function
        
        PROC_ERREUR:
                SetWindowOpacity = False
        
            End Function
        End Class
        

        Le bouton 1 affiche les fenêtres ouvertes dans une listbox et le bouton 2 tous les processus dans une msgbox...

        Merci encore ^^
        • Partager sur Facebook
        • Partager sur Twitter
          5 juillet 2011 à 9:00:24

          Pour la transparence, essaye ceci

          Private Const WS_EX_TRANSPARENT = &H00000020
          [...]
          extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE)
          SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT)
          
          • Partager sur Facebook
          • Partager sur Twitter
            5 juillet 2011 à 18:27:02

            Merci beaucoup mais comment utilise-t-on ton code ?
            • Partager sur Facebook
            • Partager sur Twitter

            [VB.NET]API Windows Win32

            × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
            × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
            • Editeur
            • Markdown