Click here to Skip to main content
Licence CPOL
First Posted 28 Nov 2008
Views 13,960
Downloads 169
Bookmarked 27 times

WindowClass

By | 9 Feb 2009 | Article
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)

About the Author

Kristian Sixhoej



Denmark Denmark

Member

New biography coming sometime.
Green Alien | [Alien]



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
NewsWhy 4/5 but really good PinmemberMegaTen11:41 16 Oct '10  
QuestionCan this class be used to get a collection of the desktop window? PinmemberDukeForge17:11 17 Feb '09  
AnswerRe: Can this class be used to get a collection of the desktop window? PinmemberKristian Sixhoej20:01 17 Feb '09  
GeneralGood Idea but PinmemberFabrice CARUSO20:14 10 Feb '09  
GeneralRe: Good Idea but PinmemberKristian Sixhoej22:15 10 Feb '09  
GeneralMy Vote of 2 PinmemberJeffOSh6:11 10 Feb '09  
GeneralRe: My Vote of 2 PinmemberKristian Sixhoej6:14 10 Feb '09  
Generalgreat Pinmemberpimb25:23 10 Feb '09  
GeneralRe: great PinmemberKristian Sixhoej5:26 10 Feb '09  
GeneralMy vote of 3 Pinmemberjohannesnestler1:48 10 Feb '09  
GeneralMessage Automatically Removed Pinmemberosy22:21 28 Nov '08  
GeneralRe: My vote of 1 PinmemberKristian Sixhoej23:20 28 Nov '08  
GeneralRe: My vote of 1 PinmemberKenan E.K.1:49 29 Nov '08  
GeneralRe: My vote of 1 PinmemberKristian Sixhoej23:10 29 Nov '08  
GeneralMessage Automatically Removed Pinmemberg0got210:38 28 Nov '08  

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.

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