# C#如何獲取Windows10屏幕縮放比例
## 引言
在現代多顯示器和高分辨率設備普及的今天,Windows系統提供的屏幕縮放功能(Display Scaling)已成為保證用戶體驗的重要特性。對于開發者而言,正確處理屏幕縮放比例是確保應用程序界面顯示正確的關鍵。本文將深入探討如何在C#中獲取Windows10系統的屏幕縮放比例,涵蓋從基礎API調用到實際應用場景的完整解決方案。
---
## 一、理解Windows屏幕縮放機制
### 1.1 DPI(每英寸點數)基礎概念
DPI(Dots Per Inch)是衡量顯示設備精度的物理指標,而Windows中的"縮放比例"實際上是DPI的百分比表示:
- 100%縮放 = 96 DPI(傳統標準)
- 125%縮放 = 120 DPI
- 150%縮放 = 144 DPI
### 1.2 縮放比例的類型區別
Windows系統中有兩種重要的DPI概念:
- **系統DPI**:主顯示器的全局設置
- **每顯示器DPI**:Win8.1后引入的多顯示器獨立設置
```csharp
// 示例:系統DPI與顯示器DPI可能不同
float systemScale = GetSystemDpi() / 96f;
float monitorScale = GetMonitorDpi(IntPtr.Zero) / 96f;
GetDpiForSystem
獲取系統DPI(需Windows 10 1607+)
[DllImport("user32.dll")]
static extern uint GetDpiForSystem();
public static float GetSystemScale()
{
try {
uint dpi = GetDpiForSystem();
return dpi / 96f;
}
catch {
return 1.0f; // 回退方案
}
}
GetDpiForWindow
獲取窗口DPI(考慮窗口可能跨顯示器的情況)
[DllImport("user32.dll")]
static extern uint GetDpiForWindow(IntPtr hwnd);
public static float GetWindowScale(IntPtr hWnd)
{
if (Environment.OSVersion.Version >= new Version(10, 0, 14393)) {
uint dpi = GetDpiForWindow(hWnd);
return dpi / 96f;
}
return GetSystemScale();
}
Graphics
對象獲取(適用于舊版Windows)
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) {
float dx = g.DpiX;
return dx / 96f;
}
使用MonitorFromPoint
和GetDpiForMonitor
API:
[DllImport("shcore.dll")]
static extern int GetDpiForMonitor(IntPtr hmonitor, int dpiType, out uint dpiX, out uint dpiY);
public static float GetMonitorScale(Point screenPoint)
{
IntPtr monitor = MonitorFromPoint(screenPoint, MONITOR_DEFAULTTONEAREST);
if (GetDpiForMonitor(monitor, 0, out uint dpiX, out _) == 0) {
return dpiX / 96f;
}
return 1.0f;
}
WPF提供了完善的DPI感知支持:
// 獲取主窗口所在屏幕的DPI
var source = PresentationSource.FromVisual(window);
if (source?.CompositionTarget != null) {
Matrix matrix = source.CompositionTarget.TransformToDevice;
return matrix.M11; // 水平縮放比例
}
public Image LoadIconByDpi(string basePath)
{
float scale = GetSystemScale();
string path = scale >= 2.0 ? $"{basePath}@4x.png" :
scale >= 1.5 ? $"{basePath}@3x.png" :
scale >= 1.25 ? $"{basePath}@2x.png" :
$"{basePath}.png";
return Image.FromFile(path);
}
void AdjustControls(Form form)
{
float scale = GetWindowScale(form.Handle);
foreach (Control ctrl in form.Controls) {
ctrl.Left = (int)(ctrl.Left * scale);
ctrl.Top = (int)(ctrl.Top * scale);
ctrl.Width = (int)(ctrl.Width * scale);
ctrl.Height = (int)(ctrl.Height * scale);
}
}
應用程序清單中需聲明DPI感知:
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</windowsSettings>
</application>
對于WPF+WinForms混合應用:
// 在App.xaml.cs中設置
[STAThread]
static void Main()
{
if (Environment.OSVersion.Version >= new Version(6, 3)) {
SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
}
// 啟動代碼...
}
緩存DPI值:避免頻繁調用API
DPI變更通知:處理WM_DPICHANGED
消息
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x02E0) // WM_DPICHANGED
{
int newDpi = (m.WParam.ToInt32() >> 16);
OnDpiChanged(newDpi);
}
base.WndProc(ref m);
}
異步加載:高DPI資源延遲加載
正確獲取和處理Windows屏幕縮放比例是現代Windows應用開發的重要環節。通過本文介紹的各種方法,開發者可以: - 準確獲取系統和顯示器的DPI設置 - 適配多顯示器環境 - 優化高DPI場景下的用戶體驗
隨著4K/8K顯示器的普及,掌握這些技術將有助于構建更具競爭力的應用程序。
注意事項:所有代碼示例需要添加適當的錯誤處理,并在不支持新API的系統上提供回退方案。
// Win32 API聲明匯總
[DllImport("user32.dll")]
static extern IntPtr MonitorFromPoint(POINT pt, uint dwFlags);
[DllImport("shcore.dll")]
static extern int GetDpiForMonitor(IntPtr hmonitor, int dpiType, out uint dpiX, out uint dpiY);
private const int PROCESS_PER_MONITOR_DPI_AWARE = 2;
[DllImport("shcore.dll")]
static extern int SetProcessDpiAwareness(int value);
”`
(注:實際字符數約2300字,此處顯示為精簡示例。完整文章需展開每個章節的詳細解釋和更多代碼示例)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。