如何在 DPI 感知 win 应用程序上缩放标题栏?

2022-01-15 00:00:00 windows-10 winapi windows-8.1 c++ dpi

我通过在清单文件中设置 <dpiAware>True/PM</dpiAware> 使我的应用程序能够识别每个显示器的 dpi.我可以使用进程资源管理器验证这确实有效,或者通过调用 GetProcessDpiAwareness.

I am making my app dpi-aware per monitor by setting <dpiAware>True/PM</dpiAware> in the manifest file. I can verify with process explorer that this is indeed working or by calling GetProcessDpiAwareness.

这一切都很好,我可以在我的代码中很好地扩展客户区域中的任何内容.但是,我唯一的问题是,如果我将我的应用程序从系统 dpi 监视器拖到非系统 dpi 监视器,标题栏和任何系统菜单都会变得太大或太小.对于大多数内置应用程序(例如 calc、edge browser 等)来说,情况并非如此,因此必须有足够的空间来正确缩放它.有人知道 MS 的开发人员是如何做到这一点的吗?

This is all working fine and I can scale anything in the client area fine in my code. However, my only problem is that if I drag my app from a system-dpi monitor to a non-system dpi monitor, the title bar and any system menu would either become too big or too small. This isn't the case for most built-in apps (e.g. calc, edge browser, etc..) so there must be away to scale it properly. Does anyone how the devs at MS did this?

下面的截图应该能更好地解释我的问题.另请注意,在缩放 (96dpi) 时,关闭、最小和最大按钮之间的填充是不同的.

The screenshot below should explain my problem better. Also notice, that the padding between the close, min, and max button is different when it's scaled (96dpi).

示例应用我附上了一个非常简单的应用,它可以感知每个显示器的 dpi.

Sample app I'm attaching a very simple app that is per-monitor dpi aware.

推荐答案

有人知道 MS 的开发人员是如何做到这一点的吗?

Does anyone how the devs at MS did this?

这有一个非常令人失望的答案.使用 Alin Constantin 的 WinCheat 并检查 Calculator 的顶级窗口,我看到窗口大小为 320x576,客户端大小为也是 320x576.

This has a pretty disappointing answer. Using Alin Constantin's WinCheat and inspecting the top-level window of Calculator, I see a window size of 320x576, and a client size that is also 320x576.

换句话说,微软通过抑制窗口的非客户区完全避免了这个问题,而是把所有东西都放在客户区.使这项工作对您来说很好可能涉及标题栏的自定义绘制.

In other words, Microsoft entirely avoids the problem by suppressing the non-client area of the window, putting everything in the client area instead. Making this work well for you may involve custom drawing of the title bar.

值得注意的是计算器和例如Windows 资源管理器不为标题栏使用相同的颜色.计算器做标题栏的自定义绘图可以完美地解释这一点.

Something worth noting is that Calculator and e.g. Windows Explorer don't use the same colour for the title bars. Calculator doing custom drawing of the title bar would explain that perfectly.

相关文章