Windows Forms 中的高 DPI 支援
從 .NET Framework 4.7 開始,Windows Forms 包含常見高 DPI 和動態 DPI 案例的增強功能。 其中包含:
改善許多 Windows Forms 控制項(例如 MonthCalendar 控制項和 CheckedListBox 控制項)的縮放比例和版面配置。
單次調整規模。 在 .NET Framework 4.6 及更早版本中,調整是透過多個行程執行,這會導致某些控制項的縮放比例超過所需。
支援動態 DPI 案例,在此案例中,使用者會在 Windows Forms 應用程式啟動之後變更 DPI 或縮放比例。
從 .NET Framework 4.7 開始的 .NET Framework 版本中,增強的高 DPI 支援是可加入宣告的功能。 您必須設定應用程式以利用它。
為您的 Windows Forms 應用程式設定高 DPI 支援
支援高 DPI 感知的新 Windows Forms 功能只適用于以 .NET Framework 4.7 為目標的應用程式,並在從 Windows 10 Creators Update 開始的 Windows 作業系統上執行。
此外,若要在 Windows Forms 應用程式中設定高 DPI 支援,您必須執行下列動作:
宣告與 Windows 10 的相容性。
若要這樣做,請將下列內容新增至您的資訊清單檔案:
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> <application> <!-- Windows 10 compatibility --> <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" /> </application> </compatibility>啟用 app.config 檔案中的個別監視器 DPI 感知。
Windows Forms 引進新
<System.Windows.Forms.ApplicationConfigurationSection>的元素,以支援從 .NET Framework 4.7 開始新增的新功能和自訂。 若要利用支援高 DPI 的新功能,請將下列程式檔新增至您的應用程式佈建檔。<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> <!-- ... other xml settings ... --> <System.Windows.Forms.ApplicationConfigurationSection> <add key="DpiAwareness" value="PerMonitorV2" /> </System.Windows.Forms.ApplicationConfigurationSection> </compatibility>重要
在先前的 .NET Framework 版本中,您已使用資訊清單來新增高 DPI 支援。 不再建議使用這個方法,因為它會覆寫 app.config 檔案上定義的設定。
呼叫靜態 EnableVisualStyles 方法。
這應該是應用程式進入點中的第一個方法呼叫。 例如:
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form2()); }
退出個別的高 DPI 功能
DpiAwareness將值設定為 PerMonitorV2 ,可啟用 .NET Framework 版本從 .NET Framework 4.7 開始支援的所有高 DPI 感知功能。 這通常適用于大部分的 Windows Forms 應用程式。 不過,您可能會想要退出一或多個個別功能。 這麼做的最重要原因是您現有的應用程式程式碼已經處理該功能。 例如,如果您的應用程式會處理自動調整,您可能會想要停用自動調整大小功能,如下所示:
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<!-- ... other xml settings ... -->
<System.Windows.Forms.ApplicationConfigurationSection>
<add key="DpiAwareness" value="PerMonitorV2" />
<add key="EnableWindowsFormsHighDpiAutoResizing" value="false" />
</System.Windows.Forms.ApplicationConfigurationSection>
</compatibility>
如需個別索引鍵和其值的清單,請參閱Windows Forms 新增設定元素。
新的 DPI 變更事件
從 .NET Framework 4.7 開始,三個新的事件可讓您以程式設計方式處理動態的 DPI 變更:
- DpiChangedAfterParent,當控制項的 DPI 設定在它的父控制項或表單發生 DPI 變更事件之後,以程式設計方式變更時,就會引發此錯誤。
- DpiChangedBeforeParent,當控制項的 DPI 設定在其父控制項或表單發生 DPI 變更事件之前,以程式設計方式變更時,就會引發此錯誤。
- DpiChanged,這會在目前顯示表單的顯示裝置上的 DPI 設定變更時引發。
新的 helper 方法和屬性
.NET Framework 4.7 也會新增一些新的 helper 方法和屬性,以提供 DPI 調整的相關資訊,並可讓您執行 DPI 縮放比例。 其中包含:
LogicalToDeviceUnits,將值從邏輯轉換成裝置圖元。
ScaleBitmapLogicalToDevice,會將點陣圖影像調整為裝置的邏輯 DPI。
DeviceDpi,傳回目前裝置的 DPI。
版本控制考慮
除了在 .NET Framework 4.7 和 Windows 10 Creators Update 上執行之外,您的應用程式也可能會在與高 DPI 增強功能不相容的環境中執行。 在此情況下,您必須為您的應用程式開發一個備用。 您可以執行此動作來執行 自訂繪圖 以處理調整。
若要這樣做,您也需要判斷應用程式執行所在的作業系統。 您可以使用如下的程式碼:
// Create a reference to the OS version of Windows 10 Creators Update.
Version OsMinVersion = new Version(10, 0, 15063, 0);
// Access the platform/version of the current OS.
Console.WriteLine(Environment.OSVersion.Platform.ToString());
Console.WriteLine(Environment.OSVersion.VersionString);
// Compare the current version to the minimum required version.
Console.WriteLine(Environment.OSVersion.Version.CompareTo(OsMinVersion));
請注意,如果應用程式未在應用程式資訊清單中列為支援的作業系統,您的應用程式將無法成功偵測 Windows 10。
您也可以檢查用來建立應用程式的 .NET Framework 版本:
Console.WriteLine(AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName);