How to get the text from the label by the handle with specific coordinates on any part of the desktop screen

Mansour_Dalir 1,676 Reputation points

I want to extract the text inside the label by giving certain coordinates from the screen, if the type of the coordinates is from the label, then extract the part of the obtained part.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,865 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,481 questions
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,633 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 27,666 Reputation points Microsoft Vendor

    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
        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
        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)
                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())
                    Console.WriteLine("The window at the specified coordinates is not a label.")
                End If
                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.

    0 comments No comments