WindowInteropHelper.EnsureHandle Method
Creates the HWND of the window if the HWND has not been created yet.
Assembly: PresentationFramework (in PresentationFramework.dll)
Use the EnsureHandle method when you want to separate window handle (HWND) creation from the actual showing of the managed Window. This is useful when you have an automation client that can accomplish its tasks without the need for showing a window.
If the native window has not yet been created, this method creates the native window, sets the Handle property, and returns the HWND. If the native window has been created already, the handle of the existing native window is returned.
If the native window is created as a result of calling this method, the SourceInitialized event is raised.
Querying the Handle property after the EnsureHandle method is called returns the existing window handle. The visual tree is not attached to the window until after the Show method is called.
Calling the EnsureHandle method more than one time does not create new window handles. Calling the EnsureHandle method when the handle has already been created by a call to the Show method does not create a new window handle. A native window is only created when no handle exists when the EnsureHandle method is called.
Window properties that are set by using native window APIs via p/invoke may not appear in the managed window APIs. For example, if you set the window to be topmost by using the native MS_EX_TOPMOST flag after the EnsureHandle method is called, the Topmost property is not guaranteed to reflect the native setting.
-
UIPermission
to create and access a window handle. Associated enumeration: AllWindows
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
using System.Reflection;
namespace System.Windows.Interop
{
/// <summary>
/// Provides NetFX 4.0 EnsureHandle method for
/// NetFX 3.5 WindowInteropHelper class.
/// </summary>
public static class WindowInteropHelperExtensions
{
/// <summary>
/// Creates the HWND of the window if the HWND has not been created yet.
/// </summary>
/// <param name="helper">An instance of WindowInteropHelper class.</param>
/// <returns>An IntPtr that represents the HWND.</returns>
/// <remarks>Use the EnsureHandle method when you want to separate
/// window handle (HWND) creation from the
/// actual showing of the managed Window.</remarks>
public static IntPtr EnsureHandle(this WindowInteropHelper helper)
{
if (helper == null)
throw new ArgumentNullException("helper");
if (helper.Handle == IntPtr.Zero)
{
var window = (Window)typeof(WindowInteropHelper).InvokeMember("_window",
BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
null, helper, null);
typeof(Window).InvokeMember("SafeCreateWindow",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
null, window, null);
}
return helper.Handle;
}
}
}
- 1/27/2011
- Alexander Yudakov