?
Solved

Finding coordinates of window

Posted on 2000-03-04
6
Medium Priority
?
275 Views
Last Modified: 2010-05-02
Hi,

I need to find out the x,y coordinates of a named window which is
a completely different program to my visual basic one.

The windows title bar name is known as is the executable

For example if the window with the title 'Program1 executable' is at
location 200,300 I would like the solution to return two int variables
which would be 200 and 300 for x,y coordinates.

Can anyone tell me how to do this ?

Thanks

Neil

0
Comment
Question by:njm123
  • 3
  • 3
6 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 600 total points
ID: 2583251
Try this out:

Option Explicit
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" _
    (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Command1_Click()
    Dim lhWnd As Long
    Dim rc As RECT
    Dim sWinTitle As String
    sWinTitle = "Calculator"
    lhWnd = FindWindow(vbNullString, sWinTitle)
    If lhWnd <> 0 Then 'Get the coords
        Call GetWindowRect(lhWnd, rc)
        MsgBox sWinTitle & " originates at " & vbCrLf & _
            "X: " & CStr(rc.Left) & " Y: " & CStr(rc.Top)
    End If
End Sub

0
 

Author Comment

by:njm123
ID: 2583274
Exactly what I was looking for
0
 

Author Comment

by:njm123
ID: 2583475
Is there any way to get the coordinates if of each window if there are multiple windows of the same name ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:Erick37
ID: 2583549
Yes, it's a bit different, but it works:

'~~~~FORM CODE~~~~
Option Explicit

Private Sub Command1_Click()
    'Set the global Window Title to search
    sWinTitle = "Calculator"
    'Start searching all windows for a match
    'When matches are found, the NotifyWindowPos
    'function is called with the info.
    Call SearchWindows
End Sub

Public Function NotifyWindowPos(ByVal lhWnd As Long, ByVal x As Long, ByVal y As Long)
    Debug.Print Hex(lhWnd) & " x: " & CStr(x) & "  y: " & CStr(y)
End Function

'=======================================================

'~~~~MODULE CODE~~~~
'MUST RESIDE IN A (BAS) MODULE
Option Explicit

Public Const MAX_PATH = 260
Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Declare Function EnumWindows Lib "user32" _
    (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowRect Lib "user32" _
    (ByVal hwnd As Long, lpRect As RECT) As Long

Global sWinTitle As String

Public Function EnumFunc(ByVal hwnd As Long, ByVal lpData As Long) As Long
    Dim lResult    As Long
    Dim sWndName   As String
'
' This callback function is called by Windows (from the EnumWindows
' API call) for EVERY window that exists.
'
    If sWinTitle = "" Then
        EnumFunc = 0
        Exit Function
    End If
    EnumFunc = 1
    sWndName = Space$(MAX_PATH)
    lResult = GetWindowText(hwnd, sWndName, MAX_PATH)
    sWndName = Left(sWndName, lResult)
    'Test for a match
    If (sWndName = sWinTitle) Then
        Dim rc As RECT
        Call GetWindowRect(hwnd, rc)
        ''Change Form1 below to the name of the
        '''form which contains the function
        Call Form1.NotifyWindowPos(hwnd, rc.Left, rc.Top)
    End If
End Function

Public Function SearchWindows() As Boolean
    Dim hwnd As Long
'
' The EnumWindows function enumerates all top-level windows
' on the screen by passing the handle of each window, in turn,
' to an application-defined callback function. EnumWindows
' continues until the last top-level window is enumerated or
' the callback function returns FALSE.
'
    Call EnumWindows(AddressOf EnumFunc, hwnd)
End Function
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2583569
One last note:

Some applications like Notepad will have a different window title depending on the file that is opened.  To find such windows you can use the Like operator in the EnumFunc function and use wildcards in the sWinTitle variable.

sWinTitle = "*Notepad"

'===============

If (sWndName Like sWinTitle) Then

This will find all windows whose title ends in "Notepad."
0
 

Author Comment

by:njm123
ID: 2583608
Thanks again - this is really helpfull stuff !
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question