-3

I need to know how we can minimize an already open Internet Explorer browser using vb.net. Everywhere i can only find code to minimize the form and not for the web browser. Any help will be appreciated. Thanks in advance.

anonymous21
  • 81
  • 2
  • 9

1 Answers1

2

Here is how to. But remember this is WindowsAPI. You should read, and learn more before doing any serious stuff.

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

I'll explain.

The import in the first line is needed to use DllImport, which will be used subsequently. The lines of code beginning with <DllImport... imports a function from user32.dll. Since we are going to work with an external application, we take support from WindowsAPI, a set of services provided by windows, to manage that.

The function we're using has the ability to Minimize, Maximize, Hide or Restore an external window. The possible alternatives are listed in the Enum which is at the end of the code. pinvoke.net nicely lists down what they do, if you need a peek.

This code simply assigns a button click do all the work, but of course, this is an example, you should change this as you wish.

We then get the process we need, here it is iexplore, for internet explorer. You can find it in the task manager or tasklist command in Command Prompt. But use it without the .exe part. When we are getting the process, we receive a list: of course, multiple instances of iexplore might be running! I extracted the first one. (Be careful, though, if no iexplore is running, it'll throw an error - handle that.)

And then, get the handle of the main window! What is a handle, btw?

Use ShowWindow(hWnd, ShowWindowCommands.ForceMinimize) to minimize Internet Explorer with the API. (I had to force. It didn't work with Minimize = 6 value.)

Read more here on pinvoke and here on MSDN

EDIT:
OMG! Internet explorer is multi-process! Instead of minimizing the first, minimize them all!
Change code inside Button1_Click to:

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
Community
  • 1
  • 1
Wickramaranga
  • 943
  • 2
  • 19
  • 35