WindowsFormsHost 項目的配置考量Layout Considerations for the WindowsFormsHost Element

本主題描述如何WindowsFormsHost項目互動WPFWPF版面配置系統。This topic describes how the WindowsFormsHost element interacts with the WPFWPF layout system.

WPFWPF Windows FormsWindows Forms支援不同但相似,邏輯大小及定位在表單或頁面上的項目。and Windows FormsWindows Forms support different, but similar, logic for sizing and positioning elements on a form or page. 當您建立混合式使用者介面 (UI) 裝載Windows FormsWindows Forms中的控制項WPFWPF,則WindowsFormsHost項目整合的兩個版面配置。When you create a hybrid user interface (UI) that hosts Windows FormsWindows Forms controls in WPFWPF, the WindowsFormsHost element integrates the two layout schemes.

在配置中 WPF 與 Windows Form 之間的差異Differences in Layout Between WPF and Windows Forms

WPFWPF 使用版面配置的解析度無關。uses resolution-independent layout. 所有WPFWPF使用指定的配置維度裝置無關的像素All WPFWPF layout dimensions are specified using device-independent pixels. 與裝置無關的像素是一個有 90-六分之一英吋為單位的大小與解析度無關,因此取得類似的結果,不論您要轉譯 72 dpi 監視器或 19200 dpi 印表機。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.

WPFWPF 也依據動態配置is also based on dynamic layout. 這表示 UI 項目,在表單或根據其內容、 其父系版面配置容器中和可用的螢幕大小的頁面上排列本身。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. 動態配置其所包含的字串長度變更時,自動調整的大小和位置的 UI 項目,從而完成當地語系化。Dynamic layout facilitates localization by automatically adjusting the size and position of UI elements when the strings they contain change length.

在 版面配置Windows FormsWindows Forms是裝置而異,更有可能是靜態。Layout in Windows FormsWindows Forms is device-dependent and more likely to be static. 一般而言,Windows FormsWindows Forms控制項會使用硬體像素中指定的維度,將表單上絕對定位。Typically, Windows FormsWindows Forms controls are positioned absolutely on a form using dimensions specified in hardware pixels. 不過,Windows FormsWindows Forms並支援某些動態配置功能下, 表摘要說明。However, Windows FormsWindows Forms does support some dynamic layout features, as summarized in the following table.

版面配置功能Layout feature 描述Description
自動調整大小Autosizing 某些Windows FormsWindows Forms控制項調整大小本身無法正確顯示其內容。Some Windows FormsWindows Forms controls resize themselves to display their contents properly. 如需詳細資訊,請參閱 < AutoSize 屬性概觀For more information, see AutoSize Property Overview.
錨定和停駐Anchoring and docking Windows FormsWindows Forms 控制項支援定位和調整大小為基礎的父容器。controls support positioning and sizing based on the parent container. 如需詳細資訊,請參閱 Control.AnchorControl.DockFor more information, see Control.Anchor and Control.Dock.
自動調整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. 如需詳細資訊,請參閱 < 在 Windows Form 中的自動調整For more information, see Automatic Scaling in Windows Forms.
版面配置容器Layout containers FlowLayoutPanelTableLayoutPanel控制項排列其子控制項,並根據其內容調整本身的大小。The FlowLayoutPanel and TableLayoutPanel controls arrange their child controls and size themselves according to their contents.

版面配置限制Layout Limitations

一般情況下,Windows FormsWindows Forms控制項無法調整,而且範圍在轉換WPFWPFIn general, Windows FormsWindows Forms controls cannot be scaled and transformed to the extent possible in WPFWPF. 下列清單描述已知的限制時WindowsFormsHost項目會嘗試將其裝載的整合Windows FormsWindows Forms控制項放入WPFWPF版面配置系統。The following list describes the known limitations when the WindowsFormsHost element attempts to integrate its hosted Windows FormsWindows Forms control into the WPFWPF layout system.

  • 在某些情況下,無法調整 Windows FormsWindows Forms 控制項大小,或只能將它調整為特定維度。In some cases, Windows FormsWindows Forms controls cannot be resized, or can be sized only to specific dimensions. 例如, Windows FormsWindows Forms ComboBox控制項支援只有單一高度由控制項字型大小所定義。For example, a Windows FormsWindows Forms ComboBox control supports only a single height, which is defined by the control's font size. WPFWPF其中項目可以垂直延伸,裝載的動態配置ComboBox控制項將不會如預期延伸。In a WPFWPF dynamic layout where elements can stretch vertically, a hosted ComboBox control will not stretch as expected.

  • 無法旋轉或扭曲 Windows FormsWindows Forms 控制項。Windows FormsWindows Forms controls cannot be rotated or skewed. WindowsFormsHost項目引發LayoutError事件,如果您套用扭曲或旋轉轉換。The WindowsFormsHost element raises the LayoutError event if you apply a skew or rotation transformation. 如果您不會處理LayoutError事件, InvalidOperationException ,就會引發。If you do not handle the LayoutError event, an InvalidOperationException is raised.

  • 在大部分情況下,Windows FormsWindows Forms 控制項不支援等比例縮放。In most cases, Windows FormsWindows Forms controls do not support proportional scaling. 雖然會縮放控制項的整體維度,但是可能無法如預期調整控制項的子控制項和元件大小。Although the overall dimensions of the control will scale, child controls and component elements of the control may not resize as expected. 這項限制取決於每個 Windows FormsWindows Forms 控制項支援縮放比例的程度。This limitation depends on how well each Windows FormsWindows Forms control supports scaling. 此外,您無法調整Windows FormsWindows Forms控制項到 0 像素的大小。In addition, you cannot scale Windows FormsWindows Forms controls down to a size of 0 pixels.

  • Windows FormsWindows Forms 控制項支援自動調整,以表單將自動調整大小本身和它的字型大小為基礎的控制項。controls support autoscaling, in which the form will automatically resize itself and its controls based on the font size. WPFWPF 使用者介面中,變更字型大小並不會調整整個版面配置大小,雖然可能會動態調整個別項目。In a WPFWPF user interface, changing the font size does not resize the entire layout, although individual elements may dynamically resize.

疊置順序Z-order

WPFWPF 使用者介面中,您可以變更項目的 Z 順序以控制重疊行為。In a WPFWPF user interface, you can change the z-order of elements to control overlapping behavior. 裝載的 Windows FormsWindows Forms 控制項會以不同的 HWND 進行繪製,因此一律會將它繪製在 WPFWPF 項目頂端。A hosted Windows FormsWindows Forms control is drawn in a separate HWND, so it is always drawn on top of WPFWPF elements.

託管Windows FormsWindows Forms控制項也會繪製上方任何Adorner項目。A hosted Windows FormsWindows Forms control is also drawn on top of any Adorner elements.

版面配置行為Layout Behavior

下列各節描述的版面配置行為的特定層面,裝載時Windows FormsWindows Forms中的控制項WPFWPFThe following sections describe specific aspects of layout behavior when hosting Windows FormsWindows Forms controls in WPFWPF.

調整大小、 單位轉換和裝置獨立性Scaling, Unit Conversion, and Device Independence

每當WindowsFormsHost項目執行的作業涉及WPFWPFWindows FormsWindows Forms牽涉到兩個座標系統的維度: 裝置獨立像素WPFWPF和硬體像素Windows FormsWindows FormsWhenever the WindowsFormsHost element performs operations involving WPFWPF and Windows FormsWindows Forms dimensions, two coordinate systems are involved: device-independent pixels for WPFWPF and hardware pixels for Windows FormsWindows Forms. 因此,您必須套用適當的單位和縮放轉換,以達到一致的版面配置。Therefore, you must apply proper unit and scaling conversions to achieve a consistent layout.

座標系統之間的轉換取決於目前的裝置解析度和任何配置或轉譯轉換套用至WindowsFormsHost項目或其祖系。Conversion between the coordinate systems depends on the current device resolution and any layout or rendering transforms applied to the WindowsFormsHost element or to its ancestors.

如果在輸出裝置是 96 dpi,且無縮放已套用至WindowsFormsHost項目,一個以與裝置無關的像素等於一個硬體像素。If the output device is 96 dpi and no scaling has been applied to the 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. 相反地,WindowsFormsHost項目會嘗試調整裝載的控制項及其所有子控制項。Instead, the WindowsFormsHost element attempts to scale the hosted control and all of its child controls. 因為Windows FormsWindows Forms不完全支援縮放比例、WindowsFormsHost項目調整為特定的控制項支援的程度。Because Windows FormsWindows Forms does not fully support scaling, the WindowsFormsHost element scales to the degree supported by particular controls.

覆寫ScaleChild方法,以提供裝載 Windows Forms 控制項的自訂的調整行為。Override the ScaleChild method to provide custom scaling behavior for the hosted Windows Forms control.

除了調整WindowsFormsHost項目處理捨入和溢位的情況下下, 表中所述。In addition to scaling, the WindowsFormsHost element handles rounding and overflow cases as described in the following table.

轉換問題Conversion issue 描述Description
四捨五入Rounding WPFWPF 指定為與裝置無關的像素尺寸double,並Windows FormsWindows Forms硬體像素尺寸會指定為intdevice-independent pixel dimensions are specified as double, and Windows FormsWindows Forms hardware pixel dimensions are specified as int. 萬一其中double-根據的維度會轉換成int-基礎維度WindowsFormsHost項目使用標準的圓弧半徑,以便小於 0.5 的小數的值會無條件捨去為 0。In cases where double-based dimensions are converted to int-based dimensions, the WindowsFormsHost element uses standard rounding, so that fractional values less than 0.5 are rounded down to 0.
溢位Overflow WindowsFormsHost項目將從轉換double值來int值,可能會溢位。When the WindowsFormsHost element converts from double values to int values, overflow is possible. 值大於MaxValue設定為MaxValueValues that are larger than MaxValue are set to MaxValue.

控制項中的版面配置行為的屬性Windows FormsWindows Forms控制項和WPFWPF項目會適當地藉由對應WindowsFormsHost項目。Properties that control layout behavior in Windows FormsWindows Forms controls and WPFWPF elements are mapped appropriately by the WindowsFormsHost element. 如需詳細資訊,請參閱 Windows Form 和 WPF 屬性對應For more information, see Windows Forms and WPF Property Mapping.

裝載控制項中的配置變更Layout Changes in the Hosted Control

中所裝載的版面配置變更Windows FormsWindows Forms控制項都會傳播到WPFWPF觸發版面配置更新。Layout changes in the hosted Windows FormsWindows Forms control are propagated to WPFWPF to trigger layout updates. InvalidateMeasure方法WindowsFormsHost可確保裝載控制項中的配置變更會造成WPFWPF版面配置引擎來執行。The InvalidateMeasure method on WindowsFormsHost ensures that layout changes in the hosted control cause the WPFWPF layout engine to run.

持續調整 Windows Form 控制項Continuously Sized Windows Forms Controls

Windows FormsWindows Forms 支援完整連續的縮放比例的控制項互動WPFWPF版面配置系統。controls that support continuous scaling fully interact with the WPFWPF layout system. WindowsFormsHost項目會使用MeasureOverrideArrangeOverride方法,如往常般以調整大小和排列裝載Windows FormsWindows Forms控制項。The WindowsFormsHost element uses the MeasureOverride and ArrangeOverride methods as usual to size and arrange the hosted Windows FormsWindows Forms control.

調整大小演算法Sizing Algorithm

WindowsFormsHost項目使用下列程序來裝載的控制項的大小:The WindowsFormsHost element uses the following procedure to size the hosted control:

  1. WindowsFormsHost元素會覆寫MeasureOverrideArrangeOverride方法。The WindowsFormsHost element overrides the MeasureOverride and ArrangeOverride methods.

  2. 若要判斷所裝載的控制項的大小MeasureOverride方法會呼叫裝載的控制項GetPreferredSize含有條件約束的方法會轉譯從傳遞給條件約束MeasureOverride方法。To determine the size of the hosted control, the MeasureOverride method calls the hosted control's GetPreferredSize method with a constraint translated from the constraint passed to the MeasureOverride method.

  3. ArrangeOverride方法會嘗試將指定的大小條件約束來設定裝載的控制項。The ArrangeOverride method attempts to set the hosted control to the given size constraint.

  4. 如果裝載的控制項Size屬性符合指定的條件約束,裝載的控制項大小為條件約束。If the hosted control's Size property matches the specified constraint, the hosted control is sized to the constraint.

如果Size屬性不符合指定的條件約束,裝載的控制項不支援連續的調整大小。If the Size property does not match the specified constraint, the hosted control does not support continuous sizing. 比方說,MonthCalendar控制項可讓只有離散的大小。For example, the 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. 在此情況下WindowsFormsHost項目行為,如下所示:In cases such as this, the WindowsFormsHost element behaves as follows:

  • 如果Size屬性會傳回與指定的條件約束,較大的大小WindowsFormsHost元素裁剪裝載的控制項。If the Size property returns a larger size than the specified constraint, the WindowsFormsHost element clips the hosted control. 高度和寬度會分開處理,因此可能會裁剪裝載的控制項,在任一方向。Height and width are handled separately, so the hosted control may be clipped in either direction.

  • 如果Size屬性會傳回與指定的條件約束,較小的大小WindowsFormsHost接受這個大小值,並傳回值,以WPFWPF版面配置系統。If the Size property returns a smaller size than the specified constraint, WindowsFormsHost accepts this size value and returns the value to the WPFWPF layout system.

另請參閱See also