One of my friend wanted to control the window state of some application using a VB.NET code. That's why I am writing this article. This also includes Win32 functions.
To start with include namespaces
-
Imports
System.Runtime.InteropServices -
Imports
System.Diagnostics
Now declare these variables
Private Const SW_HIDE As Integer = 0
Private Const SW_RESTORE As Integer = 9
Private hWnd As Integer
Now declare win32 function ShowWindow <DllImport("User32")> _
Private Shared Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
The above function accepts 2 parameters hWnd is handle of a window whose window state we want to modify nCmdShow contains integer value which denotes state Following table will clear it
| Sr No | nCmdShow | Value |
| 1 | Hide | 0 |
| 2 | Minimized | 1 |
| 3 | Maximized | 2 |
| 4 | Restore | 9 |
Now the main problem is how to get handle of a particular window. Its simple, suppose you want to get handle of notepad application which is running on your machine then use following code
Private p As Process() = Process.GetProcessesByName("notepad")
This will return array of all the notepad processes. After this you can use foreach loop to iterate through each process in this array. Here we will just consider first process in array.
The following line of code will return me handle value
hWnd = CType(p(0).MainWindowHandle, Integer)
Now if you want to hide this process the just give a call to ShowWindow method as follows ShowWindow(hWnd, SW_HIDE)
That' it. Notepad will vanish..
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/).