Find control Name

This commit is contained in:
Jonathan Jenne
2019-08-07 16:05:53 +02:00
parent 43d3ba5ef2
commit aa84e977b9
22 changed files with 2556 additions and 179 deletions

View File

@@ -15,11 +15,15 @@ Public Class ClassWindowAPI
Public ProcessName As String = ""
Public ClassName As String = ""
Public ProcessId As Integer = 0
Public hWnd As Int32
Public ControlName As String = ""
Public hWnd As IntPtr
End Class
Private Delegate Function EnumCallBackDelegate(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer
Private Declare Function GetCurrentThreadId Lib "kernel32.dll" Alias "GetCurrentThreadId" () As IntPtr
Private Declare Function AttachThreadInput Lib "user32.dll" Alias "AttachThreadInput" (ByVal idAttach As IntPtr, ByVal idAttachTo As IntPtr, fAttach As Boolean) As Boolean
Private Declare Function GetFocus Lib "user32.dll" Alias "GetFocus" () As IntPtr
Private Declare Function GetForegroundWindow Lib "user32.dll" Alias "GetForegroundWindow" () As IntPtr
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpdwProcessID As Integer) As Integer
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As IntPtr, ByVal WinTitle As String, ByVal MaxLength As Integer) As Integer
@@ -182,4 +186,49 @@ Public Class ClassWindowAPI
Return GetWindowInfo(hWnd)
End Function
''' <summary>
'''
''' </summary>
''' <param name="WindowHandle">Current window handle; can be obtained from Me.Handle</param>
''' <returns></returns>
Public Shared Function GetFocusedControl(WindowHandle As IntPtr) As WindowInfo
Try
Dim oWindow = GetWindowInfo()
If oWindow Is Nothing Then
Return Nothing
End If
Dim oThreadId As IntPtr = GetWindowThreadProcessId(oWindow.hWnd, IntPtr.Zero)
Dim oMyThreadId As IntPtr = GetWindowThreadProcessId(WindowHandle, IntPtr.Zero)
If AttachThreadInput(oThreadId, oMyThreadId, True) Then
Try
Dim oControlhWnd = GetFocus()
Dim oControl As WindowInfo = GetWindowInfo(oControlhWnd)
If oControl Is Nothing Then
Return Nothing
End If
Dim oUtils As New ClassWindowAPIUtils()
Dim oName = oUtils.GetWinFormsId(oControlhWnd)
oControl.ControlName = oName
Return oControl
Catch ex As Exception
Logger.Error(ex)
Finally
AttachThreadInput(oThreadId, oMyThreadId, False)
End Try
End If
Return Nothing
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
End Class