65.9K
CodeProject is changing. Read more.
Home

WindowClass

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.92/5 (14 votes)

Nov 28, 2008

CPOL

2 min read

viewsIcon

37544

downloadIcon

362

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