是否有SetProcessDPIAware的反函数,支持Windows 7?或者如何回归到底是什么?

问题描述 投票:2回答:1

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

我读了文章Setting the default DPI awareness for a processSetProcessDpiAwareness()SetProcessDpiAwarenessContext()不适用于Windows 7或Windows Vista。

在调用SetProcessDPIAware()之后,如何返回之前的DPI意识?

c# windows winforms winapi dpi
1个回答
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());
    }
}

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

enter image description here

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();
}

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.