Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

HwndSource Class

Presents Windows Presentation Foundation (WPF) content within a Win32 window.

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

public class HwndSource : PresentationSource, IDisposable, IWin32Window, IKeyboardInputSink
public class HwndSource extends PresentationSource implements IDisposable, IWin32Window, 
	IKeyboardInputSink
public class HwndSource extends PresentationSource implements IDisposable, IWin32Window, 
	IKeyboardInputSink
You cannot use this managed class in XAML.
Security noteSecurity Note:

Many members of this class are not available in the Internet security zone.

An HwndSource implements a Win32 window that can contain WPF content.  The WPF content in the window is arranged, measured, rendered, and is interactive to input.  Because the HwndSource is specifically designed to interoperate with Win32, this class exposes a number of low-level Win32 features, including:

  • Specifying window class styles

  • Specifying window styles

  • Specifying extended window styles

  • Hooking the window procedure

  • Access to the window handle (HWND)

  • Destroying the window

The HwndSource class is designed for general interoperability, and is not designed as a managed HWND wrapper.  In general, it does not provide managed methods for manipulating the window or properties for inspecting its state.  Instead, the HwndSource class provides access to the Win32 window handle (HWND) via the Handle property, which can be passed via PInvoke techniques to Win32 APIs in order to manipulate the window.

Construction

Many aspects of the HwndSource can only be specified at construction time.  To create an HwndSource, first create an HwndSourceParameters structure and populate it with the desired parameters.  These parameters include:

  • Class, window, and extended window styles.  You must PInvoke to change the styles after the window is created.  Not all styles can be changed after the window has been created.  Consult the Win32 documentation before changing window styles.

  • Initial position of the window.

  • Initial size of the window, including whether the size is specified or should be determined from the determined size of the WPF content.

  • The parent window.

  • The HwndSourceHook to include in the window procedure chain.  If you specify a hook at construction time, it will receive all messages for the window.  You can use AddHook to add a hook after the window has been created.

  • Transparency settings. A top-level windows can be configured to blend with the other windows on the desktop according to the per-pixel transparency of the WPF content.  To enable this, set the UsesPerPixelOpacity property in the HwndSourceParameters to true.  This can only be specified at construction time, and only through the HwndSource(HwndSourceParameters) constructor signature, and it imposes a number of limitations.

Once you have populated the HwndSourceParameters structure, pass it to the HwndSource(HwndSourceParameters) constructor for the HwndSource.

Object Lifetime

An HwndSource is a regular common language runtime (CLR) object, and its lifetime is managed by the garbage collector.  However, since the HwndSource represents an unmanaged resource, HwndSource implements IDisposable.  Calling Dispose will synchronously destroy the Win32 window immediately if called from the owner thread.  If called from another thread, the Win32 window will be destroyed asynchronously. Calling Dispose explicitly from the interoperating code might be necessary for certain interoperation scenarios.

Window Procedures

The HwndSource class implements its own window procedure.  This window procedure is used to process important window messages, such as those related to layout, rendering, and input.  However, you can also hook the window procedure for your own use.  You can specify your own hook during construction by setting the HwndSourceParameters.HwndSourceHook property, or you can also use AddHook and RemoveHook to add and remove hooks after the window has been created.  The hooks will be called by last-in,first-out order, which enables your hooks to execute before the built-in processing.  The actual hooks are held by a weak reference, so make sure you manage the lifetime of your hook delegate.

For more information about HwndSource and other interoperation classes, see WPF and Win32 Interoperation Overview.

Notice how the constructors for HwndSource take parameters that resemble the parameters for Win32 functions such as CreateWindowEx.

Scale transformations should not be applied to the RootVisual of an HwndSource. See Remarks for RootVisual.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.