Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Winforms - Adjust width of vertical scrollbar on CheckedListBox
  • Change width of scrollbars

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

like image 294
Martin Prikryl Avatar asked Mar 08 '17 14:03

Martin Prikryl


People also ask

How do I change the scrollbar width?

The scrollbar-width property is used to set the width or thickness of an element's scrollbar when shown. This property can be used on pages where the user interface requires the element to be displayed more prominently and shrinking the scrollbar width gives more space to the element.

How wide are scrollbars?

The default scrollbar width can range anywhere from 12px to 17px. Webkit browsers also have the ability for the user to configure scrollbar widths. Webkit browsers, such as Chrome can have custom scrollbars that can have any size scrollbar.

How do we get the width of a webpage with or without scrollbars?

To get the width of the scrollbar, you use the offsetWidth and clientWidth of the Element : The offsetWidth returns the width of the Element in pixels including the scrollbar. The clientWidth returns the with of the Element in pixels without the scrollbar.

How do I make my scrollbar thinner CSS?

scrollbar-width accepts the following values: auto is the default value and will render the standard scrollbars for the user agent. thin will tell the user agent to use thinner scrollbars, when applicable. none will hide the scrollbar completely, without affecting the element's scrollability.


1 Answers

Scrollbars are usually not separate windows. If a window uses the WS_HSCROLL/WS_VSCROLL styles then the scrollbars are implemented in the non-client area of the control itself.

Windows 10 Creators Update is going to introduce something called Per Monitor V2 DPI awareness. This includes child window notifications, "Windows Forms DPI scaling improvements" and "Improved theming behavior". It is also going to automatically scale the non-client area.

In the meantime you can try calling EnableNonClientDpiScaling in WM_NCCREATE (added in the Anniversary Update).

like image 179
Anders Avatar answered Nov 10 '22 00:11

Anders