Solved

Finding coordinates of window

Posted on 2000-03-04
6
269 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 150 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 40
JSON Response and request in VB6 application 11 564
Problem to With line 4 63
Sub or Function is not defined 6 43
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

789 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