2017-09-14 197 views
1

我的Outlook Express始終位於頂端,Google Chrome位於Outlook後面。如何使用Visual Basic在OutLook Express上運行谷歌瀏覽器?如何將外部應用程序窗口置於頂部?

以下打開一個新的應用程序,但我希望現有的谷歌瀏覽器頂部?

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

編輯:

enter image description here

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 
+1

不知道這是否是問題,但調用的是'SetForegroundWindow()'用小寫字母「G」 – AlanT

+1

有Chrome的窗口位置/尺寸得到意外修改許多限制哪些進程可以設置前臺窗口(https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx),所以它可能是你正在對其中一個犯規。首先,我會檢查FindWindow()返回一個有效的句柄,然後檢查SetForegroundWindow()的返回值。注意:其中一個限制是該進程無法被調試。 – AlanT

+0

我不知道這是否是您的真實代碼,因爲您發佈的第一個代碼因爲運行時錯誤而失敗。您似乎也完全忽略了錯誤檢查。爲什麼?你有沒有想過調試? –

回答

1

方法@Codexer的1的工作(方法2,3也包括用於研究後)。需要注意的是,雖然應用ShowWindow(Handle, 9)

enter image description here

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 
相關問題