* Reset branch for WPF changes * Convert BMP to PNG; fix link-out-of-scope err * Add snippets for WPF... 6794 files!!!! * Add missing snippets * update file updated between migration * Fix paths to include * update breadcrumb and toc * fix index links * fix index links * fix index links * fix markdown
7.6 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||
|---|---|---|---|---|---|---|---|---|---|
| Tutorial: Hosting Visual Objects in a Win32 Application | 03/30/2017 |
|
|
f0e1600c-3217-43d5-875d-1864fa7fe628 |
Tutorial: Hosting Visual Objects in a Win32 Application
[!INCLUDETLA#tla_winclient] provides a rich environment for creating applications. However, when you have a substantial investment in Win32 code, it might be more effective to add [!INCLUDETLA2#tla_winclient] functionality to your application rather than rewrite your code. To provide support for Win32 and [!INCLUDETLA2#tla_winclient] graphics subsystems used concurrently in an application, [!INCLUDETLA2#tla_winclient] provides a mechanism for hosting objects in a Win32 window.
This tutorial describes how to write a sample application, Hit Test with Win32 Interoperation Sample, that hosts [!INCLUDETLA2#tla_winclient] visual objects in a Win32 window.
Requirements
This tutorial assumes a basic familiarity with both [!INCLUDETLA2#tla_winclient] and Win32 programming. For a basic introduction to [!INCLUDETLA2#tla_winclient] programming, see Walkthrough: My first WPF desktop application. For an introduction to Win32 programming, see any of the numerous books on the subject, in particular Programming Windows by Charles Petzold.
Note
This tutorial includes a number of code examples from the associated sample. However, for readability, it does not include the complete sample code. For the complete sample code, see Hit Test with Win32 Interoperation Sample.
Creating the Host Win32 Window
The key to hosting [!INCLUDETLA2#tla_winclient] objects in a Win32 window is the xref:System.Windows.Interop.HwndSource class. This class wraps the [!INCLUDETLA2#tla_winclient] objects in a Win32 window, allowing them to be incorporated into your [!INCLUDETLA#tla_ui] as a child window.
The following example shows the code for creating the xref:System.Windows.Interop.HwndSource object as the Win32 container window for the visual objects. To set the window style, position, and other parameters for the Win32 window, use the xref:System.Windows.Interop.HwndSourceParameters object.
[!code-csharpVisualsHitTesting#101] [!code-vbVisualsHitTesting#101]
Note
The value of the xref:System.Windows.Interop.HwndSourceParameters.ExtendedWindowStyle%2A property cannot be set to WS_EX_TRANSPARENT. This means that the host Win32 window cannot be transparent. For this reason, the background color of the host Win32 window is set to the same background color as its parent window.
Adding Visual Objects to the Host Win32 Window
Once you have created a host Win32 container window for the visual objects, you can add visual objects to it. You will want to ensure that any transformations of the visual objects, such as animations, do not extend beyond the bounds of the host Win32 window's bounding rectangle.
The following example shows the code for creating the xref:System.Windows.Interop.HwndSource object and adding visual objects to it.
Note
The xref:System.Windows.Interop.HwndSource.RootVisual%2A property of the xref:System.Windows.Interop.HwndSource object is set to the first visual object added to the host Win32 window. The root visual object defines the top-most node of the visual object tree. Any subsequent visual objects added to the host Win32 window are added as child objects.
[!code-csharpVisualsHitTesting#100] [!code-vbVisualsHitTesting#100]
Implementing the Win32 Message Filter
The host Win32 window for the visual objects requires a window message filter procedure to handle messages that are sent to the window from the application queue. The window procedure receives messages from the Win32 system. These may be input messages or window-management messages. You can optionally handle a message in your window procedure or pass the message to the system for default processing.
The xref:System.Windows.Interop.HwndSource object that you defined as the parent for the visual objects must reference the window message filter procedure you provide. When you create the xref:System.Windows.Interop.HwndSource object, set the xref:System.Windows.Interop.HwndSourceParameters.HwndSourceHook%2A property to reference the window procedure.
[!code-csharpVisualsHitTesting#102] [!code-vbVisualsHitTesting#102]
The following example shows the code for handling the left and right mouse button up messages. The coordinate value of the mouse hit position is contained in the value of the lParam parameter.
[!code-csharpVisualsHitTesting#103] [!code-vbVisualsHitTesting#103]
Processing the Win32 Messages
The code in the following example shows how a hit test is performed against the hierarchy of visual objects contained in the host Win32 window. You can identify whether a point is within the geometry of a visual object, by using the xref:System.Windows.Media.VisualTreeHelper.HitTest%2A method to specify the root visual object and the coordinate value to hit test against. In this case, the root visual object is the value of the xref:System.Windows.Interop.HwndSource.RootVisual%2A property of the xref:System.Windows.Interop.HwndSource object.
[!code-csharpVisualsHitTesting#104] [!code-vbVisualsHitTesting#104]
For more information on hit testing against visual objects, see Hit Testing in the Visual Layer.