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
Solved

After using GetWindowText to list the Child Name how do I gets its position and size

Posted on 2008-10-10
6
472 Views
Last Modified: 2012-05-05
My goal is to get window text, size, position, handle of each child and the parent.

 I'm able to get the child using GetWindowText but when I use GetWindowThreadProcessId followed by GetProcessById it breaks down and gives me only the information for the active window


First I call
   Public Function EnumWindowsCallBack(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32

        'Get the windowtext length
        sSave = Space(GetWindowTextLength(hwnd) + 1)

        'get the window text
        GetWindowText(hwnd, sSave, Len(sSave))

        'remove the last Chr(0)
        sSave = Microsoft.VisualBasic.Left(sSave, Len(sSave) - 1)

And this works to list the windows and all the children

The next step is where my conversion goes wrong and only list the active window

        'get the child processID
        ProcessID = 0
        GetWindowThreadProcessId(hwnd, ProcessID)
        If (ProcessID = 0) Then
            MessageBox.Show("Error finding target process ID")
            Exit Function
        End If

        Dim tempProc As Process = Process.GetProcessById(ProcessID)

        Dim processName As String = tempProc.ProcessName
        If (processName = "iexplore") Then

            If (sSave.Length <> 0) Then
               'Works
                Lst.Items.Add(sSave)
               'Doesn't Work
                Lst.Items.Add(ProcessID.ToString)
                Lst.Items.Add(tempProc.MainWindowHandle)
                Lst.Items.Add(tempProc.MainWindowTitle)
            End If
        End If

ProcessID.ToString is giving me the same id for children as the active id

mpProc.MainWindowTitle is not giving me the same name as GetWindowText
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Lst = ListBox1      '--------> ADD THIS LINE
        EnumWindows(AddressOf EnumWindowsCallBack, 0)
 
    End Sub
 
 
End Class
 
 
Module Module1
    Private Declare Auto Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef ProcessID As Integer) As Integer
 
    Public Lst As ListBox
 
    Public Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32
 
    Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32
 
    Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Int32
 
    Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32
 
    'Callback function to enum windows
    Public Function EnumWindowsCallBack(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32
        Dim sSave As String
        Dim ProcessID As Long
 
        'Get the windowtext length
        sSave = Space(GetWindowTextLength(hwnd) + 1)
 
        'get the window text
        GetWindowText(hwnd, sSave, Len(sSave))
 
        'remove the last Chr(0)
        sSave = Microsoft.VisualBasic.Left(sSave, Len(sSave) - 1)
 
        'get the child processID
        ProcessID = 0
        GetWindowThreadProcessId(hwnd, ProcessID)
        If (ProcessID = 0) Then
            MessageBox.Show("Error finding target process ID")
            Exit Function
        End If
 
        Dim tempProc As Process = Process.GetProcessById(ProcessID)
 
        Dim processName As String = tempProc.ProcessName
        If (processName = "iexplore") Then
 
            If (sSave.Length <> 0) Then
 
                Lst.Items.Add(sSave)
                Lst.Items.Add(ProcessID.ToString)
                Lst.Items.Add(tempProc.MainWindowHandle)
                Lst.Items.Add(tempProc.MainWindowTitle)
            End If
        End If
        If sSave.Trim <> "" Then
            Debug.WriteLine(sSave)
        End If
 
        Return 1 'continue enumeration        
    End Function
 
End Module

Open in new window

0
Comment
Question by:vaultworld
  • 3
  • 2
6 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22688314
Hi vaultworld,

Not sure I'm following where you're going with this...

You are enumerating CHILD windows right?  You successfully get the text from the child with GetWindowText()...kewl beans.

An APPLICATION, though, only has ONE ProcessID (PID)...so when you call GetWindowThreadProcessId() it is returning the ID for the entire app.  Thus when you use Process.GetProcessById() and query the info of course it is going to be for the active/main window.  The children windows do not have their own PIDs....they only have "Parents"...for example, a Label in a Panel: the "parent" handle of the Label would be the handle of the Panel.

Your description of the problem states:

    "After using GetWindowText to list the Child Name how do I gets its position and size"

If you want the position and size of a window use the GetWindowRect() API.

0
 
LVL 11

Expert Comment

by:ladarling
ID: 22688540
My goal is to get window text, size, position, handle of each child and the parent.
Why not just use something like:
 
 

 For Each f As Form In Me.OwnedForms
            Debug.Print(f.Text & vbTab & f.Location.ToString & vbTab & vbTab & f.Size.ToString & vbTab & f.Handle.ToString)
        Next
 
 
 
 
...

Open in new window

0
 
LVL 11

Expert Comment

by:ladarling
ID: 22688548
I think I left out size, but anyway....
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Closing Comment

by:vaultworld
ID: 31505036
I was doing a little more research and that looks to be it exactly.  THanks again for your time.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22688609
He's working with EXTERNAL windows ladarling...  =)
0
 
LVL 11

Expert Comment

by:ladarling
ID: 22688791
Ahhh, sorry. Attention to detail.. for me that means 'check out the exception call stack" lol.
:-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending receiving text messages in vb.net 15 47
Help with error in Query 2 31
Complex SQL statement in VB.NET 7 31
ASP.NET (VB) return a record 2 34
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 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