none
How to get the title/name of the last active window? (VB.NET) RRS feed

  • Question

  • Hello!

    I know I can use GetForegroundWindow to get the current active window. However, I want to get the last(previous) active window.

    For example, I am opening 4 windows:
    1. Notepad (current active window)
    2. Win Word
    3. IE
    4. Notepad++

    Then how can I get the title of the last active window (i.e. Win Word) with VB.NET?

    Thanks

    Tuesday, September 12, 2017 7:04 AM

Answers

  • Thank you all :)

    I found a workable method to track all fore ground windows :

    Reference: http://www.vb-helper.com/howto_net_track_fg_window.html

    Private m_LastHwnd As Integer
    Dim index As String = 0
    Dim title_list As New ArrayList
    Dim app_index As String
    
    Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer2.Tick
    
            ' Get the window's handle.
    
            Dim fg_hwnd As Long = GetForegroundWindow()
    
            ' If this is the same as the previous foreground window,
            ' let that one remain the most recent entry.
            If m_LastHwnd = fg_hwnd Then Exit Sub
            m_LastHwnd = fg_hwnd
    
            Console.WriteLine("---------------------------------")
    
            title_list.Add(GetWindowTitle(fg_hwnd))
    
            If title_list(index).ToString = "app" Then
                app_index = index
                Console.WriteLine("app_index: " + app_index)
            End If
    
            index += 1
    
    
        End Sub

    Wednesday, September 13, 2017 7:55 AM

All replies

  • The below code will retrieve the prior active window and set it to active:

        Enum GetWindowCmd As UInteger
            GW_HWNDFIRST = 0
            GW_HWNDLAST = 1
            GW_HWNDNEXT = 2
            GW_HWNDPREV = 3
            GW_OWNER = 4
            GW_CHILD = 5
            GW_ENABLEDPOPUP = 6
        End Enum
    	
        Declare Function GetWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal wCmd As Integer) As Integer
        Declare Function GetParent Lib "user32.dll" (ByVal hWnd As IntPtr) As IntPtr
        Declare Function SetFocusAPI Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As IntPtr) As Integer
    		
        Public Function SetLastWindow() As Boolean
    
            Dim lastWindowHandle As IntPtr = GetWindow(Process.GetCurrentProcess.MainWindowHandle, CType(GetWindowCmd.GW_HWNDNEXT, UInteger))
    
            While True
                Dim temp As IntPtr = GetParent(lastWindowHandle)
                If temp.Equals(IntPtr.Zero) Then
                    Exit While
                End If
    
                lastWindowHandle = temp
            End While
    
            SetFocusAPI(lastWindowHandle)
    
        End Function


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, September 12, 2017 12:21 PM
  • Then how can I get the title of the last active window (i.e. Win Word) with VB.NET?

    From your description it appears that Win Word was the second of four applications that you opened.  Therefore it is not clear why you have named it as the last active window.   And if by last you actually mean previous then that doesn't actually improve things, because the window previous to any active window is the window that opened that active widow (it had to be active so that you could do whatever you did to open the next window) and I don't think you are opening Win Word from Notepad.   But in any case the sequence in which applications were opened does not indicate the sequence in which they were most recently active - users might have switched between windows. 

    You should provide more context for your problem.  Is this VB code and you are opening these apps from within VB? Or are you trying to monitor all open applications and work out the sequence in which they are becoming active? And are you sure you are interested in active, not opened?

    Wednesday, September 13, 2017 12:55 AM
  • Think he means the main window of the last active process, prior to the currently active process. Also, he has the applications listed in reverse order of how they were opened, e.g. Notepad++ was opened first, IE next, etc. As indicated Notepad is the current window and Word was active prior to that.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, September 13, 2017 1:38 AM
  • Hi rainyancandy,

    According to your description, you want to get last active window title and name. For example, you are opening 4 windows:
    1. Notepad (current active window)
    2. Win Word
    3. IE
    4. Notepad++

    You said that i.e. Win Word are the last active windows? The last active window is now only one windows?

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, September 13, 2017 7:30 AM
    Moderator
  • Thank you all :)

    I found a workable method to track all fore ground windows :

    Reference: http://www.vb-helper.com/howto_net_track_fg_window.html

    Private m_LastHwnd As Integer
    Dim index As String = 0
    Dim title_list As New ArrayList
    Dim app_index As String
    
    Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer2.Tick
    
            ' Get the window's handle.
    
            Dim fg_hwnd As Long = GetForegroundWindow()
    
            ' If this is the same as the previous foreground window,
            ' let that one remain the most recent entry.
            If m_LastHwnd = fg_hwnd Then Exit Sub
            m_LastHwnd = fg_hwnd
    
            Console.WriteLine("---------------------------------")
    
            title_list.Add(GetWindowTitle(fg_hwnd))
    
            If title_list(index).ToString = "app" Then
                app_index = index
                Console.WriteLine("app_index: " + app_index)
            End If
    
            index += 1
    
    
        End Sub

    Wednesday, September 13, 2017 7:55 AM
  • Not sure what this app is supposed to be doing, but in order for this to work the app will always need to be running and the Timer event will have to be constantly firing or you might not catch all the windows.

    Also, not all windows are the main window of an app, if that is important to you.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, September 13, 2017 11:25 AM
  • Thank you all :)

    I found a workable method to track all fore ground windows :

    Reference: http://www.vb-helper.com/howto_net_track_fg_window.html

     I don`t think you have fully tested your solution.  If you test it on various OS's and with different programs,  you will find that the GetForegroundWindow function will also detect and return windows that you most likely would not want.  Windows such as the (Start) button menu,  the "Net UI Tool Windows" which are the right click menus for WordPad,  and the "Jump List" windows which are what pops up when a user right clicks on an item in the Taskbar.

     It will also detect any messagebox windows and dialog windows which are owned/created by any programs too.  I am sure i missed a few types but,  you get the point i think.  You would have references to windows that may have closed already (Dialogs, Menus, MessageBoxes) or even just windows that the user closed and your program does not know about.

     It would take a lot more work to filter out the unwanted types of windows.  Also,  the signature for the GetForegroundWindow function you are using is wrong.  It is suppose to return an IntPtr type in VB.Net,  not a Long type.  The Long was used in VB6 signatures.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, September 13, 2017 9:26 PM
    Wednesday, September 13, 2017 8:58 PM