How do I make the scrollbar's thumbtrack resize

3.5k views Asked by At

If you open Notepad, add ten lines and resize the editor to only show nine, the thumb track (the draggable block on a scroll bar) is almost the full length of the vertical scroll bar. If you add a couple of thousand rows, the thumb track is only a few pixels high.

I'd like to duplicate this bahaviour in a TCustomControl descendant where I implemented a horizontal scroll bar. So I added WS_HSCROLL to my window style (in CreateParams) and implemented a handler for WM_HSCROLL. Along the way, I use SetScrollRange and SetScrollPos to manage the range and the position of the thumb track - but the little bugger remains a near-perfect square.

What am I missing?

2

There are 2 answers

2
Rob Kennedy On BEST ANSWER

The documentation recommends using SetScrollInfo rather than SetScrollRange and SetScrollPos.

As Wouter's answer points out, you also need to set the page size. The position and range tell the OS where the center of the thumb belongs, but the page size tells it how much of the range is visible, and that's what determines the size of the thumb. You have to use SetScrollInfo for that; as a bonus, it lets you set the position, range, and page size all at once.


TCustomControl differs from TWinControl in just one way: It has a canvas. TScrollingWinControl differs in just one way, too: It has scroll bars. Adding a canvas to a TScrollingWinControl descendant should be much easier than adding scroll bars to a TCustomControl descendant — it's less code to copy and paste from the VCL source code. Change your control's base class and then see where you are.

Even if that's not an option, you'd still do well to look at how TScrollingWinControl and TControlScrollBar work together.

1
Wouter van Nifterick On

You can use the PageSize property to influence the thumb size.

Example:

ScrollBar1.Min      := 0;
ScrollBar1.Max      := 100;
ScrollBar1.Position := 70;
ScrollBar1.PageSize := 50;

Will look like:

Page Size