2016-09-07 96 views

回答

1

以下是如何操作。但請記住這是WindowsAPI。在做任何嚴肅的事情之前,你應該閱讀並且學習更多。

Imports System.Runtime.InteropServices 

Public Class Form1 

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> 
    Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As ShowWindowCommands) As Boolean 
    End Function 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     Dim hWnd As Long = Process.GetProcessesByName("iexplore").First().MainWindowHandle 

     ShowWindow(hWnd, ShowWindowCommands.ForceMinimize) 

    End Sub 

    Enum ShowWindowCommands As Integer 
     Hide = 0 
     Normal = 1 
     ShowMinimized = 2 
     Maximize = 3 
     ShowMaximized = 3 
     ShowNoActivate = 4 
     Show = 5 
     Minimize = 6 
     ShowMinNoActive = 7 
     ShowNA = 8 
     Restore = 9 
     ShowDefault = 10 
     ForceMinimize = 11 
    End Enum 
End Class 

我會解釋。

第一行中的導入需要使用DllImport,隨後將使用它。以<DllImport開頭的代碼行從user32.dll導入函數。由於我們將與外部應用程序一起工作,因此我們從Windows API提供的支持來管理這些服務。

我們正在使用的功能具有最小化,最大化,隱藏或恢復外部窗口的功能。代碼末尾的Enum中列出了可能的替代方案。 pinvoke.net很好地列出他們做什麼,如果你需要偷看。

該代碼只是簡單地指定一個按鈕點擊完成所有工作,但當然,這是一個例子,您應該根據需要更改它。

然後,我們得到我們需要的過程,這裏是iexplore,用於Internet Explorer。您可以在任務管理器中找到它或在命令提示符下輸入tasklist命令。但使用它沒有.exe部分。當我們獲得這個過程時,我們會收到一個列表:當然,iexplore的多個實例可能正在運行!我提取了第一個。 (但要小心,如果沒有iexplore正在運行,它會拋出一個錯誤 - 處理該錯誤。)

然後,獲取主窗口的句柄What is a handle, btw?

使用ShowWindow(hWnd, ShowWindowCommands.ForceMinimize)可以最大限度地減少帶有API的Internet Explorer。 (。我不得不強迫它沒有與Minimize = 6值工作)

更多here on pinvokehere on MSDN

編輯:
OMG! Internet Explorer是多進程的! 而不是最小化第一,儘量減少它們!
更改代碼中Button1_Click到:

For Each p In Process.GetProcessesByName("iexplore") 
    ' Since Internet Explorer always has its name in the title bar, 
    If p.MainWindowTitle.Contains("Internet Explorer") Then 
     Dim hWnd As Long = p.MainWindowHandle 
     ShowWindow(hWnd, ShowWindowCommands.ForceMinimize) 
    End If 
Next 
+0

thanx的快速反應Wickramaranga併爲代碼的詳細說明:)但它並沒有減少我的IE瀏覽器:( – anonymous21

+0

@ anonymous21編輯 – Wickramaranga

+0

它的工作原理謝謝! ) – anonymous21