This topic has not yet been rated - Rate this topic

HwndSource Class

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

Namespace:  System.Windows.Interop
Assembly:  PresentationCore (in PresentationCore.dll)
public class HwndSource : PresentationSource, IDisposable, 
	IWin32Window, IKeyboardInputSink

The HwndSource type exposes the following members.

  Name Description
Public method HwndSource(HwndSourceParameters) Initializes a new instance of the HwndSource class by using a structure that contains the initial settings.
Public method HwndSource(Int32, Int32, Int32, Int32, Int32, String, IntPtr) Initializes a new instance of the HwndSource class with a specified class style, style, extended style, x-y position, name, and parent window.
Public method HwndSource(Int32, Int32, Int32, Int32, Int32, Int32, Int32, String, IntPtr) Initializes a new instance of the HwndSource class with a specified class style, style, extended style, x-y position, width, height, name, and parent window.
Public method HwndSource(Int32, Int32, Int32, Int32, Int32, Int32, Int32, String, IntPtr, Boolean) Initializes a new instance of the HwndSource class with a specified class style, style, extended style, x-y position, width, height, name, and parent window, and by specifying whether the window is autosized.
Top
  Name Description
Public property AcquireHwndFocusInMenuMode Gets the value that determines whether to acquire Win32 focus for the WPF containing window for this HwndSource.
Public property ChildKeyboardInputSinks Gets a sequence of registered input sinks.
Public property CompositionTarget Gets the visual manager for the hosted window.
Public property Static member DefaultAcquireHwndFocusInMenuMode Gets or sets the default AcquireHwndFocusInMenuMode value for new instances of HwndSource.
Public property Dispatcher Gets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Public property Handle Gets the window handle for this HwndSource.
Public property IsDisposed Gets a value that indicates whether Dispose has been called on this HwndSource. (Overrides PresentationSource.IsDisposed.)
Protected property KeyboardInputSiteCore Gets or sets a reference to the component's container's IKeyboardInputSite interface.
Public property RestoreFocusMode Gets the RestoreFocusMode for the window.
Public property RootVisual Gets or sets the RootVisual of the window. (Overrides PresentationSource.RootVisual.)
Public property SizeToContent Get or sets whether and how the window is sized to its content.
Public property UsesPerPixelOpacity Gets a value that declares whether the per-pixel opacity of the source window content is respected.
Top
  Name Description
Public method AddHook Adds an event handler that receives all window messages.
Protected method AddSource Adds a PresentationSource derived class instance to the list of known presentation sources. (Inherited from PresentationSource.)
Public method CheckAccess Determines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Protected method ClearContentRenderedListeners Sets the list of listeners for the ContentRendered event to null. (Inherited from PresentationSource.)
Public method CreateHandleRef Gets the window handle for the HwndSource. The window handle is packaged as part of a HandleRef structure.
Public method Dispose Releases all managed resources that are used by the HwndSource, and raises the Disposed event.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Static member FromHwnd Returns the HwndSource object of the specified window.
Protected method GetCompositionTargetCore Gets the visual target of the window. (Overrides PresentationSource.GetCompositionTargetCore().)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method HasFocusWithinCore Gets a value that indicates whether the sink or one of its contained components has focus.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method OnMnemonicCore Called when one of the mnemonics (access keys) for this sink is invoked.
Protected method RegisterKeyboardInputSinkCore Registers the IKeyboardInputSink interface of a contained component.
Public method RemoveHook Removes the event handlers that were added by AddHook.
Protected method RemoveSource Removes a PresentationSource derived class instance from the list of known presentation sources. (Inherited from PresentationSource.)
Protected method RootChanged Provides notification that the root Visual has changed. (Inherited from PresentationSource.)
Protected method TabIntoCore Sets focus on either the first tab stop or the last tab stop of the sink.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Protected method TranslateAcceleratorCore Processes keyboard input at the key-down message level.
Protected method TranslateCharCore Processes WM_CHAR, WM_SYSCHAR, WM_DEADCHAR, and WM_SYSDEADCHAR input messages before the OnMnemonic method is called.
Public method VerifyAccess Enforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Top
  Name Description
Public event AutoResized Occurs when layout causes the HwndSource to automatically resize.
Public event ContentRendered Occurs when content is rendered and ready for user interaction. (Inherited from PresentationSource.)
Public event Disposed Occurs when the Dispose method is called on this object.
Public event SizeToContentChanged Occurs when the value of the SizeToContent property changes.
Top
  Name Description
Explicit interface implemetation Private method IKeyboardInputSink.HasFocusWithin For a description of this member, see IList.Remove.
Explicit interface implemetation Private property IKeyboardInputSink.KeyboardInputSite For a description of this member, see KeyboardInputSite.
Explicit interface implemetation Private method IKeyboardInputSink.OnMnemonic For a description of this member, see OnMnemonic.
Explicit interface implemetation Private method IKeyboardInputSink.RegisterKeyboardInputSink For a description of this member, see RegisterKeyboardInputSink.
Explicit interface implemetation Private method IKeyboardInputSink.TabInto For a description of this member, see TabInto.
Explicit interface implemetation Private method IKeyboardInputSink.TranslateAccelerator For a description of this member, see TranslateAccelerator.
Explicit interface implemetation Private method IKeyboardInputSink.TranslateChar For a description of this member, see TranslateChar.
Top
Security note Security Note

Many members of this class are unavailable 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, and rendered; and is interactive to input. Because the HwndSource is specifically designed to interoperate with Win32, this class exposes several low-level Win32 features. You can use this class to do the following:

  • Specify window styles, window class styles, and extended window styles.

  • Hook the window procedure.

  • Provide access to the window handle (HWND).

  • Destroy 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) through the Handle property, which can be passed by means of 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 the following:

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

  • The initial position of the window.

  • The initial size of the window, which includes 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 receives all messages for the window. You can use AddHook to add a hook after the window is created.

  • The transparency settings. A top-level window 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 property can only be specified at construction time, and only through the HwndSource(HwndSourceParameters) constructor signature, and it imposes several limitations.

After you populate 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. Because the HwndSource represents an unmanaged resource, HwndSource implements IDisposable. Synchronously calling Dispose immediately destroys the Win32 window if called from the owner thread. If called from another thread, the Win32 window is 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 is created. The hooks are 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. Therefore, make sure that you manage the lifetime of your hook delegate.

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

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.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Using the HWND returned by HwndSource
I know that the HwndSource.handle.ToPointer() provides a win32 handle that I can store. Can I use this handle to re-establish contact with the WPF control?

For example, if I have a control to show in a dialog, I can add it as the RootVisual in a create type function. Then later on,. when the user presses a win32 button, I want to let the control know. By this time all I have is the HWND that I extracted from the HwndSource in the create function. Is there any way I can use this to get back intouch with the RootVisual. I dont seem to be able to store any references to anything else as there is a conflict with .NET during compile.

Yes, you can always reconstruct an HwndSource directly from an HWND to get access to everything associated with that HWND.  Remember that there's no magic in an HwndSource.  :)  It's just providing a different interface for all the Win32 goodness that's already there.  Direct quote from the WPF Unleashed book: "You don't need to share an HwndSource globally as long as you have its corresponding HWND.  HwndSource defines a static FromHwnd method, which returns an HwndSource instance corresponding to any HWND (assuming that the HWND belongs to an HwndSource in the first place). ..."

Please note that a community site such as StackOverflow or the MSDN forums is a better place to ask questions like this and get a timely response.
  • 3/31/2011
  • cws