Solved

X,Y coordinates of a window

Posted on 2008-06-13
8
333 Views
Last Modified: 2008-06-13
Hi
I need some way of knowing the x & y location of a window (which is not in my own application). For example, a popup window from which i wan't to extract the color. I have used
  Private Declare Auto Function FindWindow Lib "user32" ()
to locate the window for which i need to know the x & y coordinates, but after that it got complicated.

Any thoughts ?
0
Comment
Question by:soriega
[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
  • 2
8 Comments
 
LVL 10

Expert Comment

by:Clif
ID: 21780776
Something like this?


Structure RECT
       Public Left As Long
       Public Top As Long
       Public Right As Long
       Public Bottom As Long
End Structure
 
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
   Private Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, ByVal lpRect As RECT) As Long
 
Private Sub MoveIt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MoveIt.Click
       Dim NotepadRect As RECT
       Dim NotepadHandle As Long
 
       NotepadHandle = FindWindow("Notepad", "Untitled - Notepad")
 
       ' ERROR IS ON THE NEXT LINE!!
       GetWindowRect(NotepadHandle, NotepadRect)
       MsgBox("Top = " & NotepadRect.Top & _
           vbCrLf & "Bottom = " & NotepadRect.Bottom & _
           vbCrLf & "Left = " & NotepadRect.Left & _
           vbCrLf & "Right = " & NotepadRect.Right)
End Sub

Open in new window

0
 

Author Comment

by:soriega
ID: 21780884
I get this errormessage:
System.AccessViolationException was unhandled
  Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
0
 
LVL 10

Expert Comment

by:Clif
ID: 21780965
Oops.  Sorry, I gave you the error version (This is just not my day.  It's like it's Friday the 13th, or something).

Try the following:

Option Explicit
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
 
Private Sub Command1_Click()
       Dim NotepadRect As RECT
       Dim NotepadHandle As Long
 
       NotepadHandle = FindWindow("Notepad", "Untitled - Notepad")
 
       GetWindowRect NotepadHandle, NotepadRect
       MsgBox ("Top = " & NotepadRect.Top & _
           vbCrLf & "Bottom = " & NotepadRect.Bottom & _
           vbCrLf & "Left = " & NotepadRect.Left & _
           vbCrLf & "Right = " & NotepadRect.Right)
End Sub

Open in new window

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 21780967
If you're using VB.Net then you need to change all occurences of "Long" to "Integer".
0
 

Author Comment

by:soriega
ID: 21781059
This doesn't work in my visual studio 2005, it instructs me to use "structure" instead

Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 21781108
The code he gave you is for VB6...

For VB.Net:
    Public Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure
 
    Private Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Integer
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim r As RECT
        Call GetWindowRect(Me.Handle, r)
        Debug.WriteLine("Left = " & r.Left)
        Debug.WriteLine("Top = " & r.Top)
        Debug.WriteLine("Right = " & r.Right)
        Debug.WriteLine("Bottom = " & r.Bottom)
    End Sub

Open in new window

0
 
LVL 10

Accepted Solution

by:
Clif earned 150 total points
ID: 21781188
Sorry (again) I missed that you were using VB.Net

One last time (withfeeling):
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer
    Private Structure RECT
        Dim RectLeft As Integer
        Dim RectTop As Integer
        Dim RectRight As Integer
        Dim RectBottom As Integer
    End Structure
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim NotepadRect As RECT
        Dim NotepadHandle As Integer
 
        NotepadHandle = FindWindow("Notepad", "Untitled - Notepad")
 
        GetWindowRect(NotepadHandle, NotepadRect)
        MsgBox("Top = " & NotepadRect.RectTop & vbCrLf & "Bottom = " & NotepadRect.RectBottom & vbCrLf & "Left = " & NotepadRect.RectLeft & vbCrLf & "Right = " & NotepadRect.RectRight)
    End Sub

Open in new window

0
 

Author Comment

by:soriega
ID: 21781248
That seems to be the solution, i will just have to test it on my "popupscreen", works fine on notepad though. Great Work! thanks
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c#, case, if 4 39
VBA Replace Function - What am I missing? 3 52
Access 2003, find all instances of database ODBC 3 53
VB .net 2010 Byte array 2 24
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 …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

752 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