winformslistviewwinapiscrollbardpi-aware

Setting/scaling width of control scrollbars


When per-monitor DPI changes (WM_DPICHANGED message), the built-in scrollbars of controls like list view, tree view, rich edit, listbox are not scaled (as expected).

Unscaled scrollbar

They need to be scaled programmatically.

Though I didn't find any API (neither in Win32, let alone in WinForms) to set scrollbar size.

What API can I use to set scrollbar size (width)?

Or at least how I do get hold of the scrollbar handle? (I assume that internally the scrollbars are separate child controls) Once I have the handle, I assume I can use MoveWindow to resize it (Edit: my assumption was incorrect, as the answer by @Anders shows)


For a background, see High DPI Desktop Application Development on Windows.


There are couple seemingly duplicate questions, but none of them are actually relevant:

The answers there either change system-wide settings or work for DataGrid only (that have separate child scrollbar controls available in its interface).


Solution

  • As suggested by @Anders, the Per Monitor V2 DPI awareness in Windows 10 Creators Update resolves this.

    Scaled control scrollbar

    To enable Per Monitor V2 DPI awareness, while still supporting old Per Monitor DPI awareness on older Windows 10 builds and Windows 8.1 and DPI awareness on yet older versions of Windows, make your application manifest like this:

    <assembly ...>
        <!-- ... --->
        <asmv3:application>
            <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
                <dpiAware>True/PM</dpiAware>
                <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2,PerMonitor</dpiAwareness>
            </asmv3:windowsSettings>
        </asmv3:application>
    </assembly>
    

    References: