• Stars
    star
    114
  • Rank 308,031 (Top 7 %)
  • Language
    C++
  • License
    ISC License
  • Created about 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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 11 (tested on insider build 10.0.22523.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
    • the OS may ask for different DPI icon (e.g. for Taskbar on monitor with different DPI)
  • Since Windows 10 there is mismatch between reality and documented Taskbar icon size
    • Taskbar used to use ICON_BIG/SM_CXICON-sized icon (32×32) but starting with Windows 10, it's resized down to 24×24 (so called Start size on XP)
    • there is no API to query, so testing OS version, and updating code as Microsoft makes changes, remains
    • Alt+Tab in Windows 11 now does the same, takes BIG icon, and scales it down to size of a SMALL one (16×16)

Additional

  • The example also shows how to track "TextScaleFactor" for UWP apps, see Settings > Accessibility > Text size, and apply it to Win32 window content scaling.
  • Multiple WM_SETTINGCHANGE and other GUI change notifications can come in quick succession, so it is possible to alleviate excess refresh and flickering by coalescing those, to improve user experience.

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

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.

More Repositories

1

icomake

Combine multiple ICO/PNGs into single .ICO file, retaining format of each sub-image, optimizing the order.
C++
22
star
2

rwspinlock

Slim, simple, cross-process, reader-writer unfair fast spin lock for Windows
C++
10
star
3

adaptive-rounding

Smart floating-point number rounding algorithm. Attempts to round the number "nicely" like a human would do.
C++
9
star
4

doubleinteger

C++ large integer template, which doubles the width of little-endian integers and can be nested
C++
9
star
5

x32-abi-windows

Test/benchmark of using 32-bit pointers in 64-bit code on Windows. Not an actual ABI, only inspired by Linux's x32 ABI.
C++
9
star
6

code-style-agnostic-search

Coding Style -agnostic (and more) search for C++
C++
7
star
7

rsrcgen

Windows Version Info and Manifest generator
C++
7
star
8

setmica

Tool that applies Mica backdrop to Win32 window
C++
5
star
9

defprep

DLL Module-Definition (.Def) Files Preprocessor
C++
5
star
10

winver

Tiny Windows executable that outputs version information about the OS.
C++
5
star
11

win32-wstring_view

Experimental reimplementations of some Windows API functions taking std::wstring_view as argument
C++
4
star
12

cuckoo

Cuckoo Cycle simple miner C++ template
C
2
star
13

taskbar-sound-badges

Small program that monitors which applications are currently playing sound, and sets Badge on their Taskbar icons.
C++
2
star
14

emphasize

generally useful assorted C/C++ Windows API code
C++
2
star
15

ext

Simple extensions to the C++ library
2
star
16

atomlist

Enumerate list of user32 atoms in current session, an exercise in getting EXE as small as possible with MSVC.
C++
2
star
17

papers

Papers & proposals, for C++ programming language, Microsoft Windows OS, and other
2
star
18

lamp

If you need a lamp, but all you have are monitors
C++
1
star
19

SQLite

Simple C++ abstraction of SQLite
C++
1
star
20

win32-hub-events

Downlevel and sample implementation of (proposed) Hub Events
C++
1
star
21

win32-iocp-events

Example of waiting for Event Objects by associating them with a I/O Completion Port (IOCP), effectively lifting MAXIMUM_WAIT_OBJECTS (64) limit of WaitForMultipleObjects(Ex) API.
C++
1
star