SetProcessDPIAware 是否有支持 Windows 7 的反函数?或者如何回到原来的状态?

SQL*_*rog 5 c# windows winapi dpi winforms

我在 WinForms 应用程序中使用该SetProcessDPIAware()函数。user32.dll调用后SetProcessDPIAware(),我需要返回到之前的DPI感知流程。

我阅读了文章设置进程的默认 DPI 感知SetProcessDpiAwareness()并且SetProcessDpiAwarenessContext()不适用于 Windows 7 或 Windows Vista。

在调用某个进程后,如何返回到之前的 DPI 感知SetProcessDPIAware()

Rez*_*aei 0

作为一个选项,您可以重新启动应用程序,并根据设置或命令行参数决定是否要设置进程 DPI 感知。

Settings您可以在文件夹下的文件中创建布尔用户设置属性Properties。此设置将确定是否启用 DPI 感知。然后当应用程序启动时,检查该设置是否启用,然后调用SetProcessDPIAware

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
static class Program
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SetProcessDPIAware();

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        if (Environment.OSVersion.Version.Major >= 6 &&
            Properties.Settings.Default.DPIAware)
            SetProcessDPIAware();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(true);
        Application.Run(new Form1());
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,在主 UI 表单中,您可以检查设置并显示如下消息,并允许用户通过启用或禁用 DPI 感知来重新启动应用程序。为此,只需设置设置值,保存设置并调用Application.Restart()

在此输入图像描述

private void Form1_Load(object sender, EventArgs e)
{
    if (Properties.Settings.Default.DPIAware)
        toolStripLabel1.Text = "DPI-awareness is enabled. Restart to disable DPI-awareness.";
    else
        toolStripLabel1.Text = "DPI-awareness is disabled. Restart to enable DPI-awareness.";
}
private void toolStripLabel1_Click(object sender, EventArgs e)
{
    Properties.Settings.Default.DPIAware = !Properties.Settings.Default.DPIAware;
    Properties.Settings.Default.Save();
    Application.Restart();
}
Run Code Online (Sandbox Code Playgroud)

不要忘记创建 DPIAware 设置,它将告诉我们是否要调用SetProcessDPIAware方法main

在此输入图像描述