Click here to Skip to main content
Click here to Skip to main content
Go to top

WindowClass

, 9 Feb 2009
Rate this:
Please Sign up or sign in to vote.
A class that provides methods for finding and manipulating windows

Introduction

The WindowClass is a small class that makes it easy to find and manipulate windows, such as hiding/showing, bringing them to foreground, changing their title and so on.
This class provides some easy-to-use functions for this. 

Using the Code

Using the code is very easy. The first thing you wanna do, is add the class to your project, then add a reference to it, using the using keyword.

using WindowClass;	

To find a window, you simply create a new instance of the Window class:

Window myWindow = new Window("Untitled - Notepad");

In this case, "Untitled - Notepad" is the title (the text in the title bar) of the window we're finding. It is also possible to find windows by classname: 

Window myWindow = new Window(null, "Notepad");

In both cases, we create a new instance of Window containing the handle, classname and title of the window. In order to get these properties, we would do this:

// Get the handle for the window.
IntPtr handle = myWindow.Handle;

// Get the classname for the window.
string classname = myWindow.ClassName;

// Get the title for the window.
string title = myWindow.Title;

Properties

The class has the following properties:

  • ClassName - Returns the classname for the window
  • Handle - Returns a handle for the window
  • Title - Gets or sets the title of the window
  • Position - Gets or sets the position of the window

Manipulating windows

Now we're getting to the interesting part - manipulating the windows.

The Window class gives you the ability to: 

  • Hide windows
  • Show windows
  • Maximize windows
  • Minimize windows
  • Bring windows to foreground
  • Change the title of windows
  • Move windows around 
  • Retrieve the position of windows

To do all (except changing the title and the position) of the things above, the class provides a method to do it in a very easy way - the SetWindowState method.

This is what the SetWindowState method looks like:

public void SetWindowState(IntPtr handle, WindowState windowstate)
{
    switch (windowstate)
    {
        case WindowState.Hidden:
            {
                ShowWindow(handle, SW_HIDE);
                break;
            }
(...)  

It takes two parameters. A handle for the window to perform the operation on, and an enum telling the method what to do with the window. So as to minimize a window, you call the SetWindowState method, passing in the handle and the state the window should be set to:

// Minimize the window.
myWindow.SetWindowState(myWindow.Handle, Window.WindowState.Minimized); 

The last two things I will cover are how to change the title of the window and changing the position of the window, which is even easier.
The WindowClass has a Title property which makes it very easy to do this.

myWindow.Title = "Notepad - CodeProject edition";

So now the Notepad window should be titled "Notepad - CodeProject edition".

It works the same way with the position:

myWindow.Position = new Position(0, 0); 

This will move the window's TOP-LEFT corner to 0,0 (top-left corner of the screen).

Points of Interest

The reason why I wrote this class is because I wanted an easy way to find and manipulate windows, so I didn't have to use API calls to achieve this. The class itself uses API calls to do it, but I don't have to worry about using them, since the class handles all the dirty work through easy-to-use methods.

History

  • 27 November 2008: First version
  • 10 February 2009: Updated version with ability to change and retrieve the window's position

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kristian Sixhoej
Student
Denmark Denmark

New biography coming sometime.
Green Alien | [Alien]


Comments and Discussions

 
NewsWhy 4/5 but really good PinmemberMegaTen16-Oct-10 11:41 
QuestionCan this class be used to get a collection of the desktop window? PinmemberDukeForge17-Feb-09 17:11 
AnswerRe: Can this class be used to get a collection of the desktop window? PinmemberKristian Sixhoej17-Feb-09 20:01 
GeneralGood Idea but PinmemberFabrice CARUSO10-Feb-09 20:14 
GeneralRe: Good Idea but PinmemberKristian Sixhoej10-Feb-09 22:15 
GeneralMy Vote of 2 PinmemberJeffOSh10-Feb-09 6:11 
GeneralRe: My Vote of 2 PinmemberKristian Sixhoej10-Feb-09 6:14 
Generalgreat Pinmemberpimb210-Feb-09 5:23 
GeneralRe: great PinmemberKristian Sixhoej10-Feb-09 5:26 
GeneralMy vote of 3 Pinmemberjohannesnestler10-Feb-09 1:48 
GeneralMy vote of 1 Pinmemberosy28-Nov-08 22:21 
GeneralRe: My vote of 1 PinmemberKristian Sixhoej28-Nov-08 23:20 
GeneralRe: My vote of 1 PinmemberKenan E.K.29-Nov-08 1:49 
GeneralRe: My vote of 1 PinmemberKristian Sixhoej29-Nov-08 23:10 
GeneralMy vote of 1 Pinmemberg0got228-Nov-08 10:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141002.1 | Last Updated 10 Feb 2009
Article Copyright 2008 by Kristian Sixhoej
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid