Article C0002 C# .NET
Window Management, using class with unmanaged code

Pop-up killers and other similar tools like KillAfter, which can be found on uTools, have direct access to the Windows forms / applications. This functionality is currently not available within the .NET Framework, but can be accessed easily using unmanaged code. Windows are created in Z-Order, you can iterate within this list using the GetWindow function. If you're interested in the Z-Order creation details you can read the following MSDN article.

The class:

//
// Edward Willemsen, 2007
//
// This class defines several routines to the unmanaged
// code required to access windows forms or application.
//
// The class required to have access to the unmanaged code:
public class UnManaged
{
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
public StringBuilder Buff = new StringBuilder(256);
	[DllImport("user32.dll")]
	public static extern IntPtr GetTopWindow(int hWnd);
	[DllImport("user32.dll")]
	public static extern Boolean IsWindowVisible(IntPtr hWnd);
	[DllImport("user32.dll", CharSet = CharSet.Auto)]
	public static extern IntPtr GetWindowText(IntPtr hWnd,
                             StringBuilder Buff, int nMaxCount);
	[DllImport("user32.dll")]
	public static extern IntPtr GetWindow(IntPtr hWnd, UInt16 GW_HWNDNEXT);
	[DllImport("user32.dll", CharSet = CharSet.Auto)]
	public static extern IntPtr SendMessage(IntPtr hWnd, int msg,
                                            int wParam, IntPtr lParam);
}
Using the following manner you can call these routines so that your managed code can use the information and respond as desired:

// Get the Top Window (usually called 'start'):
IntPtr hWnd = UnManaged.GetTopWindow(0);
// Get the caption text of the selected Window:
UnManaged.GetWindowText(hWnd, winCap, 256);
// Check if a Window is visible (returns a boolean):
UnManaged.IsWindowVisible(hWnd);
// Get the next Window in the Windows list:
UnManaged.GetWindow(hWnd, GW_HWNDNEXT);
// To manage a Window send a message to it:
UnManaged.SendMessage(hWnd, UnManaged.WM_SYSCOMMAND,
    UnManaged.SC_CLOSE, IntPtr.Zero);
The following table explains the codes which can be used using the GetWindow function:

GW_HWNDNEXT
GetWindow returns the handle to the given window's next sibling. When an application calls GetWindow with GW_HWNDNEXT, it receives the same result that calling GetNextWindow with GW_HWNDNEXT returns.

GW_HWNDFIRST
GetWindow returns the handle to the given window's topmost sibling in the Z order. This is the most visible sibling window. When an application calls GetWindow with GW_HWNDFIRST, it receives the same result that calling GetTopWindow returns.

GW_HWNDLAST
GetWindow returns the given window's bottommost sibling in the Z order. This is the least visible sibling.

GW_HWNDPREV
GetWindow returns the handle to the given window's previous sibling. When an application calls GetWindow with GW_HWNDPREV, it receives the same result that calling GetNextWindow with GW_HWNDPREV returns.

GW_OWNER
GetWindow returns the handle to the given window's owner. If the given window has no owner, GetWindow returns NULL.

GW_CHILD
GetWindow returns the handle to the given window's first child window. The first child window is at the top of the given window's Z order. If the given window has no child window, the GetWindow function returns NULL.

The next table explains the commands which can be send to a window. These command need the WM_SYSCOMMAND as shown in the previous example code:

SC_CLOSE
Closes the window.

SC_CONTEXTHELP
Changes the cursor to a question mark with a pointer. If the user then clicks a control in the dialog box, the control receives a WM_HELP message.

SC_DEFAULT
Selects the default item; the user double-clicked the window menu.

SC_HOTKEY
Activates the window associated with the application-specified hot key. The lParam parameter identifies the window to activate.

SC_HSCROLL
Scrolls horizontally.

SC_KEYMENU
Retrieves the window menu as a result of a keystroke.

SC_MAXIMIZE
Maximizes the window.

SC_MINIMIZE
Minimizes the window.

SC_MONITORPOWER
Sets the state of the display. This command supports devices that have power-saving features, such as a battery-powered personal computer. The lParam parameter can have the following values: 1 - the display is going to low power, 2 - the display is being shut off.

SC_MOUSEMENU
Retrieves the window menu as a result of a mouse click.

SC_MOVE
Moves the window.

SC_NEXTWINDOW
Moves to the next window.

SC_PREVWINDOW
Moves to the previous window.

SC_RESTORE
Restores the window to its normal position and size.

SC_SCREENSAVE
Executes the screen saver application specified in the [boot] section of the System.ini file.

SC_SIZE
Sizes the window.

SC_TASKLIST
Activates the Start menu.

SC_VSCROLL
Scrolls vertically.

~Edward