Savoir si une fenetre est visible dans la barre de tache ( .net )

Contenu du snippet

cette classe ( inachevée ) contient des routines pour tester si une fenetre est listée dans la taskbar ( et le alt-tab ) et pour recupérer l'icone associée a cette fenetre ( pour coder une fausse taskbar ! )

LA CLASSE EST INACHEVEE ( pas de try catch, code imparfait ) c'est un départ pour ceux qui ont comme moi lutté pour trouver les critères ( styles ) qui ne sont pas complètement ce que la msdn nous dit !

Afin de vous éviter de chercher pendant des heures une logique dans spy++ , je publie cette source, ne ralez pas si le code est crade, c'est à vous de mettre en forme, c'est une matière première !

Source / Exemple :


#Region "Copyright"
'************************************************************************
'* Auteur                   * Proviste / Coq                            *
'* Date Création            * 07/12/04                                  *
'* Derniere Modification    * 25/12/04                                  *
'************************************************************************
'*              *                                                       *
'* Module       * clsHwndInfos                                          *
'*              *                                                       *
'* Description  * Operation et information sur les hwnd                 *
'*              *                                                       *
'*              *                                                       *
'************************************************************************
#End Region

#Region "Imports"
'-_-'
Imports System.Runtime.InteropServices
#End Region

Public Class clsHwndInfo
#Region "   Declaration"

#Region "        Enum Window Style :o"
    Private Enum enWindowStyles
        WS_BORDER = &H800000
        WS_CAPTION = &HC00000
        WS_CHILD = &H40000000
        WS_CLIPCHILDREN = &H2000000
        WS_CLIPSIBLINGS = &H4000000
        WS_DISABLED = &H8000000
        WS_DLGFRAME = &H400000
        WS_EX_ACCEPTFILES = &H10&
        WS_EX_DLGMODALFRAME = &H1&
        WS_EX_NOPARENTNOTIFY = &H4&
        WS_EX_TOPMOST = &H8&
        WS_EX_TRANSPARENT = &H20&
        WS_EX_TOOLWINDOW = &H80&
        WS_GROUP = &H20000
        WS_HSCROLL = &H100000
        WS_MAXIMIZE = &H1000000
        WS_MAXIMIZEBOX = &H10000
        WS_MINIMIZE = &H20000000
        WS_MINIMIZEBOX = &H20000
        WS_OVERLAPPED = &H0&
        WS_POPUP = &H80000000
        WS_SYSMENU = &H80000
        WS_TABSTOP = &H10000
        WS_THICKFRAME = &H40000
        WS_VISIBLE = &H10000000
        WS_VSCROLL = &H200000
        '\\ New from 95/NT4 onwards
        WS_EX_MDICHILD = &H40
        WS_EX_WINDOWEDGE = &H100
        WS_EX_CLIENTEDGE = &H200
        WS_EX_CONTEXTHELP = &H400
        WS_EX_RIGHT = &H1000
        WS_EX_LEFT = &H0
        WS_EX_RTLREADING = &H2000
        WS_EX_LTRREADING = &H0
        WS_EX_LEFTSCROLLBAR = &H4000
        WS_EX_RIGHTSCROLLBAR = &H0
        WS_EX_CONTROLPARENT = &H10000
        WS_EX_STATICEDGE = &H20000
        WS_EX_APPWINDOW = &H40000
        WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE)
        WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE Or _
                                 WS_EX_TOOLWINDOW Or WS_EX_TOPMOST)
    End Enum
#End Region

#Region "   GetWindowLong Diverses Declaration"

    <DllImport("user32.dll", CharSet:=CharSet.Ansi, EntryPoint:="GetWindowLong")> _
   Private Shared Function GetWindowLong32(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Ansi, EntryPoint:="GetWindowLong")> _
    Private Shared Function GetWindowLongPtr(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As IntPtr
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Ansi, EntryPoint:="GetWindowLong")> _
    Private Shared Function GetWindowLong64(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Long
    End Function

#End Region

#Region "   GetClassLong Diverses Declaration"

    <DllImport("user32.dll", CharSet:=CharSet.Ansi, EntryPoint:="GetClassLong", SetLastError:=True)> _
    Private Shared Function GetClassLong32(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Ansi, EntryPoint:="GetClassLong", SetLastError:=True)> _
    Private Shared Function GetClassLongPtr(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As IntPtr
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Ansi, EntryPoint:="GetClassLong", SetLastError:=True)> _
    Private Shared Function GetClassLong64(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Long
    End Function

#End Region

    <DllImport("user32.dll", CharSet:=CharSet.Ansi, EntryPoint:="SendMessage")> _
    Private Shared Function SendMessagePtr(ByVal hwnd As IntPtr, _
        ByVal wMsg As Int32, _
        ByVal wParam As Integer, _
        ByVal lParam As Integer) As IntPtr
    End Function

    '-sendmessage
    Private Declare Function SendMessage _
        Lib "user32" _
        Alias "SendMessageA" _
       (ByVal hwnd As IntPtr, _
        ByVal wMsg As Int32, _
        ByVal wParam As Integer, _
    ByVal lParam As System.Text.StringBuilder) As Integer

    Private Declare Function SendMessage _
        Lib "user32" _
        Alias "SendMessageA" _
        (ByVal hwnd As IntPtr, _
        ByVal wMsg As Int32, _
        ByVal wParam As Integer, _
    ByVal lParam As Integer) As Integer

    Private Const WM_SETTEXT = &HC
    Private Const WM_GETTEXT = &HD
    Private Const WM_GETTEXTLENGTH = &HE
    Private Const WM_SETICON = &H80
    Private Const WM_GETICON = &H7F
    Private Const ICON_SMALL = 0
    Private Const ICON_BIG = 1
    Private Const Icon_BIG_XP = 2

    Private Enum enGetWindowLong
        GWL_EXSTYLE = (-20)
        GWL_HINSTANCE = (-6)
        GWL_HWNDPARENT = (-8)
        GWL_ID = (-12)
        GWL_STYLE = (-16)
        GWL_USERDATA = (-21)
        GWL_WNDPROC = (-4)
    End Enum

    Private Enum enGetWindowLongPtr
        GCLP_MENUNAME = (-8)
        GCLP_HBRBACKGROUND = (-10)
        GCLP_HCURSOR = (-12)
        GCLP_HICON = (-14)
        GCLP_HMODULE = (-16)
        GCLP_WNDPROC = (-24)
        GCLP_HICONSM = (-34)
    End Enum

    Private Const GCL_CBCLSEXTRA = -20
    Private Const GCL_CBWNDEXTRA = -18
    Private Const GCL_HCURSOR = -12
    Private Const GCL_HICON = -14
    Private Const GCL_HMODULE = -16
    Private Const GCL_MENUNAME = -8
    Private Const GCL_STYLE = -26
    Private Const GCL_WNDPROC = -24
    Private Const GCL_HICONSM = -34

    Private myHwndPtr As IntPtr

#End Region

#Region "   Procédures"
#Region "       Publiques"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Crée une nouvelle instance de la classe.
    ''' </summary>
    ''' <param name="hwndptr">Hwndptr qui sera traité</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	Proviste	28/12/2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub New(ByVal hwndptr As IntPtr)
        NewInst(hwndptr)
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Crée une nouvelle instance de la classe
    ''' </summary>
    ''' <param name="hwnd"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	Proviste	28/12/2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub New(ByVal hwnd As Long)
        NewInst(New IntPtr(hwnd))
    End Sub

#End Region

#Region "       Privées"

    Private Sub NewInst(ByVal hwndptr As IntPtr)
        myHwndPtr = hwndptr
    End Sub

#End Region

#End Region

#Region "   Property"
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Cette propriété renvoie ou définie le texte associé à un hwnd.
    ''' </summary>
    ''' <value>Valeur de type string</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	Proviste	28/12/2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Property WindowText() As String
        Get
            Dim wdsTxtBuilt As System.Text.StringBuilder
            Dim wdsTxtLength As Integer

            wdsTxtLength = SendMessage(myHwndPtr, WM_GETTEXTLENGTH, 0, 0) + 1

            wdsTxtBuilt = New System.text.StringBuilder(Chr(0), wdsTxtLength)

            SendMessage(myHwndPtr, WM_GETTEXT, wdsTxtLength, wdsTxtBuilt)

            Return wdsTxtBuilt.ToString
        End Get
        Set(ByVal Value As String)
            SendMessage(myHwndPtr, WM_SETTEXT, Value.Length, New System.Text.StringBuilder(Value))
        End Set

    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Retourne une valeur booléene pour savoir si l'objet associée au hwnd ( à priori une feuille ) est affichée dans la barre des tâche.
    ''' </summary>
    ''' <value>True : la form associée au hwnd passé en paramètre est affichée dans la barre des tâches</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	Proviste 01/12/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public ReadOnly Property ShownInTaskBar() As Boolean
        Get

            If myHwndPtr.Size = 8 Then 'win64
                Dim wStyles As Long
                wStyles = GetWindowLong64(myHwndPtr, enGetWindowLong.GWL_STYLE)
                If (wStyles And enWindowStyles.WS_VISIBLE) = enWindowStyles.WS_VISIBLE Then
                    Dim wStylesEx As Long
                    wStylesEx = GetWindowLong32(myHwndPtr, enGetWindowLong.GWL_EXSTYLE)
                    If (wStylesEx And enWindowStyles.WS_EX_TOOLWINDOW) = enWindowStyles.WS_EX_TOOLWINDOW Then
                        Return False
                    Else
                        Return ((wStyles And enWindowStyles.WS_EX_APPWINDOW) = enWindowStyles.WS_EX_APPWINDOW) OrElse _
                           ((wStyles And enWindowStyles.WS_POPUP) = enWindowStyles.WS_POPUP)
                    End If
                Else
                    Return False
                End If
            Else
                Dim wStyles As Integer
                wStyles = GetWindowLong32(myHwndPtr, enGetWindowLong.GWL_STYLE)
                If (wStyles And enWindowStyles.WS_VISIBLE) = enWindowStyles.WS_VISIBLE Then
                    Dim wStylesEx As Integer
                    wStylesEx = GetWindowLong32(myHwndPtr, enGetWindowLong.GWL_EXSTYLE)
                    If (wStylesEx And enWindowStyles.WS_EX_TOOLWINDOW) = enWindowStyles.WS_EX_TOOLWINDOW Then
                        Return False
                    Else
                        Return ((wStyles And enWindowStyles.WS_EX_APPWINDOW) = enWindowStyles.WS_EX_APPWINDOW) OrElse _
                           ((wStyles And enWindowStyles.WS_POPUP) = enWindowStyles.WS_POPUP)
                    End If
                Else
                    Return False
                End If
            End If
        End Get
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Retourne une valeur booléene pour savoir si l'objet associé au hwnd est visible ( indépendement du fait qu'il soit caché ou non ) 
    ''' </summary>
    ''' <value>Renvoie True si l'objet est visible</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	Proviste	01/12/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public ReadOnly Property Visible() As Boolean
        Get
            If myHwndPtr.Size = 8 Then 'win64
                Return ((GetWindowLong64(myHwndPtr, enGetWindowLong.GWL_EXSTYLE) And enWindowStyles.WS_VISIBLE) = enWindowStyles.WS_VISIBLE)
            Else
                Return ((GetWindowLong32(myHwndPtr, enGetWindowLong.GWL_EXSTYLE) And enWindowStyles.WS_VISIBLE) = enWindowStyles.WS_VISIBLE)
            End If
        End Get
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Renvoie ou définie le handle de l'icone associé à la fenetre
    ''' </summary>
    ''' <value></value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	Proviste	01/12/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public ReadOnly Property hIcon() As IntPtr
        Get
            Dim hIcn As IntPtr

            hIcn = SendMessagePtr(myHwndPtr, WM_GETICON, ICON_BIG, 0)
            If hIcn.ToString = "0" Then
                Return GetClassLongPtr(myHwndPtr, GCL_HICON)
            Else
                Return hIcn
            End If

        End Get
        '        Set(ByVal Value As IntPtr)
        '           'MsgBox(SetClassLong(myHwndPtr, GCL_HICON, Value))
        '      End Set
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Renvoie ou définie le handle de l'icone 16*16 associé à la fenetre
    ''' </summary>
    ''' <value></value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	Proviste	01/12/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public ReadOnly Property hIconSmall() As IntPtr
        Get
            Dim hIcn As IntPtr

            hIcn = SendMessagePtr(myHwndPtr, WM_GETICON, ICON_SMALL, 0)
            If hIcn.Equals(IntPtr.Zero) Then
                Return GetClassLongPtr(myHwndPtr, GCL_HICONSM)
            Else
                Return hIcn
            End If

        End Get
        '        Set(ByVal Value As IntPtr)
        '
        '       End Set 
    End Property
#End Region
End Class

Conclusion :


Merci au vieux coq pour son aide :D

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.