My VB.NET application is suposed to monitor what application currently is running in the topmost window. I have tried the following approach using a timer:
Declare Function GetActiveWindow Lib "user32" () As System.IntPtr
Declare Function GetForegroundWindow Lib "user32" () As System.IntPtr
Public Declare Auto Function GetWindowText Lib "user32" _
(ByVal hWnd As System.IntPtr, _
ByVal lpString As System.Text.StringBuilder, _
ByVal cch As Integer) As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Caption As New System.Text.StringBuilder(256)
Dim hWnd As IntPtr = GetForegroundWindow()
GetWindowText(hWnd, Caption, Caption.Capacity)
'Caption now holds the title of the topmost window
End Sub
By this I can see that for example Outlook or Internet Explorer is the topmost window as the name is in Window's title bar. However, if the user crates a new mail in Outlook the title of the window is "Untitled message" givning no hint of what application is running in the the window.
How do I get what application is connected to the topmost window?p>
Help is appreciated!