5,665,355 members and growing! (14,693 online)
Email Password   helpLost your password?
Desktop Development » Dialogs and Windows » General     Intermediate License: The Code Project Open License (CPOL)

CWindow (wrapper class for the window API functions) + IuSpy

By Maxim Berezov

CWindow (wrapper class for the window API functions) + IuSpy(Spy++ analog)
C# 2.0, C#.NET, .NET 2.0, Win32, WinForms

Posted: 9 May 2008
Updated: 9 May 2008
Views: 7,049
Bookmarked: 13 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 4.50 Rating: 4.50 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 10.0%
3
1 vote, 10.0%
4
8 votes, 80.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
CWindow

Introduction

In .NET there are few of functions for working with windows (of other applications). The most part have appeared in .NET 3.0, but they are not enough. So, we have to use WinApi functions.
I introduce the CWindow class, which is the wrapper class for the window API functions.

About

I decided to make CWindow look-like the System.Windows.Forms.Form .NET class. So, the description of majority functions/properties is similar with the functions/properties with the same name of the Form class. Sometimes even the inner construction is the same.

In this article I wouldn’t describe all the functions/properties, but just those, that differ enough from the same of the Form .NET class. Description of others you can see in the CWindow code or in MSDN. Code is commented well enough, and the most part of descriptions in this article are just shorted descriptions of the functions in the class code with the explanations, when they are necessary.
I decided not to throw exceptions in CWindow at all, because mistakes could be very specific and it is better to catch them in the main code.

IuSpy program was written firstly as just an example of using CWindow class. But then, after getting of a working application, I decided to decorate it and make it comfortable for usage.
So remember, that IuSpy is not the end product. You can see, how some CWindow functions/properties work, but don't be sure, that it is the best way. For example, in TreeForm and FindWindowForm I enumerate all windows, but in TreeForm I did it in a slower way. It is just an example.
IuSpy is very simple program. Almost all controls have tooltips. That’s why I wouldn’t describe it in this article. You just can see its picture in the top of this page.

CWindow class

Static functions/properties.

Properties
TopWindow - Gets/sets the window at the top of the windows Z order.
StateChangeAnimation - Gets/sets whether windows animation, when minimizing and maximizing, is enabled.
ActiveWindow - Gets/sets the currently active window.
FocusedWindow - Gets/sets the window that has the keyboard focus.
Captured - Gets the window (if any) that has captured the mouse.
DesktopWindow - Gets the desktop window.
- Gets the collection of all the top-level windows on the screen.

Functions
FromPoint - Retrieves the window at a specified location.
FindWindow
- Overloaded. Retrieves a window whose class name and window name match the specified values.
EnumWindows - Enumerates all top-level windows on the screen. For details see EnumWindows in MSDN or description in the code.
EnumThreadWindows - Enumerates all nonchild windows associated with the specified thread. For details see EnumThreadWindows in MSDN or description in the code.
GetThreadWindows - Retrieves the collection of the top-level windows, contained within the specified thread.

Object of a class

CWindow object is just an IntPtr – a handle to a window.

Implicit operators:

CWindow -> IntPtr; IntPtr -> CWindow; CWindow -> int;

Overloaded operators:

==/!= (CWindow, CWindow), ==/!= (CWindow, IntPtr).
So you can use this in such a way:
IntPtr p;
CWindow w,w2;
w = new CWindow(p);
w = p;
p = w;
w = 0x15864;
if(w == w2)…
if(w != p)… 

Window Info

TextUnsafe - Gets/sets the text associated with this window. The simple Text property can’t get/set the text of the most window types. TextUnsafe sends a message to the specified window, so your application can hang if that window is not responding. Check IsHung before using this property.
Exists - Determines whether this object identifies an existing window.
Menu - Gets/sets a handle to the menu assigned to the window.
ThreadId - Gets the identifier of the thread that created this window.
ProcessId - Gets the identifier of the process that created the window.
IsUnicodeWindow - Determines whether the specified window is a native Unicode window.
IsHung - Determine if Microsoft Windows considers that a specified application is not responding.
ClassName - Gets the name of the class to which the specified window belongs.
RealClassName - Gets a string that specifies the window type.

You can get icons with the following properties:
Icon

SmallIcon
ClassIcon

SmallClassIcon
Be careful with these properties, check IsHung first.
If you want to set your icon, for proper work you need to write it to the memory space of the process, to which the window belongs to. You can do this with WriteProcessMemory(). But don't forget, that you need also to destroy the old icon. You have to call DestroyIcon() from the space of window's process. For these operations you have to inject your Win32 DLL with the help of hooks. I use for this some of my other classes, and I decided not to put it all together.

Styles

Styles
UpdateStyles()

GetStyle()
SetStyle()

You can get window styles with the help of GetStyle() and set them with SetStyle(). If you need to get or set styles several times for the same window, you can use the Styles property and the object of WindowStyle class, returned by it. Don’t forget, that you need not only to set styles to a window, but also to apply them. You can do this with UpdateStyles(), or specify this in the SetStyle() function.

Extended Styles
ExStyles
UpdateExStyles()

GetExStyle()
SetExStyle()

Description is the same as for the simple styles.

State

Properties
WindowState - Gets/sets the window's state.
WindowVisibleState - Gets/sets the window's visible state. Implements the same as Form.WindowState. Sometimes it differs from real window state. For changing real current state use WindowState.
RestoreToMaximized - Gets/sets a value indicating whether the window is maximized in restored state.

Functions
Minimize - Minimizes the window.
Restore - Restores the window from minimized to its previous state

Window Relations

Properties
Children - Gets the collection of windows contained within the window. It contains only immediate child windows.
IsTopWindow - If current window is a top-level window, checks if it is at the top of the Z order. If the window is a child window, checks if it is at the top of it parent's Z order.
ThreadWindows - Gets the collection of the top-level windows contained within the window's thread.
TopChild - Gets the child window at the top of the Z order, if this window is a parent window; otherwise, returns null-window.
TopLevelWindow - Retrieves the root window by walking the chain of parent windows.

Functions
EnumChildWindows - Enumerates the child windows that belong to this window.
EnumThreadWindows - Enumerates all nonchild windows associated with the current thread.
FindChildWindow - Overloaded. Retrieves the window, whose class name and window name match the specified values.
GetNextWindow - Retrieves a window that has the specified relationship (Z-Order or owner) to this window.
InsertAfter - Inserts this window after the specified in the Z order.

Other

Transparent - Gets/sets weather the window is considered to be transparent. Can be used for hit testing or for drawing underlayered windows.
ForceActivate() - Activates the window and gives it focus. Activate() function can’t activate the window in some cases, for example, when a popup window is shown. So you can use ForceActivate() function, if you want to activate a window in any case.

Namesakes

The description of the following properties/function you can see in the class code or in MSDN. Remember, that their real description may not fully repeat MSDN description. So with MSDN description you can decide, whether it is the function that you need, and then see the specification in the class code.

Window Relations
HasChildren
Owner
Parent

TopLevel
BringToFront()
Contains()
GetChildAtPoint()
SendToBack()
SetTopLevel()

Coordinats
Bottom
Bounds
ClientRectangle
ClientSize
DesktopBounds
DesktopLocation
Height
Left
Location
MaximizedBounds
MaximumSize
MinimumSize
RestoreBounds
Right
Size
Top
Width
PointToClient()
PointToScreen()
RectangleToClient()
RectangleToScreen()

Visibility
AllowTransparency
Opacity
Region
TransparencyKey
Visible
Hide()
Invalidate()
Refresh()
Show()
Update()

Window Info
Handle
Text
Enabled
Modal
CanFocus
Focused
ContainsFocus
Capture
ShowInTaskbar

Others
Activate()
Close()
CreateGraphics()
Focus()

Additional classes:

CWindowWorker, CWindowsWorker

These classes were made not big. They contain only basic features, with the help of which you can try to implement everything what you need.

CWindowWorker

You can use CWindowWorker class if you want to change several properties of a window in a single screen-refreshing cycle. The constructor is simple. Just put there the CWindow object or a handle to a window. With this class you can change Location, Size, Visible state of the window Activate(), set styles and extanded styles and set the window order.

You can set window styles with SetStyle() or SetExStyle(), but they will be applied to a window, when you'll call the Reposition() function.

SetOrder() Overloaded. It changes the window’s placement in the Z-order. But remember, that you can use this function only once for current reposition. This is the only function in the Iu.Windows namespace, where I threw an exception if this function called more than once for the current object, because this is obvious developer mistake.

Call RePosition() after all to apply everything what you have specified.

CWindowsWorker

Use this class, when you want to change properties of several windows in a single screen-refreshing cycle. This class have only the indexer and the Reposition() function.

You can specify a CWindow object or a handle in the indexer and you will get an object of CWindowWorker class for a specified window. You can specify everything what you want in returned object, but don’t call Reposition().

At the end, after changing of all the windows, call the Reposition() function of the CWindowsWorker class to apply all changes.

WindowStyle, WindowExStyle

Styles and ExStyles properties of CWindow class return objects of aforesaid classes. They were written to check/set specified styles. They are useful, when you need to check/set styles several times for the same window. Both of this classes have Check() and Set() functions and a lot of implicit operators.

License

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

About the Author

Maxim Berezov



Location: Ukraine Ukraine

Other popular Dialogs and Windows articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralQuestion ?memberBnR-Fouress6:05 18 Jun '08  
GeneralRe: Question ?memberBnR-Fouress1:25 19 Jun '08  
QuestionQuestionmemberJHONPABLO4:18 13 May '08  
AnswerRe: QuestionmemberMaxim Berezov2:35 14 May '08  
GeneralGreat utility, Very useful. Thank you.memberMishel1:34 13 May '08  
GeneralHelp!!1memberdurbich1:18 13 May '08  
AnswerRe: Help!!1memberMaxim Berezov2:31 14 May '08  
GeneralRe: Help!!1memberdurbich2:54 14 May '08  
GeneralNicememberMember 252246410:43 9 May '08  
AnswerRe: NicememberMaxim Berezov11:17 9 May '08  
GeneralRe: Nicememberhardsoft12:08 9 May '08  
AnswerRe: Nice [modified]memberMaxim Berezov12:28 9 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 May 2008
Editor:
Copyright 2008 by Maxim Berezov
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project