Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
Dim hWnd As Integer
If txtWindowCaption.Text = "" And txtClassName.Text = "" Then
hWnd = Win32API.FindWindowAny(0, 0)
ElseIf txtWindowCaption.Text = "" And txtClassName.Text <> "" Then
hWnd = Win32API.FindWindowNullWindowCaption(txtClassName.Text, 0)
ElseIf txtWindowCaption.Text <> "" And txtClassName.Text = "" Then
hWnd = Win32API.FindWindowNullClassName(0, txtWindowCaption.Text)
Else
hWnd = Win32API.FindWindow(txtClassName.Text, txtWindowCaption.Text)
End If
If hWnd = 0 Then
MsgBox("Specified win not running.", MsgBoxStyle.Exclamation, Me.Text)
Else
Win32API.SetForegroundWindow(hWnd)
If Win32API.IsIconic(hWnd) Then
Win32API.ShowWindow(hWnd, Win32API.SW_RESTORE)
Else
Win32API.ShowWindow(hWnd, Win32API.SW_SHOW)
End If
End If
End Sub
Some Helpful Points
1-FindWindowAny takes to Integer parameters and finds any available window.
2-FindWindowNullWindowCaption attempts to locate a window by class name alone.
3-FindWindowNullClassName attempts to locate a window by window name alone.
4-FindWindow searches for a window by class name and window name.
If the window isn't found FindWindow sets the windows handle to 0. If the handle is 0 display an error message, otherwise bring window to foreground. Win32API.SetForegroundWindow (hWnd) set the window as foreground. If window is minimized, simply restore, otherwise show it. Notice the declaration of Win32API.IsIconic defines the return value as Boolean allowing .NET to marshall the integer value to a Boolean.