Export (0) Print
Expand All

WindowInteropHelper.EnsureHandle Method

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

Namespace:  System.Windows.Interop
Assembly:  PresentationFramework (in PresentationFramework.dll)

public IntPtr EnsureHandle()

Return Value

Type: System.IntPtr
An IntPtr that represents the HWND.

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.

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Show:
© 2015 Microsoft