?
Solved

Finding coordinates of window

Posted on 2000-03-04
6
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 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