cs_fauve
Messages postés661Date d'inscriptionvendredi 2 décembre 2005StatutMembreDernière intervention23 mars 2011
-
14 mars 2007 à 21:40
cs_fauve
Messages postés661Date d'inscriptionvendredi 2 décembre 2005StatutMembreDernière intervention23 mars 2011
-
15 mars 2007 à 17:23
Salut,
Voilà mon soucis, j'ai reussi à faire un petit programme qui me liste les noms des fenêtres ouvertes et qui apparaissent dans la barre des tâches. Mais en réalité en plus d'afficher le nom de ces fenêtres, il me liste plein d'autres choses. Je mets le code de mon programme en dessous :
Public Class Form1
Const WM_GETTEXT As Long = &HD
Const WM_GETTEXTLENGTH As Long = &HE
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Private Declare Function EnumWindows Lib "user32" (ByVal x As MyDelegateCallBack, ByVal y As Integer) As Integer
Private Declare Auto Function SendMessageA Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr
Public Delegate Function MyDelegateCallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
Private Declare Auto Function SendMessageString Lib "user32.dll" Alias "SendMessage" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wparam As Integer, ByVal lparam As System.Text.StringBuilder) As IntPtr
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim del As MyDelegateCallBack
del = New MyDelegateCallBack(AddressOf EnumOutput)
EnumWindows(del, 0)
EnumWindows(AddressOf EnumOutput, 0)
End Sub
Public Function EnumOutput(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
Dim texte As String
texte = GetWindowText(hwnd)
If texte <> "" Then
MsgBox(texte)
End If
Return True
End Function
Function GetWindowText(ByVal WindowHandle As IntPtr) As String
Dim ptrRet As IntPtr
Dim ptrLength As IntPtr
'get length for buffer...
ptrLength = SendMessageA(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)
'create buffer for return value...
Dim sb As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)
Tiens pour faire plus simple avec le framework (désolé, j'avais pas pensé avant, j'étais parti sur les apis)
For Each Process As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
If Not Process.MainWindowHandle = System.IntPtr.Zero Then
If Not String.IsNullOrEmpty(Process.MainWindowTitle) Then
System.Windows.Forms.MessageBox.Show(Process.MainWindowTitle)
End If
End If
Next,
MadM@tt
Messages postés2167Date d'inscriptionmardi 11 novembre 2003StatutMembreDernière intervention16 juillet 20091 14 mars 2007 à 23:09
Ah mince j'ai jamais vu ça, est ce que à tout hasard "IsWindowVisible" serait déjà une fonction qui existe dans VB.net ?
As tu bien declaré l'api ?
Faudrait que quelqu'un qui sache faire du .net passe par la
MadM@tt
Messages postés2167Date d'inscriptionmardi 11 novembre 2003StatutMembreDernière intervention16 juillet 20091 14 mars 2007 à 23:33
ça a l'air bon, attend quelques jours et si personne n'a répondu, poste une nouvelle question avec la capture d'écran que tu as mis plus haut, généralement tu as plus de chance d'avoir des réponses quand personne ne t'as encore répondu.
cs_fauve
Messages postés661Date d'inscriptionvendredi 2 décembre 2005StatutMembreDernière intervention23 mars 20118 15 mars 2007 à 10:08
Salut,
J'ai aussi essayé de remplacer le long par IntPtr mais ca ne fonctionne
toujours pas. Il me mets toujours là même erreur (voir ci-dessus)
Est ce que le fait de mettre Shared à la place de Declare (comme tu l'a
fait) peut éventuellement résoudre ce problème ou en revanche ca ne
changeras rien ?
cs_fauve
Messages postés661Date d'inscriptionvendredi 2 décembre 2005StatutMembreDernière intervention23 mars 20118 15 mars 2007 à 14:51
Salut,
Je tiens à te remercier Charles Racaud en suivant ton conseil j'ai pu enfin compiler mon projet et voir ce que ca a donné. Mais j'ai un autre souci qui vient d'apparaitre, en effet, j'ai la fonction IsTask (que m'a proposé MadM@tt qui d'ailleurs je tiens aussi à remercier) qui me permet de savoir si la fenêtre trouvée apparait dans la barre des tâches (Si c'est le cas IsTask rend True sinon False), alors j'ai testé la condition si IsTask a rendue True,
mais là, la MsgBox n'apparait plus, je suppose que IsTask rends toujours False car dés que je supprimes la condition sur IsTask, ma MsgBox réapparait. Je mets le code pour que vous puissiez voir pourquoi ca fonctionne pas (et merci pour tout) :
Public Class Form1
Const WM_GETTEXT As Long = &HD
Const WM_GETTEXTLENGTH As Long = &HE
Const GWL_HWNDPARENT As Long = -8
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
Private Declare Function EnumWindows Lib "user32" (ByVal x As MyDelegateCallBack, ByVal y As Integer) As Integer
Private Declare Auto Function SendMessageA Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr
Public Delegate Function MyDelegateCallBack(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
Private Declare Auto Function SendMessageString Lib "user32" Alias "SendMessage" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wparam As Integer, ByVal lparam As System.Text.StringBuilder) As IntPtr
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As IntPtr) As Boolean
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As IntPtr) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Function GetWindowClass(ByVal hwnd As Long) As String
' On prépare la chaine de caractère avec 255 espaces
GetWindowClass = Space(255)
' On récuère le nom de la classe
GetClassName(hwnd, GetWindowClass, 255)
' On efface les caractères vides
GetWindowClass = Microsoft.VisualBasic.Left(GetWindowClass, InStr(GetWindowClass, vbNullChar) - 1)
End Function
Public Function EnumOutput(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
Dim texte As String
texte = GetWindowText(hwnd)
If IsTask(hwnd) = True Then
MsgBox(texte)
End If
Return True
End Function
Public Function IsTask(ByVal hwnd As IntPtr) As Boolean
IsTask = False
'le fenêtre doit être visible
If IsWindowVisible(hwnd) = True And (GetWindowLong(hwnd, GWL_HWNDPARENT) = 0) And Not (GetWindowTextLength(hwnd)) Then
' Vérifie que ce n'est pas le TaskManager
If GetWindowClass(hwnd) <> "Progman" Then
IsTask = True
End If
End If
End Function
Function GetWindowText(ByVal WindowHandle As IntPtr) As String
Dim ptrRet As IntPtr
Dim ptrLength As IntPtr
'get length for buffer...
ptrLength = SendMessageA(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)
'create buffer for return value...
Dim sb As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)
'get return value...
Return sb.ToString
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim del As MyDelegateCallBack
del = New MyDelegateCallBack(AddressOf EnumOutput)
EnumWindows(del, 1)
EnumWindows(AddressOf EnumOutput, 1)
End Sub
Tes déclarations ne sont toujours pas bonnes
Tu as oublier de remplacer des long
Et fait attention, les IntPtr sont que pour les handls, les autre, c'est des int32.
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
Private Declare Function EnumWindows Lib "user32" (ByVal x As MyDelegateCallBack, ByVal y As Integer) As Integer
Private Declare Auto Function SendMessageA Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByRef lParam As Integer) As Integer
Public Delegate Function MyDelegateCallBack(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
Private Declare Auto Function SendMessageString Lib "user32" Alias "SendMessage" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wparam As Integer, ByVal lparam As System.Text.StringBuilder) As Integer
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As IntPtr) As Boolean
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As IntPtr) As Integer
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer,
cs_fauve
Messages postés661Date d'inscriptionvendredi 2 décembre 2005StatutMembreDernière intervention23 mars 20118 15 mars 2007 à 16:08
Salut,
Merci Charles Racaud, ca fonctionne super bien. Je vais rajouter le code en entier au cas où s'il y en a pour qui cà les interresse. Mais avant, j'aurais juste une question enfin deux :
- Est ce qu'il possible de détecter si une des fenêtres listées fait partie de l'explorateur de Windows (par exemple : Poste de Travail, Mes Documents, ...) Si oui, comment on peut le faire ?
- Est-il possible de pouvoir la fermer ? Si oui, comment ?
cs_fauve
Messages postés661Date d'inscriptionvendredi 2 décembre 2005StatutMembreDernière intervention23 mars 20118 15 mars 2007 à 16:10
Voici le code :
Public Class Form1
Const WM_GETTEXT As Long = &HD
Const WM_GETTEXTLENGTH As Long = &HE
Const GWL_HWNDPARENT As Long = -8
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
Private Declare Function EnumWindows Lib "user32" (ByVal x As MyDelegateCallBack, ByVal y As Integer) As Integer
Private Declare Auto Function SendMessageA Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByRef lParam As Integer) As Integer
Public Delegate Function MyDelegateCallBack(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
Private Declare Auto Function SendMessageString Lib "user32" Alias "SendMessage" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wparam As Integer, ByVal lparam As System.Text.StringBuilder) As Integer
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As IntPtr) As Boolean
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As IntPtr) As Integer
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
Private Function GetWindowClass(ByVal hwnd As Long) As String
' On prépare la chaine de caractère avec 255 espaces
GetWindowClass = Space(255)
' On récuère le nom de la classe
GetClassName(hwnd, GetWindowClass, 255)
' On efface les caractères vides
GetWindowClass = Microsoft.VisualBasic.Left(GetWindowClass, InStr(GetWindowClass, vbNullChar) - 1)
End Function
Public Function EnumOutput(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
Dim texte As String
texte = GetWindowText(hwnd)
If texte <> "" And IsTask(hwnd) = True Then
MsgBox(texte)
End If
Return True
End Function
Public Function IsTask(ByVal hwnd As IntPtr) As Boolean
IsTask = False
'le fenêtre doit être visible
If IsWindowVisible(hwnd) = True And (GetWindowLong(hwnd, GWL_HWNDPARENT) = 0) And Not (GetWindowTextLength(hwnd)) Then
' Vérifie que ce n'est pas le TaskManager
If GetWindowClass(hwnd) <> "Progman" Then
IsTask = True
End If
End If
End Function
Function GetWindowText(ByVal WindowHandle As IntPtr) As String
Dim ptrRet As IntPtr
Dim ptrLength As IntPtr
'get length for buffer...
ptrLength = SendMessageA(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)
'create buffer for return value...
Dim sb As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim del As MyDelegateCallBack
del = New MyDelegateCallBack(AddressOf EnumOutput)
EnumWindows(del, 1)
EnumWindows(AddressOf EnumOutput, 1)
End Sub
End Class, ----
(Coloration syntaxique automatique par Kenji)