HwndSource.FromHwnd Method (IntPtr)

 

Returns the HwndSource object of the specified window.

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

[SecurityCriticalAttribute]
public static HwndSource FromHwnd(
	IntPtr hwnd
)

Parameters

hwnd
Type: System.IntPtr

The provided window handle.

Return Value

Type: System.Windows.Interop.HwndSource

The HwndSource object for the window that is specified by the hwnd window handle.

System_CAPS_security Security Note

This method is not available in the Internet security zone, or for partial trust in general.

You can use this method to return an HwndSource for a window that is not explicitly an interoperation window. The procedure for this is:

  1. Create a WindowInteropHelper instance (providing the main Window as a constructor parameter).

  2. Get the value of the Handle property from that WindowInteropHelper instance.

  3. Pass that HWND value as a parameter to FromHwnd.

This technique can be useful if you then want to add general AddHook message processing to the window. However, whenever you create an HwndSource, you are also responsible for destroying it. This is true even if the Application object for an application HwndSource is disposed. For more information, see Remarks on HwndSource.

UIPermission

to call this method. Associated enumeration: AllWindows

.NET Framework
Available since 3.0
Return to top
Show: