שתף באמצעות


Get X/Y coordinates of an external application window

Question

Thursday, August 21, 2014 9:31 AM

Hello,

I have been trying to write a function in VS 2010 which will get the X/Y coordinates of (eg the top left corner) the window of a running application. I have tried using FindWindow, FindWindowEx etc. but have been unsuccessful, mostly because these functions require the class name of the window, which I do not know how to find out. I have tried other techniques, all which seem to require either a class name or a handle etc. - things which I cannot find. I have tried using process explorer to find a handle or class name, but could not pin point exactly what I was looking for. Spy++ is not an option as I only have access to VS2010 express, and am coding in VB.

My question is can anyone point me in the right direction to obtain the coordinates for an external application? Thanks in advance for any help.

Christian.

All replies (4)

Thursday, August 21, 2014 10:21 AM ✅Answered | 1 vote

Hi,

 You can download Spy++ from here.

Spy++ Download

 

With the FindWindow API can supply the Class Name OR the Windows Title OR you can supply both parameters. Notice in the code below i only supply the windows Title and Nothing for the Class Name. You can also do it by supplying the Class Name and Nothing for the Window Title. It is best to supply both the Class Name and the Window Title to get the window.

 This will find the rectangle area of the screen that Notepad is located. You can just use the Top and Left of the RECT to get the X, Y location of the window.

Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("user32.dll", EntryPoint:="FindWindowW")> _
    Private Shared Function FindWindowW(<MarshalAs(UnmanagedType.LPTStr)> ByVal lpClassName As String, <MarshalAs(UnmanagedType.LPTStr)> ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("user32.dll", EntryPoint:="GetWindowRect")> _
    Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure RECT
        Public left, top, right, bottom As Integer
    End Structure

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim hWnd As IntPtr = FindWindowW(Nothing, "untitled - notepad")
        If hWnd <> IntPtr.Zero Then
            Dim wr As New RECT
            GetWindowRect(hWnd, wr)
            MessageBox.Show("The window position is - X=" & wr.left.ToString & "  Y=" & wr.top.ToString)
        Else
            MessageBox.Show("Window not found")
        End If
    End Sub
End Class

 

If you say it can`t be done then i`ll try it


Thursday, August 21, 2014 9:54 AM

To do this you have to use the handle of those windows.

For contributors, be aware that the code for that can be used by persons who's only intention is bullying others.

Programmers should not change the position of windows from others programmers.

If it is for instance in a windows form it is simply getting the property.

me.left and me.top

Success
Cor


Thursday, August 21, 2014 10:02 AM

Thanks for the reply, I can assure you I'm not planning to use this code to bully others, if thats what you are suggesting? I will go into more detail, I need to locate the coordinates of the application, so that I can use a cursor to click a point within that application. I cannot find the handle for the window, if this is the only way to do it, then can you go into detail about how to find the handle for this window? Thanks, Christian


Thursday, August 21, 2014 10:44 AM

Thankyou, this answer was perfect! I was attempting similar code but realised I wasnt being specific enough with the name of the window, I just used "Notepad".