简体   繁体   English

设置外部应用程序焦点

[英]Setting external application focus

In VB.NET, you can set focus to an external application using 在VB.NET中,可以使用以下命令将焦点设置为外部应用程序

AppActivate("Windows Name")

or 要么

AppActivate(processID As Integer)

Now this works fine if you do for example: 现在,如果您这样做,则可以正常工作:

Dim intNotePad As Integer = Shell("C:\WINNT\Notepad.exe",
AppWinStyle.MinimizedNoFocus)
AppActivate(intNotePad)

But when I do: 但是当我这样做时:

For Each theprocess As Process In processlist
    If InStr(theprocess.ProcessName, "DWG") Then
        strProcessList += String.Format("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id) + vbCrLf
        AppActivate(theprocess.ID)
    End If
Next

then it doesn't find the window, even if it's open and even if it finds the window using the window title. 那么即使打开了窗口,也没有使用窗口标题找到窗口,它也找不到窗口。

But I need it by process ID. 但是我需要按进程ID。

How can I do that? 我怎样才能做到这一点?

I need it to set focus on a 3rd party installer in a windows installer setup project. 我需要它来将重点放在Windows安装程序安装项目中的第三方安装程序上。

I don't know exactly why your not achieving the correct result. 我不知道为什么您没有达到正确的结果。 Generally, when setting focus to other applications, I've never had too much luck with AppActivate (varying degrees of success, at least). 通常,将重点放在其他应用程序上时,我对AppActivate从未有过太多的运气(至少成功的程度不同)。 Try this instead: 尝试以下方法:

Add this class to the same module / object / whereever your code is: 将此类添加到相同的模块/对象/无论您的代码在哪里:

Public NotInheritable Class Win32Helper
    <System.Runtime.InteropServices.DllImport("user32.dll", _
    EntryPoint:="SetForegroundWindow", _
    CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _
    CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
    Public Shared Function _
    SetForegroundWindow(ByVal handle As IntPtr) As Boolean
        ' Leave function empty
    End Function

    <System.Runtime.InteropServices.DllImport("user32.dll", _
    EntryPoint:="ShowWindow", _
    CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _
    CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
    Public Shared Function ShowWindow(ByVal handle As IntPtr, _
    ByVal nCmd As Int32) As Boolean
        ' Leave function empty
    End Function
End Class

Then in your code, instead of AppActivate , do the following: 然后在您的代码中,而不是AppActivate ,执行以下操作:

Dim appHandle As intPtr
appHandle = theprocess.MainWindowHandle 'theprocess is naturally your process object

Dim Win32Help As New Win32Helper
Win32Helper.SetForegroundWindow(appHandle)

Try these Win32 functions: 尝试以下Win32函数:

Declare Sub SwitchToThisWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal fAltTab As Boolean)
Declare Function SetActiveWindow Lib "user32.dll" (ByVal hwnd As IntPtr) As IntPtr
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

Use Process.MainWindowHandle to get the handle. 使用Process.MainWindowHandle获取句柄。 This works on most, but not all, applications. 这适用于大多数(但不是全部)应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM