小编Dav*_*vid的帖子

自动缩放但仍处理WM_DPICHANGED

我在使用C#编写的非常复杂的WinForms应用程序时遇到了一些问题.我希望应用程序在更改DPI时让Windows自动缩放但我仍然需要挂钩WM_DPICHANGED事件以缩放某些自定义绘制文本.

困境是,如果我离开应用程序DPI不知道WM_DPICHANGED消息永远不会在DefWndProc中被截获,并且永远无法检索到正确的DPI标度,但是表单"自动缩放"我想要的方式.但是,如果我将应用程序设置为DPI Aware,则会拦截WM_DPICHANGED消息,并且可以计算出正确的DPI,但表单不会"自动缩放".

正如我所说,应用程序非常复杂并使用了大量第三方控件,因此我无法花时间在WPF中重写应用程序或尝试自行扩展应用程序.

如何让应用程序拦截WM_DPICHANGED消息,计算正确的DPI并仍然允许Windows管理表单缩放?

Program.cs中:

static class Program
{       
    [STAThread]
    static void Main()
    {
        if (Environment.OSVersion.Version.Major >= 6)
        {
            // If the line below is commented out then the app is no longer DPI aware and the 
            // WM_DPICHANGED event will never fire in the DefWndProc of the form
            int retValue = SetProcessDpiAwareness(ProcessDPIAwareness.ProcessPerMonitorDPIAware);

        }

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    private enum ProcessDPIAwareness
    {
        ProcessDPIUnaware = 0,
        ProcessSystemDPIAware = 1,
        ProcessPerMonitorDPIAware = 2
    }

    [DllImport("shcore.dll")]
    private static extern int SetProcessDpiAwareness(ProcessDPIAwareness …
Run Code Online (Sandbox Code Playgroud)

c# dpi winforms

3
推荐指数
1
解决办法
1474
查看次数

标签 统计

c# ×1

dpi ×1

winforms ×1