List windows to get one below my app

Im trying to create a program to put in snippets of code from the system tray, What Im trying to do is list the windows open to find the one below my application, then on click bring the application into focus, then using sendkeys send the snippet.

I have no idea how to get the window below mine, does anyone else?

Thank you
tonelm54Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
What does "below mine" mean?  Are you talking about the z-order of the windows?  You might be able to enumerate the top-level windows, and check whether the bounds of the window is within the bounds of your window, but the z-order would make it trickier.
0
tonelm54Author Commented:
Basically is is possible to pull a list of all windows and their Z-Order?
0
Bob LearnedCommented:
Here is some code to enumerate windows.  Determining z-order is done by starting with the top-level window, and determining child windows from that point.

Imports System
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports System.Text

''' <summary>
''' Enumerate top-level and child windows
''' </summary>
''' <example>
''' WindowsEnumerator enumerator = new WindowsEnumerator(); 
''' foreach (ApiWindow top in enumerator.GetTopLevelWindows()) 
''' { 
'''    Console.WriteLine(top.MainWindowTitle); 
'''        foreach (ApiWindow child in enumerator.GetChildWindows(top.hWnd))  
'''            Console.WriteLine(" " + child.MainWindowTitle); 
''' } 
''' </example>
Public Class WindowsEnumerator

	Private Delegate Function EnumCallBackDelegate(hWnd As Integer, lParam As Integer) As Integer

	Private Declare Ansi Function EnumWindows Lib "user32" (lpEnumFunc As EnumCallBackDelegate, lParam As Integer) As Integer

	Private Declare Ansi Function EnumChildWindows Lib "user32" (hWndParent As Integer, lpEnumFunc As EnumCallBackDelegate, lParam As Integer) As Integer

	Private Declare Ansi Function GetClassName Lib "user32" Alias "GetClassNameA" (hWnd As Integer, lpClassName As StringBuilder, nMaxCount As Integer) As Integer

	Private Declare Ansi Function IsWindowVisible Lib "user32" (hWnd As Integer) As Integer

	Private Declare Ansi Function GetParent Lib "user32" (hWnd As Integer) As Integer

	Private Declare Ansi Function SendMessage Lib "user32" Alias "SendMessageA" (hWnd As Int32, wMsg As Int32, wParam As Int32, lParam As Int32) As Int32

	Private Declare Ansi Function SendMessage Lib "user32" Alias "SendMessageA" (hWnd As Int32, wMsg As Int32, wParam As Int32, lParam As StringBuilder) As Int32


	' Top-level windows.
	' Child windows.
	' Get the window class.
	' Test if the window is visible--only get visible ones.
	' Test if the window's parent--only get the one's without parents.
	' Get window text length signature.
	' Get window text signature.

	Private _listChildren As New List(Of ApiWindow)()
	Private _listTopLevel As New List(Of ApiWindow)()

	Private _topLevelClass As String = ""
	Private _childClass As String = ""

	''' <summary>
	''' Get all top-level window information
	''' </summary>
	''' <returns>List of window information objects</returns>
	Public Function GetTopLevelWindows() As List(Of ApiWindow)
		EnumWindows(AddressOf EnumWindowProc, 0)

		Return _listTopLevel

	End Function

	Public Function GetTopLevelWindows(className As String) As List(Of ApiWindow)
		_topLevelClass = className

		Return Me.GetTopLevelWindows()
	End Function

	''' <summary>
	''' Get all child windows for the specific windows handle (hwnd).
	''' </summary>
	''' <returns>List of child windows for parent window</returns>
	Public Function GetChildWindows(hwnd As Int32) As List(Of ApiWindow)

		' Clear the window list.
		_listChildren = New List(Of ApiWindow)()

		' Start the enumeration process.
		EnumChildWindows(hwnd, AddressOf EnumChildWindowProc, 0)

		' Return the children list when the process is completed.
		Return _listChildren
	End Function

	Public Function GetChildWindows(hwnd As Int32, childClass As String) As List(Of ApiWindow)

		' Set the search
		_childClass = childClass

		Return Me.GetChildWindows(hwnd)

	End Function

	''' <summary>
	''' Callback function that does the work of enumerating top-level windows.
	''' </summary>
	''' <param name="hwnd">Discovered Window handle</param>
	''' <returns>1=keep going, 0=stop</returns>
	Private Function EnumWindowProc(hwnd As Int32, lParam As Int32) As Int32

		' Eliminate windows that are not top-level.
		If GetParent(hwnd) = 0 AndAlso Convert.ToBoolean(IsWindowVisible(hwnd)) Then

			' Get the window title / class name.
			Dim window As ApiWindow = GetWindowIdentification(hwnd)

			' Match the class name if searching for a specific window class.
			If _topLevelClass.Length = 0 OrElse window.ClassName.ToLower() = _topLevelClass.ToLower() Then
				_listTopLevel.Add(window)

			End If
		End If

		' To continue enumeration, return True (1), and to stop enumeration 
		' return False (0).
		' When 1 is returned, enumeration continues until there are no 
		' more windows left.

		Return 1

	End Function

	''' <summary>
	''' Callback function that does the work of enumerating child windows.
	''' </summary>
	''' <param name="hwnd">Discovered Window handle</param>
	''' <returns>1=keep going, 0=stop</returns>
	Private Function EnumChildWindowProc(hwnd As Int32, lParam As Int32) As Int32

		Dim window As ApiWindow = GetWindowIdentification(hwnd)

		' Attempt to match the child class, if one was specified, otherwise
		' enumerate all the child windows.
		If _childClass.Length = 0 OrElse window.ClassName.ToLower() = _childClass.ToLower() Then
			_listChildren.Add(window)
		End If

		Return 1

	End Function

	''' <summary>
	''' Build the ApiWindow object to hold information about the Window object.
	''' </summary>
	Private Function GetWindowIdentification(hwnd As Integer) As ApiWindow

		Const  WM_GETTEXT As Int32 = 13
		Const  WM_GETTEXTLENGTH As Int32 = 14

		Dim window As New ApiWindow()

		Dim title As New StringBuilder()

		' Get the size of the string required to hold the window title.
		Dim size As Int32 = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)

		' If the return is 0, there is no title.
		If size > 0 Then
			title = New StringBuilder(size + 1)

			SendMessage(hwnd, WM_GETTEXT, title.Capacity, title)
		End If

		' Get the class name for the window.
		Dim classBuilder As New StringBuilder(64)
		GetClassName(hwnd, classBuilder, 64)

		' Set the properties for the ApiWindow object.
		window.ClassName = classBuilder.ToString()
		window.MainWindowTitle = title.ToString()
		window.hWnd = hwnd

		Return window

	End Function

End Class

Public Class ApiWindow
	Public MainWindowTitle As String = ""
	Public ClassName As String = ""
	Public hWnd As Integer
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.