Edit

Share via


WindowInteropHelper.EnsureHandle Method

Definition

Creates the HWND of the window if the HWND has not been created yet.

public:
 IntPtr EnsureHandle();
[System.Security.SecurityCritical]
public IntPtr EnsureHandle ();
public IntPtr EnsureHandle ();
[<System.Security.SecurityCritical>]
member this.EnsureHandle : unit -> nativeint
member this.EnsureHandle : unit -> nativeint
Public Function EnsureHandle () As IntPtr

Returns

IntPtr

nativeint

An IntPtr that represents the HWND.

Attributes

Remarks

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.

Applies to