Option Strict On Imports System.Runtime.InteropServices Public Class Form1 ' http://www.pinvoke.net/default.aspx/user32.enumchildwindows <DllImport("User32.dll")> _ Private Shared Function EnumChildWindows _ (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, _ ByVal lParam As IntPtr) As Boolean End Function Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean Public Shared Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr() Dim ChildrenList As New List(Of IntPtr) Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList) Try EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle)) Finally If ListHandle.IsAllocated Then ListHandle.Free() End Try Return ChildrenList.ToArray End Function Private Shared Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean Dim ChildrenList As List(Of IntPtr) = CType(GCHandle.FromIntPtr(Parameter).Target, Global.System.Collections.Generic.List(Of Global.System.IntPtr)) If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)") ChildrenList.Add(Handle) Return True End Function ' http://codes-sources.commentcamarche.net/forum/affich-10022830-handle-d-un-controle-d-une-fenetre-externe#11 Private Const WM_GETTEXT As Integer = &HD Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _ ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click For Each p As Process In Process.GetProcesses If p.MainWindowTitle.Contains("Assistant photos") Then ' adapter le titre de la fenêtre Dim Childrens As IntPtr() = GetChildWindows(p.MainWindowHandle) Dim Handle As IntPtr = Marshal.AllocHGlobal(100) 'Dans mon cas, PositionDuContrôle=10 Dim NumText As Integer = CInt(SendMessage(Childrens(10), WM_GETTEXT, CType(50, IntPtr), Handle)) 'Récupération du texte du contrôle! MsgBox(Marshal.PtrToStringUni(Handle)) End If Next End Sub End Class
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Try For Each p As Process In Process.GetProcesses If p.MainWindowTitle.Contains("Assistant photos") Then ' adapter le titre de la fenêtre Dim Childrens As IntPtr() = GetChildWindows(p.MainWindowHandle) Dim Handle As IntPtr = Marshal.AllocHGlobal(100) For i = 1 To 30 'a adapter au nombre de controls 'Dans mon cas, PositionDuContrôle=10 Dim NumText As Integer = CInt(SendMessage(Childrens(i), WM_GETTEXT, CType(50, IntPtr), Handle)) 'Récupération du texte du contrôle! ListBox1.Items.Add(Marshal.PtrToStringUni(Handle)) Next End If Next Catch End Try End Sub
Option Strict On Imports System.Runtime.InteropServices Public Class Form1 ' http://www.pinvoke.net/default.aspx/user32.enumchildwindows <DllImport("User32.dll")> _ Private Shared Function EnumChildWindows _ (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, _ ByVal lParam As IntPtr) As Boolean End Function Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean Public Shared Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr() Dim ChildrenList As New List(Of IntPtr) Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList) Try EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle)) Finally If ListHandle.IsAllocated Then ListHandle.Free() End Try Return ChildrenList.ToArray End Function Private Shared Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean Dim ChildrenList As List(Of IntPtr) = CType(GCHandle.FromIntPtr(Parameter).Target, Global.System.Collections.Generic.List(Of Global.System.IntPtr)) If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)") ChildrenList.Add(Handle) Return True End Function ' http://codes-sources.commentcamarche.net/forum/affich-10022830-handle-d-un-controle-d-une-fenetre-externe#11 Private Const WM_GETTEXT As Integer = &HD Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _ ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr Dim nomappli As String Private Sub cmdcontrol_Click(sender As System.Object, e As System.EventArgs) Handles cmdcontrol.Click Try For Each p As Process In Process.GetProcesses If p.MainWindowTitle.Contains(nomappli) Then Dim Childrens As IntPtr() = GetChildWindows(p.MainWindowHandle) Dim Handle As IntPtr = Marshal.AllocHGlobal(100) For i As Integer = 1 To Childrens.Length Dim NumText As Integer = CInt(SendMessage(Childrens(i), WM_GETTEXT, CType(50, IntPtr), Handle)) 'Récupération du texte du contrôle! lstcontrol.Items.Add(i & " " & Marshal.PtrToStringUni(Handle)) Next End If Next Catch End Try lblcontrol.Text = CStr(lstcontrol.Items.Count) & " controls" End Sub Private Sub cmdappli_Click(sender As System.Object, e As System.EventArgs) Handles cmdappli.Click lstappli.Items.Clear() cmdcontrol.Enabled = False For Each p As Process In Process.GetProcesses If Len(Trim(p.MainWindowTitle)) > 0 Then lstappli.Items.Add(p.MainWindowTitle) Next p End Sub Private Sub lstappli_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstappli.SelectedIndexChanged nomappli = CStr(lstappli.SelectedItem) 'affiche le nom de l'application cmdcontrol.Enabled = True End Sub End Class
15 sept. 2017 à 18:43
Je n'ai pas encore chargé le projet, mais à la lecture de l'intro, ça ressemble assez bien à ce que je recherche.
Je me penche dessus aussitôt que possible.
Cordialement
19 sept. 2017 à 15:44
http://codes-sources.commentcamarche.net/source/102162-controler-une-fenetre-ouverte