# 自動縮放 Windows FormAutomatic scaling in Windows Forms

## 自動縮放的原始支援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.

• 因為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.

## 目前支援自動調整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. The 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.

### 自動調整規模動作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.