// Stephen Toub
// [email]stoub@microsoft.com[/email]
//
// ActiveWindow.cs v1.0.0
// 8/28/02
//
// Implements IWin32Window where Handle returns the handle of the system's foreground window.
// Can be used with MessageBox to display the box in front of the active window, such as:
// MessageBox.Show(ActiveWindow.Active, "Hello, World!");
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Toub.Windows.Forms
{
/// <summary>Used to get an IWin32Window for the active window.</summary>
public class ActiveWindow : IWin32Window
{
#region Private Members
/// <summary>Static instance to use for factory pattern.</summary>
private static ActiveWindow _window = new ActiveWindow();
#endregion
#region Construction
/// <summary>Prevent external instantiation.</summary>
private ActiveWindow() {}
#endregion
#region Public Properties
/// <summary>Gets an IWin32Window for the active window.</summary>
public static IWin32Window Active { get { return _window; } }
#endregion
#region Private Functions
/// <summary>Finds the frontmost window using the Win32 API.</summary>
[DllImport("user32.dll")]
private static extern int GetForegroundWindow();
/// <summary>Gets a handle to the active window.</summary>
System.IntPtr IWin32Window.Handle
{
get { return new System.IntPtr(GetForegroundWindow()); }
}
#endregion
}
}