Saturday, July 28, 2012

How to minimize a window by passing the process name

The VB.Net code posted on this page minimizes the window for the given process name.

  1. Module modMain  
  2. #Region "Declarations for Minimizing Windows"  
  3.   Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As SHOW_WINDOW) As Boolean  
  4.   
  5.   Private Enum SHOW_WINDOW As Integer  
  6.     SW_HIDE = 0  
  7.     SW_SHOWNORMAL = 1  
  8.     SW_NORMAL = 1  
  9.     SW_SHOWMINIMIZED = 2  
  10.     SW_SHOWMAXIMIZED = 3  
  11.     SW_MAXIMIZE = 3  
  12.     SW_SHOWNOACTIVATE = 4  
  13.     SW_SHOW = 5  
  14.     SW_MINIMIZE = 6  
  15.     SW_SHOWMINNOACTIVE = 7  
  16.     SW_SHOWNA = 8  
  17.     SW_RESTORE = 9  
  18.     SW_SHOWDEFAULT = 10  
  19.     SW_FORCEMINIMIZE = 11  
  20.     SW_MAX = 11  
  21.   End Enum  
  22. #End Region  
  23.   
  24.   Sub Main()  
  25.     'How to call this function  
  26.     MinimizeWindows("acrobat"'This will minimize the acrobat application  
  27.   End Sub  
  28.   
  29.   Public Sub MinimizeWindows(ByVal sProcessName As String)  
  30.     Dim p As Process  
  31.     For Each p In Process.GetProcessesByName(sProcessName)  
  32.       ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_MINIMIZE)  
  33.     Next p  
  34.   End Sub  
  35. End Module

1 comment: