Click here to Skip to main content
12,405,700 members (64,837 online)
Click here to Skip to main content
Add your own
alternative version

Stats

21.7K views
332 downloads
31 bookmarked
Posted

WindowClass

, 9 Feb 2009 CPOL
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]


You may also be interested in...

Comments and Discussions

 
NewsWhy 4/5 but really good Pin
MegaTen16-Oct-10 11:41
memberMegaTen16-Oct-10 11:41 
QuestionCan this class be used to get a collection of the desktop window? Pin
DukeForge17-Feb-09 17:11
memberDukeForge17-Feb-09 17:11 
AnswerRe: Can this class be used to get a collection of the desktop window? Pin
Kristian Sixhoej17-Feb-09 20:01
memberKristian Sixhoej17-Feb-09 20:01 
GeneralGood Idea but Pin
Fabrice CARUSO10-Feb-09 20:14
memberFabrice CARUSO10-Feb-09 20:14 
GeneralRe: Good Idea but Pin
Kristian Sixhoej10-Feb-09 22:15
memberKristian Sixhoej10-Feb-09 22:15 
GeneralMy Vote of 2 Pin
JeffOSh10-Feb-09 6:11
memberJeffOSh10-Feb-09 6:11 
GeneralRe: My Vote of 2 Pin
Kristian Sixhoej10-Feb-09 6:14
memberKristian Sixhoej10-Feb-09 6:14 
Generalgreat Pin
pimb210-Feb-09 5:23
memberpimb210-Feb-09 5:23 
GeneralRe: great Pin
Kristian Sixhoej10-Feb-09 5:26
memberKristian Sixhoej10-Feb-09 5:26 
GeneralMy vote of 3 Pin
johannesnestler10-Feb-09 1:48
memberjohannesnestler10-Feb-09 1:48 
GeneralMy vote of 1 Pin
osy28-Nov-08 22:21
memberosy28-Nov-08 22:21 
GeneralRe: My vote of 1 Pin
Kristian Sixhoej28-Nov-08 23:20
memberKristian Sixhoej28-Nov-08 23:20 
GeneralRe: My vote of 1 Pin
Kenan E.K.29-Nov-08 1:49
memberKenan E.K.29-Nov-08 1:49 
GeneralRe: My vote of 1 Pin
Kristian Sixhoej29-Nov-08 23:10
memberKristian Sixhoej29-Nov-08 23:10 
GeneralMy vote of 1 Pin
g0got228-Nov-08 10:38
memberg0got228-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 10 Feb 2009
Article Copyright 2008 by Kristian Sixhoej
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid