开发者

Change focus to another window in VB.NET

I am using VB.NET 开发者_运维知识库and need to activate a certain window. Is this possible? If so, how?


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.


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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜