如何将外部应用程序窗口置于顶部? [英] How to bring external application window on top?

查看:293
本文介绍了如何将外部应用程序窗口置于顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是将Outlook Express放在顶部,而将Google Chrome放在Outlook的后面。

I have Outlook express always on top and Google chrome behind Outlook. How to bring running Google chrome on top of OutLook express using visual basic?

以下操作如何打开一个新应用程序,但我希望现有的Google chrome可以显示在顶部?

Following opens a new application but i want existing Google chrome to bring on top?

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


推荐答案

@Codexer的方法1起作用(方法2、3也包括在以后的研究中)。应用 ShowWindow(Handle,9)

Method 1 of @Codexer works (Method 2, 3 also included for research later). Note that, Chrome window position/size get unexpectedly modified while applying 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

这篇关于如何将外部应用程序窗口置于顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆