Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
9 May 2008 1  
CWindow (a wrapper class for the window API functions) + IuSpy(Spy++ analog).

CWindow

Introduction

In .NET, there are a few 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 the WinAPI functions. I introduce here the CWindow class, which is the wrapper class for the required window API functions.

About

I decided to make CWindow look like the System.Windows.Forms.Form .NET class. So, the description of the majority of the functions/properties is similar to the functions/properties with the same name from 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. The description for others can be obtained from the CWindow code or from MSDN. The code is commented well enough, and most of the descriptions in this article are just short descriptions of the functions in the class code with the explanations. 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.

The IuSpy program was written first as just an example of using the CWindow class. But then, after getting a working application, I decided to decorate it and make it comfortable for usage. So remember, 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 fashion. It is just an example. IuSpy is a very simple program. Almost all controls have tooltips. That’s why I wouldn’t describe it in this article. You 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 window's 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.

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 the description in the code.
  • EnumThreadWindows - Enumerates all non-child windows associated with the specified thread. For details, see EnumThreadWindows in MSDN, or the description in the code.
  • GetThreadWindows - Retrieves the collection of top-level windows contained within the specified thread.

Object of a Class

The 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 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 - Determines if Microsoft Windows considers that a specified application is not responding.
  • ClassName - Gets the name of the class to which the specified window belongs to.
  • 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, 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 also need to destroy the old icon. You have to call DestroyIcon() from the space of the window's process. For these operations, you have to inject your Win32 DLL with the help of hooks. I use some of my other classes for this, and I decided not to put them 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 an object of the WindowStyle class returned by it. Don’t forget that you not only need to set the styles to a window, but also 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. Implemented the same as Form.WindowState. Sometimes it differs from the real window state. For changing the real current state, use WindowState.
  • RestoreToMaximized - Gets/sets a value indicating whether the window is maximized in the 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 the immediate child windows.
  • IsTopWindow - If the 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 its 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 non-child 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 window in the Z order.

Other

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

Namesakes

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

WindowRelations

  • HasChildren
  • Owner
  • Parent
  • TopLevel
  • BringToFront()
  • Contains()
  • GetChildAtPoint()
  • SendToBack()
  • SetTopLevel()

Coordinates

  • 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 not made big. They contain only basic features, with the help of which you can try to implement everything that you need.

CWindowWorker

You can use the 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 the Location, Size, and Visible state of the window, set styles and extended styles, and also set the window order.

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

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

Call RePosition() at the end to apply everything that you have specified.

CWindowsWorker

Use this class when you want to change properties of several windows in a single screen-refreshing cycle. This class has 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 the CWindowWorker class for a specified window. You can specify everything that you want in the returned object, but don’t call Reposition().

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

WindowStyle, WindowExStyle

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here