Code Snippet
Public Class Form1
' P/Invoke API
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Integer
Declare Function GetForegroundWindow Lib "user32" () As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form2.Show()
Form3.Show()
Timer1.Interval = 1000
Timer1.Start()
End Sub
'Do workatone secondinterval
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' Part 1: Get current active window title through P/Invoke APIs
' Get current Active Window handle
Dim hdl As IntPtr = GetForegroundWindow()
' Create Buffer for Active Window title
Dim windowTitle As String = Space(GetWindowTextLength(hdl) + 1)
' Get Active Window title, that is strTitle
GetWindowText(hdl, windowTitle, windowTitle.Length)
' Part 2: Do expectedsomething based oncurrent active window
' If Form1 is current active window
If windowTitle = "Form1" Then
Form2.Visible = True
Form3.Visible = True
Me.BringToFront()
Else ' If none of Form1,Form2 and Form3 is current active window
If windowTitle <> "Form2" And windowTitle <> "Form3" Then
Form2.Visible = False
Form3.Visible = False
'Form2.SendToBack()
'Form3.SendToBack()
End If
End If
End Sub
End Class