自動縮放 Windows FormAutomatic scaling in Windows Forms

自動縮放功能允許某部電腦上以特定顯示解析度或系統字型所設計的表單和其控制項,可以在另一部電腦上以不同的顯示解析度或系統字型正確地顯示。Automatic scaling enables a form and its controls, designed on one machine with a certain display resolution or system font, to be displayed appropriately on another machine with a different display resolution or system font. 如此可確保表單和其控制項利用智慧方式調整大小,以便與使用者和其他開發人員電腦上的原生視窗和其他應用程式保持一致。It assures that the form and its controls will intelligently resize to be consistent with native windows and other applications on both the users' and other developers' machines. 對自動縮放和視覺化樣式的.NET framework 的支援可讓.NET Framework 應用程式,以維護一致的外觀與風格相較於每個使用者的電腦上的原生 Windows 應用程式。The support of the .NET Framework for automatic scaling and visual styles enables .NET Framework applications to maintain a consistent look and feel when compared to native Windows applications on each user's machine.

大部分的情況下,作為自動調整的運作中應該有.NET Framework 版本 2.0 和更新版本。For the most part, automatic scaling works as expected in .NET Framework version 2.0 and later. 不過,字型配置變更可能會造成問題。However, font scheme changes can be problematic. 如需如何解決此問題的範例,請參閱How to:回應 Windows Forms 應用程式中的字型配置變更For an example of how to resolve this, see How to: Respond to Font Scheme Changes in a Windows Forms Application.

自動縮放的需求Need for automatic scaling

如果沒有自動縮放功能,專為某種顯示解析度或字型所設計的應用程式會在該解析度或字型變更時,看起來太小或太大。Without automatic scaling, an application designed for one display resolution or font will either appear too small or too large when that resolution or font is changed. 例如,如果應用程式在設計時使用新細明體 9 點做為基準,然後未經調整便在系統字型為新細明體 12 點的電腦上執行,則會看起來太小。For example, if the application is designed using Tahoma 9 point as a baseline, without adjustment it will appear too small if run on a machine where the system font is Tahoma 12 point. 文字項目 (例如標題、功能表、文字方塊內容等) 的顯示將會比其他應用程式還要小。Text elements, such as titles, menus, text box contents, and so on will render smaller than other applications. 此外,含有文字之使用者介面 (UI) 項目 (例如標題列、功能表和許多控制項) 的大小會取決於所使用的字型。Furthermore, the size of user interface (UI) elements that contain text, such as the title bar, menus, and many controls are dependent on the font used. 在這個範例中,這些項目看起來也會相對較小。In this example, these elements will also appear relatively smaller.

當應用程式是專為特定顯示解析度而設計時,就會發生類似的情況。An analogous situation occurs when an application is designed for a certain display resolution. 最常見的顯示解析度為 96 為 dots per inch (DPI),而這就等於 100%顯示縮放比例,但支援 125%、 150%、 200%的更高解析度顯示畫面 (哪些分別等於 120,第 144 和 192 DPI) 和更新版本會變得更為普遍。The most common display resolution is 96 dots per inch (DPI), which equals 100% display scaling, but higher resolution displays supporting 125%, 150%, 200% (which respectively equal 120, 144 and 192 DPI) and above are becoming more common. 如果未經調整,專為某種解析度所設計的應用程式 (特別是圖形應用程式) 以另一種解析度執行時,便會看起來太大或太小。Without adjustment, an application, especially a graphics-based one, designed for one resolution will appear either too large or too small when run at another resolution.

自動縮放功能會根據相對字型大小或顯示解析度,自動調整表單和其控制項的大小,以改善這些問題。Automatic scaling seeks to ameliorate these problems by automatically resizing the form and its child controls according to the relative font size or display resolution. Windows 作業系統支援使用稱為對話方塊單位的相對測量單位,來自動縮放對話方塊。The Windows operating system supports automatic scaling of dialog boxes using a relative unit of measurement called dialog units. 對話方塊單位是以系統字型以及其與像素的關聯性為依據,並可透過 Win32 SDK 函式 GetDialogBaseUnits 來判斷。A dialog unit is based on the system font and its relationship to pixels can be determined though the Win32 SDK function GetDialogBaseUnits. 當使用者變更 Windows 所使用的佈景主題時,所有對話方塊都會隨著自動調整。When a user changes the theme used by Windows, all dialog boxes are automatically adjusted accordingly. 此外,.NET Framework 支援自動調整,根據預設系統字型或顯示解析度。In addition, the .NET Framework supports automatic scaling either according to the default system font or the display resolution. 您可以選擇性地停用應用程式中的自動縮放功能。Optionally, automatic scaling can be disabled in an application.

自動縮放的原始支援Original support for automatic scaling

1.0 和 1.1 版的.NET Framework 支援自動調整的方式直接根據 UI 中,以 Win32 SDK 值表示所使用的 Windows 預設字型DEFAULT_GUI_FONTVersions 1.0 and 1.1 of the .NET Framework supported automatic scaling in a straightforward manner that was dependent on the Windows default font used for the UI, represented by the Win32 SDK value DEFAULT_GUI_FONT. 這種字型通常只有在顯示解析度變更時才會變更。This font is typically only changed when the display resolution changes. 之前會使用下列機制來實作自動縮放:The following mechanism was used to implement automatic scaling:

  1. 在設計階段,AutoScaleBaseSize 屬性 (現在已被取代) 會設定為開發人員電腦上預設系統字型的高度和寬度。At design time, the AutoScaleBaseSize property (which is now deprecated) was set to the height and width of the default system font on the developer's machine.

  2. 在執行階段,使用者電腦的預設系統字型會用來初始化 Form 類別的 Font 屬性。At runtime, the default system font of the user's machine was used to initialize the Font property of the Form class.

  3. 在顯示表單之前,會呼叫 ApplyAutoScaling 方法來縮放表單。Before displaying the form, the ApplyAutoScaling method was called to scale the form. 這個方法會從 AutoScaleBaseSizeFont 計算相對縮放比例,然後呼叫 Scale 方法實際縮放表單和其子系。This method calculated the relative scale sizes from AutoScaleBaseSize and Font then called the Scale method to actually scale the form and its children.

  4. AutoScaleBaseSize 的值已更新,後續對 ApplyAutoScaling 的呼叫不會繼續調整表單的大小。The value of AutoScaleBaseSize was updated so that subsequent calls to ApplyAutoScaling did not progressively resize the form.

雖然這項機制在大部分情況下便已足夠,但是仍有下列限制:While this mechanism was sufficient for most purposes, it suffered from the following limitations:

  • 因為AutoScaleBaseSize屬性以整數值表示基準字型大小,會發生捨入錯誤,更為明顯,表單特別是當透過多個解決方法。Since the AutoScaleBaseSize property represents the baseline font size as integer values, rounding errors occur that become evident when a form is cycled through multiple resolutions.

  • 自動縮放只會在 Form 類別中實作,並不會在 ContainerControl 類別中實作。Automatic scaling was implemented in only the Form class, not in the ContainerControl class. 因此,只有在使用者控制項是以與表單相同的解析度所設計,並且在設計階段置於表單中時,使用者控制項才能正確縮放。As a result, user controls would scale correctly only when the user control was designed at the same resolution as the form, and it was placed in the form at design time.

  • 多位開發人員只能在其電腦解析度都相同時,才能同時設計表單和其子控制項。Forms and their child controls could only be concurrently designed by multiple developers if their machine resolutions were the same. 同樣地,它也會根據與父表單關聯的解析度來繼承表單。Likewise it also made inheritance of a form dependent on the resolution associated with the parent form.

  • 它與不相容所導入的.NET Framework 2.0 版中,這類的新版配置管理員FlowLayoutPanelTableLayoutPanelIt is not compatible with the newer layout managers introduced with the .NET Framework version 2.0, such as FlowLayoutPanel and TableLayoutPanel.

  • 它不支援直接根據顯示解析度所需的.NET Compact framework 的相容性進行調整。It did not support scaling based directly on the display resolution that is required for compatibility to the .NET Compact Framework.

雖然這項機制會保留在.NET Framework 2.0 版為了與舊版相容,但它已被取代的更強大的縮機制,接下來所述。Although this mechanism is preserved in the .NET Framework version 2.0 to maintain backward compatibility, it has been superseded by the more robust scaling mechanism described next. 因此,AutoScaleApplyAutoScalingAutoScaleBaseSize 和特定 Scale 多載會標記為已過時。As a consequence, the AutoScale, ApplyAutoScaling, AutoScaleBaseSize, and certain Scale overloads are marked as obsolete.

注意

當您升級至.NET Framework 2.0 版的舊版程式碼,您可以安全地刪除這些成員的參考。You can safely delete references to these members when you upgrade your legacy code to the .NET Framework version 2.0.

目前支援自動調整Current support for automatic scaling

.NET Framework 2.0 版引進下列變更 Windows Form 自動縮放 surmounts 先前的限制:The .NET Framework version 2.0 surmounts previous limitations by introducing the following changes to the automatic scaling of Windows Forms:

  • 縮放的基礎支援已移至 ContainerControl 類別,因此表單、原生複合控制項和使用者控制項都能獲得一致的縮放支援。Base support for scaling has been moved to the ContainerControl class so that forms, native composite controls and user controls all receive uniform scaling support. 同時也加入新成員 AutoScaleFactorAutoScaleDimensionsAutoScaleModePerformAutoScaleThe new members AutoScaleFactor, AutoScaleDimensions, AutoScaleMode and PerformAutoScale have been added.

  • Control 類別也具有幾個新成員,以便可以參與縮放並支援在相同表單上使用混合縮放比例。The Control class also has several new members that allow it to participate in scaling and to support mixed scaling on the same form. ScaleScaleChildrenGetScaledBounds 成員尤其支援縮放比例。Specifically the Scale, ScaleChildren, and GetScaledBounds members support scaling.

  • 補充的系統字型支援已新增根據螢幕解析度進行縮放的支援,如 AutoScaleMode 列舉所定義。Support for scaling based upon the screen resolution has been added to complement system font support, as defined by the AutoScaleMode enumeration. 此模式適用於自動調整支援的.NET Compact Framework 啟用應用程式移轉作業更為輕鬆。This mode is compatible with automatic scaling supported by the .NET Compact Framework enabling easier application migration.

  • 自動縮放實作已新增與配置管理員 (例如 FlowLayoutPanelTableLayoutPanel) 的相容性。Compatibility with layout managers such as FlowLayoutPanel and TableLayoutPanel has been added to the implementation of automatic scaling.

  • 縮放比例現在以浮點值表示 (通常使用 SizeF 結構),因此已實際減少捨入錯誤。Scaling factors are now represented as floating point values, typically using the SizeF structure, so that rounding errors have been practically eliminated.

警告

不支援 DPI 和字型縮放模式的任意混合。Arbitrary mixtures of DPI and font scaling modes are not supported. 雖然您可以使用某種模式 (例如 DPI) 縮放使用者控制項,並使用另一種模式 (字型) 將控制項置於表單上,而不會有任何問題,但是將某種模式中的基底表單與從另一種模式的衍生表單混合,可能會導致無法預期的結果。Although you may scale a user control using one mode (for example, DPI) and place it on a form using another mode (Font) with no issues, but mixing a base form in one mode and a derived form in another can lead to unexpected results.

自動調整規模動作Automatic scaling in action

Windows Form 現在使用下列邏輯來自動縮放表單和其內容:Windows Forms now uses the following logic to automatically scale forms and their contents:

  1. 在設計階段,每個 ContainerControl 會記錄縮放模式,以及各自在 AutoScaleModeAutoScaleDimensions 中的目前解析度。At design time, each ContainerControl records the scaling mode and it current resolution in the AutoScaleMode and AutoScaleDimensions, respectively.

  2. 在執行階段,實際的解析度會儲存在 CurrentAutoScaleDimensions 屬性中。At run time, the actual resolution is stored in the CurrentAutoScaleDimensions property. AutoScaleFactor 屬性動態計算執行階段縮放解析和設計階段縮放解析間的比率。The AutoScaleFactor property dynamically calculates the ratio between the run-time and design-time scaling resolution.

  3. 當表單載入時,如果 CurrentAutoScaleDimensionsAutoScaleDimensions 的值不同,則會呼叫 PerformAutoScale 方法以縮放控制項和其子系。When the form loads, if the values of CurrentAutoScaleDimensions and AutoScaleDimensions are different, then the PerformAutoScale method is called to scale the control and its children. 這個方法會暫止配置並呼叫 Scale 方法,以執行實際的縮放。This method suspends layout and calls the Scale method to perform the actual scaling. 之後,AutoScaleDimensions 的值會更新以避免繼續縮放。Afterwards, the value of AutoScaleDimensions is updated to avoid progressive scaling.

  4. 在下列情況下,也會自動叫用 PerformAutoScalePerformAutoScale is also automatically invoked in the following situations:

    • 為了在縮放模式為 Font 時回應 OnFontChanged 事件。In response to the OnFontChanged event if the scaling mode is Font.

    • 當繼續容器控制項的配置,並在 AutoScaleDimensionsAutoScaleMode 屬性中偵測到變更時。When the layout of the container control resumes and a change is detected in the AutoScaleDimensions or AutoScaleMode properties.

    • 如上所指,當縮放父 ContainerControl 時。As implied above, when a parent ContainerControl is being scaled. 每個容器控制項會負責使用自已的縮放比例 (而非其父容器的縮放比例),來縮放其子系。Each container control is responsible for scaling its children using its own scaling factors and not the one from its parent container.

  5. 子控制項可以透過幾種方式,修改其縮放行為:Child controls can modify their scaling behavior through several means:

    • 可以覆寫 ScaleChildren 屬性,以判斷是否應該縮放其子控制項。The ScaleChildren property can be overridden to determine if their child controls should be scaled or not.

    • 可以覆寫 GetScaledBounds 方法,以調整控制項的縮放範圍,而不是縮放邏輯。The GetScaledBounds method can be overridden to adjust the bounds that the control is scaled to, but not the scaling logic.

    • 可以覆寫 ScaleControl 方法,以變更目前控制項的縮放邏輯。The ScaleControl method can be overridden to change the scaling logic for the current control.

另請參閱See also