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

Mansour_Dalir 1,976 Reputation points
2024-06-06T07:48:09.8966667+00:00

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,904 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,657 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,741 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 32,451 Reputation points Microsoft Vendor
    2024-06-06T09:16:01.94+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.