Link to home
Start Free TrialLog in
Avatar of topdeveloper1
topdeveloper1

asked on

Get the Info about last application activated

Hello all,

When my application gets activated then I want to know that which was the last activated window.

I’ve a module that helps me to find out when my application get active or deactive. Now I want to know the API or any method, which will be able to tell me about the last activated window (myapp or notepad or IE….)  

After googling I found "The GetGUIThreadInfo function retrieves information about the active window, or a specified graphical user interface (GUI) thread” On http://vbnet.mvps.org/index.html?code/system/getguithreadinfo.htm.

But still I’m not getting the accurate solution. Please help me.

Thanks
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Try this


Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long

Private Function GetTaskName(ByVal wnd As Long) As String
    Dim procid As Long
    Dim bjWMIService, objprocess, colProcesses
   
    On Error Resume Next
    GetWindowThreadProcessId wnd, procid
    Set objWMIService = GetObject("winmgmts:")
    Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process")
    For Each objprocess In colProcesses
        If objprocess.Handle = procid Then
            GetTaskName = objprocess.executablepath
            Exit Function
        End If
    Next
End Function


...


.AddItem "active window hwnd:" & vbTab & gui.hwndactive
.AddItem " taskname = " & GetTaskName(gui.hwndactive)
.AddItem "   active window title:" & vbTab & GetActiveWindowTitle(gui.hwndactive)

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of topdeveloper1
topdeveloper1

ASKER

Thanks for reply; this new project is working fine.
But there is a problem.

Ex- I create the exe of this project. Now I opened two instances of that exe.
Say app1 and app2.
Now suppose app1 is in active state and app2 is in deactivating state. When I switching from app1 to app2, then I want to know that which event occur first, de-active of app1 or active of app2.
Plz help me.

Regards
please close the issue

Thanks