以编程方式更改Windows 10文本大小(DPI)而不重新启动

时间:2017-11-21 12:16:14

标签: c# powershell windows-10 dpi

我需要一个程序,将Win 10设置中文本大小滑块的值拖动到100%(或其他一些参数化值)。

Settings

有一些描述的方法,即:Change windows dpi setting C#。但是需要重新启动才能应用注册表更改。我正在寻找一个更改Win 10 DPI而不重启的脚本。

我希望在C#或PowerShell中实现这一点,最好没有任何大的链接外部库。这可能吗?

2 个答案:

答案 0 :(得分:0)

我选择了一个非常基本且不太优雅的解决方案,但它完成了所需的工作:

using System.Threading;
using System.Windows.Forms;

 ...

    static void Main(string[] args)
    {
        System.Diagnostics.Process.Start("ms-settings:display");
        Thread.Sleep(1000);
        AppActivate("Nastavení");

        if (args.Length < 2) //After creators update the form is a bit different
        {
            SendKeys.SendWait("^(e)"); //Sets focus to search textbox
            PressTab(5); //Tabs to DPI
            SetCU(args);
            PressTab(1); //When logoff is visible then one tab more
            SetCU(args);
        }
        else //For PCs not having the creators update installed
        {
            PressTab(3);                
            SetNonCU(args);
            PressTab(1); //When logoff link is visible  then one tab more
            SetNonCU(args);
        }
        Thread.Sleep(100);
        SendKeys.SendWait("%{F4}");            
    }

    private static void PressTab(int count)
    {
        for (int i = 0; i < count; i++)
        {
            SendKeys.SendWait("{TAB}");
            Console.WriteLine("TAB");
            Thread.Sleep(100);
        }
    }

    private static void SetNonCU(string[] args)
    {
        Console.WriteLine("Set non CU");
        SendKeys.SendWait("{LEFT}");
        Thread.Sleep(100);
        SendKeys.SendWait("{LEFT}");
        Thread.Sleep(100);
        SendKeys.SendWait("{LEFT}");
        Thread.Sleep(100);
        SendKeys.SendWait("{LEFT}");
        Thread.Sleep(100);
        SendKeys.SendWait("{LEFT}");
        Thread.Sleep(100);
        if (args[0].Equals("1"))
        {
            SendKeys.SendWait("{RIGHT}");
        }
        else
        if (args[0].Equals("2"))
        {
            SendKeys.SendWait("{RIGHT}");
            Thread.Sleep(100);
            SendKeys.SendWait("{RIGHT}");
        }
        Thread.Sleep(100);
    }

    private static void SetCU(string[] args)
    {
        Console.WriteLine("Set CU");
        SendKeys.SendWait("{UP}");
        Thread.Sleep(100);
        SendKeys.SendWait("{UP}");
        Thread.Sleep(100);
        SendKeys.SendWait("{UP}");
        Thread.Sleep(100);
        SendKeys.SendWait("{UP}");
        Thread.Sleep(100);
        SendKeys.SendWait("{UP}");
        Thread.Sleep(100);
        if (args[0].Equals("1"))
        {
            SendKeys.SendWait("{DOWN}");
        }
        else
        if (args[0].Equals("2"))
        {
            SendKeys.SendWait("{DOWN}");
            Thread.Sleep(100);
            SendKeys.SendWait("{DOWN}");
        }
        Thread.Sleep(100);
    }

    [DllImportAttribute("User32.dll")]
    private static extern int SetForegroundWindow(int hWnd);

    private static bool AppActivate(string titleName)
    {
        var success = true;
        var process = Process.GetProcesses()
                      .Where(p => p.MainWindowTitle.StartsWith(titleName))
                      .FirstOrDefault();
        if (process != null)
        {
            SetForegroundWindow(process.MainWindowHandle.ToInt32());
        }
        else
        {
            success = false;
        }
        return success;
    }

答案 1 :(得分:0)

(由于输入错误而编辑) 电源外壳。确保设置执行策略。

autocomplete="off"