Solved

Finding coordinates of window

Posted on 2000-03-04
6
264 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
Comment Utility
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
Comment Utility
Exactly what I was looking for
0
 

Author Comment

by:njm123
Comment Utility
Is there any way to get the coordinates if of each window if there are multiple windows of the same name ?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 32

Expert Comment

by:Erick37
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks again - this is really helpfull stuff !
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now