2

I am using VB.NET and need to activate a certain window. Is this possible? If so, how?

Arlen Beiler
  • 15,336
  • 34
  • 92
  • 135

2 Answers2

5

You will need to use the Win32 API to do this.

First, find window you want to bring to front by calling FindWindow to obtain its handle, and then use SetForegroundWindow API to bring it to the foreground.

PInvoke contains declarations for these methods.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
0

There are 2 solutions, one using Window API and another using pure VB.Net

  1. you can use SetForegroundWindow(iHandle)

example with FindWindow to obtain Window handle

Public Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer
Public Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

Dim hWnd As Integer
hWnd = FindWindow(strClassName, strWindowCaption)

If hWnd > 0 Then
    SetForegroundWindow(hWnd)
End If
  1. you can use AppActivate(iProcessId)

example with GetActiveAppProcess() to obtain input Window active process in an hook program

    Dim hWnd As IntPtr
    Dim inputProcess = GetActiveAppProcess()

    hWnd = GetActiveAppProcess().MainWindowHandle
    AppActivate(inputProcess.Id)

    'you can also use SetForegroundWindow
    'SetForegroundWindow(inputProcess..MainWindowHandle)

    SendKeys.Send("^v")
schlebe
  • 3,387
  • 5
  • 37
  • 50