如何将外部应用程序窗口置于顶部? [复制]

Posted

技术标签:

【中文标题】如何将外部应用程序窗口置于顶部? [复制]【英文标题】:How to bring external application window on top? [duplicate] 【发布时间】:2018-02-23 02:55:38 【问题描述】:

我的 Outlook Express 始终位于顶部,Google chrome 位于 Outlook 后面。如何使用 Visual Basic 在 OutLook express 上运行 Google chrome?

以下打开了一个新应用程序,但我希望现有的 Google chrome 能够在上面显示?

Shell("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", AppWinStyle.MaximizedFocus)

编辑:

Public Class Form1
  Declare Auto Function FindWindow Lib "User32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
  Declare Auto Function SetForegroundWindow Lib "User32.dll" (ByVal Hwnd As IntPtr) As Long
  'Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Int32) As Int32
  Declare Auto Function FindWindowEx Lib "User32.dll" (ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr


  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Shell("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", AppWinStyle.MaximizedFocus)
    Dim Handle As IntPtr = FindWindow("Notepad", Nothing)
    If Handle.Equals(IntPtr.Zero) Then
      End
    End If

    'Dim HandleChildOne As IntPtr = FindWindowEx(Handle, IntPtr.Zero, "Notepad", IntPtr.Zero)
    'If HandleChildOne.Equals(IntPtr.Zero) Then
    'End
    'End If

    Dim Result As Integer = SetForegroundWindow(Handle)
    If Result.Equals(0) Then
      End
    Else
      MsgBox("Above 0: success. https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx " & Result)
    End If
  End Sub

  Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    End
  End Sub
End Class

【问题讨论】:

不确定是不是问题,但电话是SetForegroundWindow(),带有小写的“g” 对于哪些进程可以设置前台窗口 (msdn.microsoft.com/en-us/library/windows/desktop/…) 有很多限制,因此您可能遇到了其中之一。首先,我会检查 FindWindow() 是否返回了一个有效的句柄,然后检查 SetForegroundWindow() 的返回。注意:限制之一是进程不能被调试。 我想知道这是否是您的真实代码,因为您发布的第一个代码因运行时错误而失败。您似乎也完全忽略了错误检查。为什么?你考虑过调试吗? SetForegroundWindow 在进程是唯一的前台进程时工作。 1)但在我的情况下 Outlook,Microsoft Word 是前台进程 2)然后我的应用程序作为前台进程启动,然后它触发“其他应用程序”成为前台。 3)在这种情况下,我怎么能成为最前台的进程,即使是 Outlook,Microsoft Word 也是? 当我使用 `ShowWindow(Handle, 9)` 时,唯一的问题是 Chrome 窗口大小会发生变化,并且位置也会发生变化。将 Chrome 窗口置于前台时,我不想更改任何位置或大小。 【参考方案1】:

@Codexer 的方法 1 有效(方法 2、3 也包括在内以供以后研究)。请注意,Chrome 窗口位置/大小在应用 ShowWindow(Handle, 9) 时会意外修改

Public Class Form1
  Declare Auto Function FindWindow Lib "User32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
  Declare Auto Function SetForegroundWindow Lib "User32.dll" (ByVal Hwnd As IntPtr) As Long
  Declare Auto Function FindWindowEx Lib "User32.dll" (ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
  Declare Auto Function SetWindowPos Lib "User32.dll" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
  Const HWND_TOPMOST = -1
  Const HWND_NOTOPMOST = -2
  Const SWP_NOSIZE = &H1
  Const SWP_NOMOVE = &H2
  Const SWP_NOACTIVATE = &H10
  Const SWP_SHOWWINDOW = &H40

  Declare Auto Function ShowWindow Lib "User32.dll" (handle As IntPtr, nCmdShow As Integer) As Boolean
  Declare Auto Function IsIconic Lib "User32.dll" (handle As IntPtr) As Boolean

  ' Method 1
  Private Sub StartOrShowProcess(ByVal strProcessName As String)
    Try
      Dim handle As IntPtr
      Dim proc As Process() = Process.GetProcessesByName(strProcessName)
      If proc.Count > 0 Then
        For Each procP As Process In proc
          handle = procP.MainWindowHandle
          ' Do we have handle and minimized or not minimized?
          If handle <> 0 Then
            ShowWindow(handle, 9)
            SetForegroundWindow(handle)
          End If

        Next
      Else 'Not running or started...
        Process.Start(strProcessName)
      End If

    Catch ex As Exception
      'Handle your error...
    End Try
  End Sub

  ' Method 2/3
  Private Sub Old()
    '=== Method 1: Target chrome > as new window
    'Shell("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", AppWinStyle.MaximizedFocus)

    '=== Method 2: Target chrome > Target specific TAB
    Dim Handle As IntPtr = FindWindow(Nothing, "Nieuw tabblad - Google Chrome")
    If Handle.Equals(IntPtr.Zero) Then
      Handle = FindWindow(Nothing, "TITLE... - Google Chrome")
      If Handle.Equals(IntPtr.Zero) Then
        End
      End If
    End If

    ' !!!ShowWindow!!!! help to detect from minmize state
    ShowWindow(Handle, 9)
    Dim Result As Integer = SetForegroundWindow(Handle)
    If Result.Equals(0) Then
      End
    End If
  End Sub

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.TopMost = True
    StartOrShowProcess("chrome")
  End Sub

  Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    End
  End Sub

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    End
  End Sub
End Class

【讨论】:

以上是关于如何将外部应用程序窗口置于顶部? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

将一个窗口置于另一个窗口之上

微信小程序正式上线 可置于聊天窗口顶部

如何将 PyQt QProcess 窗口置于前面?

如何将子视图从其父视图的父视图置于顶部

Android 通知单击保持堆栈并在需要时将应用程序置于最前面。如果特定活动在顶部,则传递数据

winform如何让弹出窗口始终置于程序最顶层