Hi @Mansour_Dalir ,
You can use the WindowFromPoint
function to get the window handle at specific screen coordinates.
Then use GetClassName
to check if the window handle belongs to a label control (typically "Static" for labels). Use GetWindowText
to extract the text from the window handle.
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
' Declare necessary API functions
<DllImport("user32.dll")>
Private Shared Function WindowFromPoint(ByVal p As Point) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function GetClassName(ByVal hWnd As IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function
<StructLayout(LayoutKind.Sequential)>
Public Structure Point
Public X As Integer
Public Y As Integer
Public Sub New(ByVal x As Integer, ByVal y As Integer)
Me.X = x
Me.Y = y
End Sub
End Structure
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim x As Integer = 300
Dim y As Integer = 300
Dim point As New Point(x, y)
Dim hWnd As IntPtr = WindowFromPoint(point)
If hWnd <> IntPtr.Zero Then
Dim className As New StringBuilder(256)
GetClassName(hWnd, className, className.Capacity)
Console.WriteLine(className.ToString)
If className.ToString() = "The classname you need" Then
' Get the text of the window (label)
Dim windowText As New StringBuilder(256)
GetWindowText(hWnd, windowText, windowText.Capacity)
Console.WriteLine("Text at specified coordinates: " & windowText.ToString())
Else
Console.WriteLine("The window at the specified coordinates is not a label.")
End If
Else
Console.WriteLine("No window found at the specified coordinates.")
End If
End Sub
End Class
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.