/win32-dpi

Example of properly DPI-scaling Win32 windows across XP to latest Windows 10

Primary LanguageC++ISC LicenseISC

Win32 DPI-aware window example

Trivial example on how to write Win32 DPI-aware GUI application that scales properly on everything starting Windows XP and ending with latest Windows 10 (tested on insider build 10.0.18980.1).

In a nutshell

  • Primary monitor is usually at System DPI (unless changed without restart)
  • Other monitors can be at higher or lower DPI than System
  • All metrics normally (pre-v2) reported to the application are in System DPI
  • Some APIs (GetThemeFont) scale the reported values when in PerMonitorV2 awareness mode
  • It's error-prone to call GetSystemMetrics wherever needed and scale manually, precompute these
  • Mostly everything derives metrics from font size: recreate fonts on DPI change, remember height, rescale accordingly
  • Window and Taskbar icons sizes change too

Manifest

For the application to support DPI scaling to the full extent of what the underlying Operating System supports, the process DPI awareness must be set. That's accomplished either through manifest, or calling API(s). The API way is complicated. We will use my rsrcgen.exe tool to generate manifest that will request everything known so far, and then deal with what we get at runtime.

API alternative:

Additional reading

TODO

  • Extend icon to include all realistic sizes

ISC License

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.