簡體   English   中英

如何在 C# 中更改 DPI 縮放行為

[英]How to Change DPI Scaling behavior in C#

在我們的應用程序中,我們的功能在 4k 顯示器上無法正常工作。 當我們右鍵單擊可執行文件並更改兼容性設置時,我們可以通過更改以下設置來使事情正常工作。

我的問題是:是否可以從 WPF 應用程序的 c# 代碼中更改紅色框中突出顯示的這兩個設置? 我想執行一個方法,更改這些設置,當 5 秒的計時器結束時,我想將它們更改回來。

這是我獲取我正在識別的屏幕的坐標的方式。

private void btn_identifyScreens_Click(object sender, RoutedEventArgs e)
{
   int screenNumber = 1;
   foreach (System.Windows.Forms.Screen screen in System.Windows.Forms.Screen.AllScreens)
   {
       NumberSplash numberSplash = new NumberSplash(screenNumber);
       numberSplash.Left = screen.WorkingArea.Left;
       numberSplash.Top = screen.WorkingArea.Top;
       numberSplash.Width = screen.WorkingArea.Width;
       numberSplash.Height = screen.WorkingArea.Height;
       numberSplash.Show();
       screenNumber++;
   }
}

在此處輸入圖片說明

如果您不想弄亂 win32-api,您可以在 app-manifest 中為您的應用程序設置 dpi-awareness。 例如,每個監視器 dpi 感知:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
...
    <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">
         PerMonitor   
     </dpiAwareness>
        </windowsSettings>
    </application>
...
</assembly>

在 GitHub 上查看上述內容: https : //github.com/Microsoft/WPF-Samples/tree/master/PerMonitorDPI

如果您想在應用程序中進行細粒度控制,我相信您可能必須使用 win-api。 這里有一篇很棒的文章描述了這一點: https : //blogs.windows.com/buildingapps/2017/04/04/high-dpi-scaling-improvements-desktop-applications-windows-10-creators-update/

您也可以直接設置注冊表項 Computer\\HKEY_CURRENT_USER\\Control Panel\\Desktop(但這會影響所有程序,正如 Ben 在下面的評論中指出的那樣)這些鍵的描述如下: https : //docs.microsoft.com/en-我們/windows-hardware/manufacture/desktop/dpi-related-apis-and-registry-settings

編輯:第一種方法是使用清單使應用程序 DPI 感知,然后如果您需要應用程序處理 dpi 更改並使其真正感知 DPI,您將需要查詢 GetDpiForMonitor(對於每個監視器感知)。 這里有一個帶有包裝器的類似問題: How to get DPI scale for all screen?

2:nd 編輯:這里的評論后是在每個屏幕上顯示監視器數字的示例: https : //gitlab.com/mattincode/wpf_dpi_aware.git

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM