Determine the top most (zOrder) window other than my form?

jhebbel

Member
Joined
Dec 21, 2011
Messages
5
Programming Experience
5-10
Like the title says im trying to find the top most window other than my form or its parent form...

Background:
Im writing software for a TS-MFD (touch screen - Multi function display), it has a docked menu bar at the bottom with the option to pull up an onscreen keyboard. Now the keyboard ive written and works well but the only drawback is i need to specify what window to send the keys too, be it inside my own application or a 3rd party app such as notepad. I believe the answer lies an recursively looping through GetForegroundWindow() from user32.dll and ignoring "Keyboard" and "MenuBar", but I dont know how to advance to the next top most window, i only know how to get the top window...

so my window order is likely Keyboard,Menubar,(then the window im trying to grab)

working in vs2010 and im also trying to keep this .net 2 if i can
 
EnumWindows function can be used to enumerate top-level windows, they are returned in z-order.
Since you only need the "second" window you can also use GetWindow function.
 
Found this code:

VB.NET:
Imports System.Runtime.InteropServices
Imports System.Text


Module modEnumWindows


    Private windowList As New ArrayList
    Private errMessage As String


    Public Delegate Function MyDelegateCallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
    Declare Function EnumWindows Lib "user32" (ByVal x As MyDelegateCallBack, ByVal y As Integer) As Integer


    Declare Auto Function GetClassName Lib "user32" _
        (ByVal hWnd As IntPtr, _
        ByVal lpClassName As System.Text.StringBuilder, _
        ByVal nMaxCount As Integer) As Integer


    Declare Auto Function GetWindowText Lib "user32" _
       (ByVal hWnd As IntPtr, _
       ByVal lpClassName As System.Text.StringBuilder, _
       ByVal nMaxCount As Integer) As Integer


    Private Function EnumWindowProc(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean


        'working vars
        Dim sTitle As New StringBuilder(255)
        Dim sClass As New StringBuilder(255)


        Try


            Call GetClassName(hwnd, sClass, 255)
            Call GetWindowText(hwnd, sTitle, 255)


            windowList.Add(sClass.ToString & ", " & hwnd & ", " & sTitle.ToString)
        Catch ex As Exception
            errMessage = ex.Message
            EnumWindowProc = False
            Exit Function
        End Try


        EnumWindowProc = True


    End Function


    Public Function getWindowList(ByRef wList As ArrayList, Optional errorMessage As String = "") As Boolean


        windowList.Clear()


        Try
            Dim del As MyDelegateCallBack
            del = New MyDelegateCallBack(AddressOf EnumWindowProc)
            EnumWindows(del, 0)
            getWindowList = True
        Catch ex As Exception
            getWindowList = False
            errorMessage = errMessage
            Exit Function
        End Try


        wList.Clear()
        wList = windowList


    End Function


End Module
so it looks like i can create another function to loop through wList and return the first window not labeled one of the ones i want to ignore?!?
 
If you just need to get handle for second window in z-order from your app using GetWindow function is just a one line call.
 
im not sure it will always be the 2nd window, i think the safest most compatible way is to loop through all the windows in z order until the first instance of a window not in my ignore list.
if i through enumwindows in a for each loop what syntax do i use and what does enumwindows return? for example
Not actual code, just a logical flow, but would something like this work?
VB.NET:
for each window in enumwindows
for each subwindow in enumsubwindow(window)
if subwindow != "mywindow" then return subwindow
end for
end for
 
EnumWindows requires a callback method, when called this method give you one window handle. In the callback method you return True if you want next window handle.
 
now im lost, im looking at the 1st example i posted and i cant seem to make sense of it, im a self taught programmer so i learn mostly from examples. i think it returns a array that i can loop through how i want, but it just seems like there's a lot of fat in it that could stand to be cut
 
Back
Top