4

If you have ever noticed in the Task Manager, when you right-click on the running task, you have many options which include 'Minimize' and 'Maximize'. Is there anyway to do achieve this in vb?

user959631
  • 1,004
  • 2
  • 14
  • 34

1 Answers1

5

Here is an example of the code you are looking for. It will loop through all the active processes and minimize all the windows.

In your app you will probably want to use something like Process.GetProcessesByName to find the specific window you want to manipulate.

Imports System.Runtime.InteropServices

Module ManipulateWindows

    Const SW_HIDE As Integer = 0
    Const SW_RESTORE As Integer = 1
    Const SW_MINIMIZE As Integer = 2
    Const SW_MAXIMIZE As Integer = 3

    <DllImport("User32")> _
    Private Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
    End Function

    Public Sub Main()

        'iterate through all the open processes.
        For Each p As Process In Process.GetProcesses    

            'Get the Window Handle
            Dim hWnd as integer = CType(p.MainWindowHandle, Integer)

            'Write out the title of the main window for the process.
            System.Console.WriteLine(p.MainWindowTitle)

            'Minimize the Window
            ShowWindow(hWnd, SW_MINIMIZE)
        Next p    
    End Sub    
End Module
JohnFx
  • 34,542
  • 18
  • 104
  • 162
  • Hey dude, thanks so much. Did you just make it up right now, or you have used this before? If you made it up just right now I LOVE YOU!! Lol =P Thanks – user959631 Dec 12 '11 at 22:08
  • Had used the technique before. Had to look up a few of the constants and the API method (ShowWindow), but other than that wrote this from scratch. Please, just don't use it for evil (for example creating a Super-application that minimizes everything else so it can be king of the desktop) – JohnFx Dec 12 '11 at 22:11
  • lmao king of the desktop, naa I want to use it for some anoyying thing that always pops up, then I want to close its process and then maximize my window =] Thanks anyway – user959631 Dec 12 '11 at 22:32