[Vb.net]如何在外部应用程序之间进行交换 [英] [Vb.net] how to swap between external applications
问题描述
您好!我正在尝试编写一个可以在后台打开的Internet Explorer窗口之间切换的应用程序。问题是,这些Internet Explorer窗口将具有完全相同的名称(只是不同的PID)
我尝试过:
我尝试使用
Hello! I'm trying to write an app which can swap between Internet Explorer windows which are already open in the background. The problem is, these Internet Explorer windows will have the exact same name (Just different PIDs)
What I have tried:
I have tried working with
AppActivate
然而,这似乎只接受Window Handles的名称。
however, this seems to only accept Window Handles by name.
Function GetObjInts()
If Process.GetProcessesByName("iexplore").Length >= 1 Then
For Each ObjProcess As Process In Process.GetProcessesByName("iexplore")
'Get the list of IDs for us to cycle.
ObjInts.Add(ObjProcess.Id)
Next
End If
End Function
Function Swap()
For Each Id As Integer In ObjInts
AppActivate(Id)
SendKeys.SendWait("~")
End If
Next
End Function
关于如何与Windows交换窗口的任何其他想法同意(通过PID?)将不胜感激!
Any alternative ideas on how to swap windows with the same name (by PID?) would be appreciated!
推荐答案
您将要使用user32.dll函数。
声明这样的函数:
You're going to want to use a user32.dll function.
Declare the function like so:
Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer
使用API很简单。这是一个示例代码段。
Using the API is easy. Here's an example snippet.
<System.Runtime.InteropServices.DllImport("user32.dll")>
<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)>
Private Shared Function ShowWindow(ByVal hWnd As IntPtr, ByVal flags As ShowWindowEnum) As Boolean
End Function
<System.Runtime.InteropServices.DllImport("user32.dll")>
Private Shared Function SetForegroundWindow(ByVal hwnd As IntPtr) As Integer
End Function
Private Enum ShowWindowEnum
Hide = 0
ShowNormal = 1
ShowMinimized = 2
ShowMaximized = 3
Maximize = 3
ShowNormalNoActivate = 4
Show = 5
Minimize = 6
ShowMinNoActivate = 7
ShowNoActivate = 8
Restore = 9
ShowDefault = 10
ForceMinimized = 11
End Enum
Public Sub BringMainWindowToFront(ByVal processName As String)
Dim bProcess As Process = Process.GetProcessesByName(processName).FirstOrDefault()
If bProcess IsNot Nothing Then
If bProcess.MainWindowHandle = IntPtr.Zero Then
ShowWindow(bProcess.Handle, ShowWindowEnum.Restore)
End If
SetForegroundWindow(bProcess.MainWindowHandle)
Else
Process.Start(processName)
End If
End Sub
您可以根据自己的需要修改 BringMainWindowToFront (例如使用PID)。
希望这有帮助!
You can modify BringMainWindowToFront to your own needs (like using PID).
Hope this helps!
这篇关于[Vb.net]如何在外部应用程序之间进行交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!