Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/layout-considerations-for-the-windowsformshost-element.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* 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
2020-09-04 09:46:28 -07:00

13 KiB

title, ms.date, helpviewer_keywords, ms.assetid
title ms.date helpviewer_keywords ms.assetid
Layout Considerations for the WindowsFormsHost Element 03/30/2017
Windows Forms [WPF], interoperability with
Windows Forms [WPF], WPF interoperation
interoperability [WPF], Windows Forms
WindowsFormsHost element layout considerations [WPF]
dynamic layout [WPF interoperability]
device-independent pixels
3c574597-bbde-440f-95cc-01371f1a5d9d

Layout Considerations for the WindowsFormsHost Element

This topic describes how the xref:System.Windows.Forms.Integration.WindowsFormsHost element interacts with the [!INCLUDETLA2#tla_winclient] layout system.

[!INCLUDETLA2#tla_winclient] and Windows Forms support different, but similar, logic for sizing and positioning elements on a form or page. When you create a hybrid user interface (UI) that hosts Windows Forms controls in [!INCLUDETLA2#tla_winclient], the xref:System.Windows.Forms.Integration.WindowsFormsHost element integrates the two layout schemes.

Differences in Layout Between WPF and Windows Forms

[!INCLUDETLA2#tla_winclient] uses resolution-independent layout. All [!INCLUDETLA2#tla_winclient] layout dimensions are specified using device-independent pixels. A device-independent pixel is one ninety-sixth of an inch in size and resolution-independent, so you get similar results regardless of whether you are rendering to a 72-dpi monitor or a 19,200-dpi printer.

[!INCLUDETLA2#tla_winclient] is also based on dynamic layout. This means that a UI element arranges itself on a form or page according to its content, its parent layout container, and the available screen size. Dynamic layout facilitates localization by automatically adjusting the size and position of UI elements when the strings they contain change length.

Layout in Windows Forms is device-dependent and more likely to be static. Typically, Windows Forms controls are positioned absolutely on a form using dimensions specified in hardware pixels. However, Windows Forms does support some dynamic layout features, as summarized in the following table.

Layout feature Description
Autosizing Some Windows Forms controls resize themselves to display their contents properly. For more information, see AutoSize Property Overview.
Anchoring and docking Windows Forms controls support positioning and sizing based on the parent container. For more information, see xref:System.Windows.Forms.Control.Anchor%2A?displayProperty=nameWithType and xref:System.Windows.Forms.Control.Dock%2A?displayProperty=nameWithType.
Autoscaling Container controls resize themselves and their children based on the resolution of the output device or the size, in pixels, of the default container font. For more information, see Automatic Scaling in Windows Forms.
Layout containers The xref:System.Windows.Forms.FlowLayoutPanel and xref:System.Windows.Forms.TableLayoutPanel controls arrange their child controls and size themselves according to their contents.

Layout Limitations

In general, Windows Forms controls cannot be scaled and transformed to the extent possible in [!INCLUDETLA2#tla_winclient]. The following list describes the known limitations when the xref:System.Windows.Forms.Integration.WindowsFormsHost element attempts to integrate its hosted Windows Forms control into the [!INCLUDETLA2#tla_winclient] layout system.

Z-order

In a [!INCLUDETLA2#tla_winclient] user interface, you can change the z-order of elements to control overlapping behavior. A hosted Windows Forms control is drawn in a separate HWND, so it is always drawn on top of [!INCLUDETLA2#tla_winclient] elements.

A hosted Windows Forms control is also drawn on top of any xref:System.Windows.Documents.Adorner elements.

Layout Behavior

The following sections describe specific aspects of layout behavior when hosting Windows Forms controls in [!INCLUDETLA2#tla_winclient].

Scaling, Unit Conversion, and Device Independence

Whenever the xref:System.Windows.Forms.Integration.WindowsFormsHost element performs operations involving [!INCLUDETLA2#tla_winclient] and Windows Forms dimensions, two coordinate systems are involved: device-independent pixels for [!INCLUDETLA2#tla_winclient] and hardware pixels for Windows Forms. Therefore, you must apply proper unit and scaling conversions to achieve a consistent layout.

Conversion between the coordinate systems depends on the current device resolution and any layout or rendering transforms applied to the xref:System.Windows.Forms.Integration.WindowsFormsHost element or to its ancestors.

If the output device is 96 dpi and no scaling has been applied to the xref:System.Windows.Forms.Integration.WindowsFormsHost element, one device-independent pixel is equal to one hardware pixel.

All other cases require coordinate system scaling. The hosted control is not resized. Instead, the xref:System.Windows.Forms.Integration.WindowsFormsHost element attempts to scale the hosted control and all of its child controls. Because Windows Forms does not fully support scaling, the xref:System.Windows.Forms.Integration.WindowsFormsHost element scales to the degree supported by particular controls.

Override the xref:System.Windows.Forms.Integration.WindowsFormsHost.ScaleChild%2A method to provide custom scaling behavior for the hosted Windows Forms control.

In addition to scaling, the xref:System.Windows.Forms.Integration.WindowsFormsHost element handles rounding and overflow cases as described in the following table.

Conversion issue Description
Rounding [!INCLUDETLA2#tla_winclient] device-independent pixel dimensions are specified as double, and Windows Forms hardware pixel dimensions are specified as int. In cases where double-based dimensions are converted to int-based dimensions, the xref:System.Windows.Forms.Integration.WindowsFormsHost element uses standard rounding, so that fractional values less than 0.5 are rounded down to 0.
Overflow When the xref:System.Windows.Forms.Integration.WindowsFormsHost element converts from double values to int values, overflow is possible. Values that are larger than xref:System.Int32.MaxValue are set to xref:System.Int32.MaxValue.

Properties that control layout behavior in Windows Forms controls and [!INCLUDETLA2#tla_winclient] elements are mapped appropriately by the xref:System.Windows.Forms.Integration.WindowsFormsHost element. For more information, see Windows Forms and WPF Property Mapping.

Layout Changes in the Hosted Control

Layout changes in the hosted Windows Forms control are propagated to [!INCLUDETLA2#tla_winclient] to trigger layout updates. The xref:System.Windows.UIElement.InvalidateMeasure%2A method on xref:System.Windows.Forms.Integration.WindowsFormsHost ensures that layout changes in the hosted control cause the [!INCLUDETLA2#tla_winclient] layout engine to run.

Continuously Sized Windows Forms Controls

Windows Forms controls that support continuous scaling fully interact with the [!INCLUDETLA2#tla_winclient] layout system. The xref:System.Windows.Forms.Integration.WindowsFormsHost element uses the xref:System.Windows.FrameworkElement.MeasureOverride%2A and xref:System.Windows.FrameworkElement.ArrangeOverride%2A methods as usual to size and arrange the hosted Windows Forms control.

Sizing Algorithm

The xref:System.Windows.Forms.Integration.WindowsFormsHost element uses the following procedure to size the hosted control:

  1. The xref:System.Windows.Forms.Integration.WindowsFormsHost element overrides the xref:System.Windows.FrameworkElement.MeasureOverride%2A and xref:System.Windows.FrameworkElement.ArrangeOverride%2A methods.

  2. To determine the size of the hosted control, the xref:System.Windows.FrameworkElement.MeasureOverride%2A method calls the hosted control's xref:System.Windows.Forms.Control.GetPreferredSize%2A method with a constraint translated from the constraint passed to the xref:System.Windows.FrameworkElement.MeasureOverride%2A method.

  3. The xref:System.Windows.FrameworkElement.ArrangeOverride%2A method attempts to set the hosted control to the given size constraint.

  4. If the hosted control's xref:System.Windows.Forms.Control.Size%2A property matches the specified constraint, the hosted control is sized to the constraint.

If the xref:System.Windows.Forms.Control.Size%2A property does not match the specified constraint, the hosted control does not support continuous sizing. For example, the xref:System.Windows.Forms.MonthCalendar control allows only discrete sizes. The permitted sizes for this control consist of integers (representing the number of months) for both height and width. In cases such as this, the xref:System.Windows.Forms.Integration.WindowsFormsHost element behaves as follows:

See also