我在我的WinForms应用程序中使用来自user32.dll的SetProcessDPIAware()函数。在调用SetProcessDPIAware()之后,我需要返回到以前对该进程的DPI感知。
我读了Setting the default DPI awareness for a process这篇文章。SetProcessDpiAwareness()和SetProcessDpiAwarenessContext()不能在Windows7或Windows Vista上运行。
如何在为进程调用SetProcessDPIAware()后恢复到以前对该进程的DPI感知?
发布于 2019-04-29 23:48:27
作为一种选择,您可以重新启动应用程序,并根据设置或命令行参数决定是否要设置process DPI aware。
您可以在Properties文件夹下的Settings文件中创建布尔用户设置属性。此设置将确定是否启用了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()即可

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设置,它将告诉我们是否要在main方法中调用SetProcessDPIAware:

https://stackoverflow.com/questions/55903707
复制相似问题