Solved

Finding coordinates of window

Posted on 2000-03-04
6
270 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

726 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