ARTICLE

Controlling Window State Of Other Applications using VB.NET

Posted by Gaurav Pilay Articles | Visual Basic 2010 January 05, 2007
This article shows how you can control window state of any other application using VB.NET.
 
Reader Level:

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

  1. Imports System.Runtime.InteropServices

  2. 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/).

share this article :
post comment
 

Oh I was thinking this can be helped, well anyway I was using another application called sildenafil, but this one looks better, thanks a lot.

Posted by Sara Yumi Dec 01, 2010

The above article is very interesting, and makes me wonder if there is a way in VB to also determine the state and location of the form belonging to another application that is active, and attach a new form (could be just a dumb display of text or graphics) from my application to it? Regards, sk

Posted by s k Sep 14, 2008

Going a step farther. Can I control an application using VB.NET as if I was interacting with that application? I have a CCTV application that monitors 4 cameras. The application will take a jpg snapshot by right clicking the image and selecting "capture". Can I use VB.NET to automate this process by interacting with the application to select the image then input the required right click selection? Years ago I use to automate software installation using a program called WinBatch that could interact with a running application to control it.

Posted by Mike Suhar Feb 03, 2008

Hi There is a bug in this code hWnd = CType(p(0).MainWindowHandle, Integer) Should read hWnd = p(0).MainWindowHandle.ToInt32

Posted by Bones Dec 18, 2007

Hej First of all thank you for your toturial. My Name is Bishi, and i come from Iran. I am a part time programmer right now(I came out of work as a programer last year). My problem is : I want to make a Bidirectional Web TextBox. Can you please help me get started. I have seen it done, but i just cant fine anything on this subject anymore... My Email is : BizhanSE@hotmail.com

Posted by Bishi sader Mar 27, 2007
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor