Minimizing Other Windows From DotNet Application

Initiotech
Posted by in Windows Forms category on for Intermediate level | Views : 13064 red flag

The article will make use of the API " ShowWindow " from the user32 Dll and allow users to Minimise,Maximize and manuplate other windows from your DotNet Application


 Download source code for Minimizing Other Windows From DotNet Application

Introduction

The Following code will help you to minimize other Windows from your .NET Application.
To Demonstrate I have developed demo application which will first get all the running process in the Combobox
Namespaces You Need to Import are


System.Diagnostics - For Processes
System.Runtime.InteropServices - For defining the API

The Below Code in the Load Event Will Get the List of all Processes and Add it in the ComboBox


For Each p As Process In Process.GetProcesses()
If (ComboBox1.Items.Contains(p.ProcessName) = False) Then
ComboBox1.Items.Add(p.ProcessName)
End If
Next

For the Minimize,Maximize and other functions  to Work We need to Define an API Method ShowWindow from user32.dll


<DllImport("User32.dll")>_
Public Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal cmd As Integer) As Boolean
End Function

Once you have defined the API now its time to use it
Parameter Info
hwnd - The Window Handle on which the opperations will take place
cmd - An integer value which will hold the options wether to Minimise ,Maximise etc..

The Following Options can be used in the cmd parameter.


Hide = 0,
ShowNormal = 1,
ShowMinimized = 2,
ShowMaximized = 3,
Maximize = 3,
ShowNormalNoActivate = 4,
Show = 5,
Minimize = 6,
ShowMinNoActivate = 7,
ShowNoActivate = 8,
Restore = 9,
ShowDefault = 10,
ForceMinimized = 11*

Then I made a Button which will minimize all windows the code to Minimise all is


For Each p As Process In Process.GetProcesses()
ShowWindow(p.MainWindowHandle, 6)
Next

Then I made a button which will minimize Process selected from the combobox


Dim p() As Process = Process.GetProcessesByName(ComboBox1.SelectedItem)
For Each pr As Process In p
ShowWindow(pr.MainWindowHandle, 6) ' 6 used to Minimise
Next

Then I made a button which will maximise the Process selected from the combobox


Dim p() As Process = Process.GetProcessesByName(ComboBox1.SelectedItem)
For Each pr As Process In p
ShowWindow(pr.MainWindowHandle, 9) ' 9 used for Restoring
Next

You can use the Other Options for other purpose.

For More Demonstration you can download the Full Project

Hope this Article Helped you.

Regards
Hefin Dsouza

Page copy protected against web site content infringement by Copyscape

About the Author

Initiotech
Full Name: Hefin Dsouza
Member Level: Starter
Member Status: Member
Member Since: 9/16/2009 12:32:23 PM
Country: India

http://hefin.in
My Name is Hefin Dsouza. I am a faculty at Computer Education Institute.I am also an Freelance Software Developer and Software Trainner.I am working with DotNet Technologies from the past 8 years.I am an Microsoft Certified Windows Application Developer for .NET 3.5. And also a Blogger at http://blog.hefin.in

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)