Click here to Skip to main content
15,884,838 members
Articles / Desktop Programming / Win32

WindowClass

Rate me:
Please Sign up or sign in to vote.
3.92/5 (14 votes)
9 Feb 2009CPOL2 min read 36.6K   360   31   15
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.

C#
using WindowClass;	

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

C#
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: 

C#
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:

C#
// 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:

C#
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:

C#
// 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.

C#
myWindow.Title = "Notepad - CodeProject edition";

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

It works the same way with the position:

C#
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)


Written By
Student
Denmark Denmark

New biography coming sometime.
Green Alien | [Alien]


Comments and Discussions

 
NewsWhy 4/5 but really good Pin
MegaTen16-Oct-10 11:41
MegaTen16-Oct-10 11:41 
QuestionCan this class be used to get a collection of the desktop window? Pin
DukeForge17-Feb-09 17:11
DukeForge17-Feb-09 17:11 
AnswerRe: Can this class be used to get a collection of the desktop window? Pin
Kristian Sixhøj17-Feb-09 20:01
Kristian Sixhøj17-Feb-09 20:01 
GeneralGood Idea but Pin
Fabrice CARUSO10-Feb-09 20:14
Fabrice CARUSO10-Feb-09 20:14 
GeneralRe: Good Idea but Pin
Kristian Sixhøj10-Feb-09 22:15
Kristian Sixhøj10-Feb-09 22:15 
GeneralMy Vote of 2 Pin
JeffOSh10-Feb-09 6:11
JeffOSh10-Feb-09 6:11 
GeneralRe: My Vote of 2 Pin
Kristian Sixhøj10-Feb-09 6:14
Kristian Sixhøj10-Feb-09 6:14 
Generalgreat Pin
pimb210-Feb-09 5:23
pimb210-Feb-09 5:23 
GeneralRe: great Pin
Kristian Sixhøj10-Feb-09 5:26
Kristian Sixhøj10-Feb-09 5:26 
GeneralMy vote of 3 Pin
johannesnestler10-Feb-09 1:48
johannesnestler10-Feb-09 1:48 
GeneralMy vote of 1 Pin
osy28-Nov-08 22:21
osy28-Nov-08 22:21 
GeneralRe: My vote of 1 Pin
Kristian Sixhøj28-Nov-08 23:20
Kristian Sixhøj28-Nov-08 23:20 
GeneralRe: My vote of 1 Pin
Kenan E.K.29-Nov-08 1:49
professionalKenan E.K.29-Nov-08 1:49 
GeneralRe: My vote of 1 Pin
Kristian Sixhøj29-Nov-08 23:10
Kristian Sixhøj29-Nov-08 23:10 
GeneralMy vote of 1 Pin
g0got228-Nov-08 10:38
g0got228-Nov-08 10:38 

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

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